Reputation: 11
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
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);
DATE
If you're storing your date as a date
SELECT CONVERT(VARCHAR(10), @Date, 111);
Outputs:
2014/12/02
Upvotes: -1
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
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