Kleber Yassuda
Kleber Yassuda

Reputation: 11

Date Format dd/mm/yyyy to yyyy/mm/dd

How can I convert dd/mm/yyyy to yyyy/mm/dd. Sometimes SQL Server changes month with day.

For example: 02/12/2014(dd/mm/yyyy) to 2014/02/12(yyyy/dd/mm) but correct is 2014/12/02(yyyy/mm/dd).

Upvotes: 1

Views: 3781

Answers (3)

Evaldas Buinauskas
Evaldas Buinauskas

Reputation: 14077

I'd go for this:

If you're storing your date as a string (which is not recommended)

DECLARE @Date VARCHAR(10) = '02/12/2014';

SELECT CONVERT(VARCHAR(10), CONVERT(DATE, @Date, 103), 111);
  1. Ensure that your date can be converted to DATE
  2. Convert it to desired date style.

If you're storing your date as a date

SELECT CONVERT(VARCHAR(10), @Date, 111);

Outputs:

2014/12/02

Upvotes: -1

Abhishek
Abhishek

Reputation: 2490

You can use the below -

SELECT CONVERT(NVARCHAR(10),<yourdate>,103) -- for dd/mm/yyyy

SELECT CONVERT(NVARCHAR(10),<yourdate>,111) -- for yyyy/mm/dd

Upvotes: 2

Ste Bov
Ste Bov

Reputation: 856

If you want to specify the format of the date you need to convert it (slightly annoying)

For the yyyy/mm/dd format use

convert(varchar, getdate(), 111)

you can see a good list of the different format types here https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

Upvotes: 2

Related Questions