I want to write a query to get Date in dd/MM/yyyy format. (I do not want time).
So i wrote query like
SELECT Convert(varchar,A.InsertDate,103) as Tran_Date
But when i write order by Tran_Date, it is giving me the result in wrong order.
Can some body please suggest what should i do.
Thanks
To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.
In an Excel sheet, select the cells you want to format. Press Ctrl+1 to open the Format Cells dialog. On the Number tab, select Custom from the Category list and type the date format you want in the Type box. Click OK to save the changes.
dd/MM/yyyy — Example: 23/06/2013. yyyy/M/d — Example: 2013/6/23. yyyy-MM-dd — Example: 2013-06-23.
For this, simply pick Date in the Number Format box on the Home tab. To apply a format other than default, then select the cells with serial numbers and press Ctrl+1 to open the Format Cells dialog. On the Number tab, choose Date, select the desired date format under Type and click OK. Yep, it's that easy!
Give a different alias
SELECT Convert(varchar,A.InsertDate,103) as converted_Tran_Date from table as A
order by A.InsertDate
You need to use convert in order by as well:
SELECT Convert(varchar,A.InsertDate,103) as Tran_Date
order by Convert(varchar,A.InsertDate,103)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With