Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert Datetime to Date in dd/MM/yyyy format

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

like image 223
Vin05 Avatar asked Jul 16 '12 09:07

Vin05


People also ask

How do I convert DateTime to date format?

To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.

How do I change date format to mm dd yyyy?

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.

What is dd mm yyyy format example?

dd/MM/yyyy — Example: 23/06/2013. yyyy/M/d — Example: 2013/6/23. yyyy-MM-dd — Example: 2013-06-23.

How do I change the date format from text to dd mm yyyy in Excel?

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!


2 Answers

Give a different alias

SELECT  Convert(varchar,A.InsertDate,103) as converted_Tran_Date from table as A
order by A.InsertDate 
like image 76
Madhivanan Avatar answered Oct 25 '22 16:10

Madhivanan


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)
like image 41
Habib Avatar answered Oct 25 '22 18:10

Habib