Answered by:
SQL query to convert date from DD/MM/YYYY to YYYY-MM-DD

Question
-
Hi Everyone,
Could you please suggest how to convert specific date column to YYYY-MM-DD format.
Thanks in advance.Tuesday, December 13, 2016 8:07 AM
Answers
-
Hi BikashBiswal,
You may learn to use parameter style which is an integer expression that specifies how the CONVERT function is to translate expression.
SELECT CONVERT(varchar(10), CONVERT(date, '13/12/2016', 103), 120)
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by Kalman Toth Thursday, December 22, 2016 10:18 AM
Tuesday, December 13, 2016 10:24 AM
All replies
-
DECLARE @dt AS VARCHAR(20)='15/12/2016'
SELECT CONCAT(RIGHT(@dt,4),'-',SUBSTRING(@dt,4,2),'-',LEFT(@dt,2))Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
SQL Server Integration Services: Business Intelligence
- Proposed as answer by SequelMate Tuesday, December 13, 2016 9:25 AM
Tuesday, December 13, 2016 8:12 AMAnswerer -
-
We can also change the date format by using CONVERT function.
Ramesh. M
Tuesday, December 13, 2016 9:20 AM -
Hi BikashBiswal,
If the data type of the column is DATE, then the value should be in YYYY-MM-DD format.
See the MSDN documentation
https://msdn.microsoft.com/en-IN/library/ms186724.aspx
Tuesday, December 13, 2016 9:32 AM -
In SQL 2012 and up check out FORMAT() function in Help if you'd like to return formatted data to the client. Normally all custom formatting should be done in the client application itself and SQL Server should just return raw data.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles- Proposed as answer by Sam ZhaMicrosoft contingent staff Tuesday, December 13, 2016 10:19 AM
Tuesday, December 13, 2016 9:42 AM -
Hi BikashBiswal,
You may learn to use parameter style which is an integer expression that specifies how the CONVERT function is to translate expression.
SELECT CONVERT(varchar(10), CONVERT(date, '13/12/2016', 103), 120)
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by Kalman Toth Thursday, December 22, 2016 10:18 AM
Tuesday, December 13, 2016 10:24 AM -
DECLARE @Data DATETIME = GETDATE (); SELECIONAR @Data SELECT CAST (@Data AS DATE) TRY_CONVERT (DATETIME, '13 / 12/2016 ', 103)
Another approach using cast and convertWesley Neves
Tuesday, December 13, 2016 11:01 AM -
As suggested above, you can double apply the CONVERT function:
DECLARE @DDMMYYYY char(10) = '13/12/2016'; SELECT CONVERT(varchar(10), CONVERT(date, @DDMMYYYY, 103), 120); -- 2016-12-13
String to/from datetime conversions: http://www.sqlusa.com/bestpractices/datetimeconversion/
Kalman Toth Database & OLAP/AI Architect SQL Server 2016 Database Design
New Book : Beginner Database Design & SQL Programming Using Microsoft SQL Server 2016
Thursday, December 22, 2016 10:17 AM -
I got this script a long time ago, and I do not remember from where. But I run it every time that I have to format a date or time. It shows you what format value to use in the CONVERT statement.
/******************************************************************************************* sp_datetime_formats Written By: Doug Deneau, FinServ Technology Services, PLC (Fettan) Company Desc: Payment services technology company in Ethoipia Date Written: March, 2009 Website: www.GoFettan.com This stored procedure is used to generate all datetime formats when using the CONVERT(varchar(30),@MyDate,<style>) function. It shows sample formats of each variation in style parameter. The output also shows samples for the various date arguments within the date functions. INPUT PARAMTERS --------------- <none> OUTPUT DATA ----------- WO_Century - Style argument value without the century in the year (2-digit year) WO_Cent_No_Offset - Sample output without the century in the year and no offset WO_Cent_With_Offset - Sample output without the century in the year and offset W_Century - Style argument value with the century in the year (4-digit year) W_Cent_No_Offset - Sample output with the century in the year and no offset W_Cent_With_Offset - Sample output with the century in the year and offset DateName_Parm - Date Part argument name for use in various date functions DateName_Example - Sample data from using the corresponding Date Part argument MODIFICATION LOG ---------------- <name> - <datetime> - <description of change> ********************************************************************************************/ set nocount on declare @Fmt int, @More char(1), @Today datetime, @TodayO datetimeoffset select @Today = GETDATE() select @TodayO = SYSDATETIMEOFFSET() create table #data (WO_Century varchar(5) null, WO_Cent_No_Offset varchar(50) null, WO_Cent_With_Offset varchar(50) null, W_Century int not null, W_Cent_No_Offset varchar(50) null, W_Cent_With_Offset varchar(50) null, DateName_Parm varchar(50) null, DateName_Example varchar(50) null) select @More = 'Y', @Fmt = 0 while @More = 'Y' begin insert into #data (WO_Century, WO_Cent_No_Offset, WO_Cent_With_Offset, W_Century, W_Cent_No_Offset, W_Cent_With_Offset) select @Fmt, Convert(varchar(50),@Today,@Fmt), Convert(varchar(50),@TodayO,@Fmt), @Fmt + 100, Convert(varchar(50),@Today,@Fmt + 100), Convert(varchar(50),@TodayO,@Fmt + 100) select @Fmt = @Fmt + 1 if @Fmt > 14 select @More = 'N' end -- of loop insert into #data (WO_Century, WO_Cent_No_Offset, WO_Cent_With_Offset, W_Century, W_Cent_No_Offset, W_Cent_With_Offset) select 20, Convert(varchar(50),@Today,20), Convert(varchar(50),@TodayO,20), 120, Convert(varchar(50),@Today,120), Convert(varchar(50),@TodayO,120) insert into #data (WO_Century, WO_Cent_No_Offset, WO_Cent_With_Offset, W_Century, W_Cent_No_Offset, W_Cent_With_Offset) select 21, Convert(varchar(50),@Today,21), Convert(varchar(50),@TodayO,21), 121, Convert(varchar(50),@Today,121), Convert(varchar(50),@TodayO,121) insert into #data (W_Century, W_Cent_No_Offset, W_Cent_With_Offset) select 126, Convert(varchar(50),@Today,126), Convert(varchar(50),@TodayO,126) insert into #data (W_Century, W_Cent_No_Offset, W_Cent_With_Offset) select 127, Convert(varchar(50),@Today,127), Convert(varchar(50),@TodayO,127) insert into #data (W_Century, W_Cent_No_Offset, W_Cent_With_Offset) select 130, Convert(varchar(50),@Today,130), Convert(varchar(50),@TodayO,130) insert into #data (W_Century, W_Cent_No_Offset, W_Cent_With_Offset) select 131, Convert(varchar(50),@Today,131), Convert(varchar(50),@TodayO,131) update #data set WO_Century = '<def>' where WO_Century = '0' -- datepart formats update #data set DateName_Parm = 'year,yyyy,yy', DateName_Example = DATENAME(year,@Today) where WO_Century = '0' update #data set DateName_Parm = 'quarter,qq,q', DateName_Example = DATENAME(quarter,@Today) where WO_Century = '1' update #data set DateName_Parm = 'month,mm,m', DateName_Example = DATENAME(month,@Today) where WO_Century = '2' update #data set DateName_Parm = 'dayofyear,dy,y', DateName_Example = DATENAME(dayofyear,@Today) where WO_Century = '3' update #data set DateName_Parm = 'day,dd,d', DateName_Example = DATENAME(day,@Today) where WO_Century = '4' update #data set DateName_Parm = 'week,wk,ww', DateName_Example = DATENAME(week,@Today) where WO_Century = '5' update #data set DateName_Parm = 'weekday,dw', DateName_Example = DATENAME(weekday,@Today) where WO_Century = '6' update #data set DateName_Parm = 'hour,hh', DateName_Example = DATENAME(hour,@Today) where WO_Century = '7' update #data set DateName_Parm = 'minute,mi,mn', DateName_Example = DATENAME(minute,@Today) where WO_Century = '8' update #data set DateName_Parm = 'second,ss,s', DateName_Example = DATENAME(second,@Today) where WO_Century = '9' update #data set DateName_Parm = 'millisecond,ms', DateName_Example = DATENAME(millisecond,@Today) where WO_Century = '10' update #data set DateName_Parm = 'microsecond,mcs', DateName_Example = DATENAME(microsecond,@Today) where WO_Century = '11' update #data set DateName_Parm = 'nanosecond,ns', DateName_Example = DATENAME(nanosecond,@Today) where WO_Century = '12' update #data set DateName_Parm = 'TZoffset,tz', DateName_Example = DATENAME(TZOFFSET,@TodayO) where WO_Century = '13' select * from #data drop table #data go
Friday, December 23, 2016 1:47 PM