0% found this document useful (0 votes)
241 views

Conversion From String To Date

This document provides examples of SQL queries for converting between date, datetime, and string data types in SQL Server. It shows how to convert strings to dates and datetimes using CAST and CONVERT functions, specifying various date and time formats. It also demonstrates retrieving date parts, comparing dates, and other common date operations using strings, dates, and datetimes.

Uploaded by

trushnaj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
241 views

Conversion From String To Date

This document provides examples of SQL queries for converting between date, datetime, and string data types in SQL Server. It shows how to convert strings to dates and datetimes using CAST and CONVERT functions, specifying various date and time formats. It also demonstrates retrieving date parts, comparing dates, and other common date operations using strings, dates, and datetimes.

Uploaded by

trushnaj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

c 


   
    c  
   Y
          
         Y
USE AdventureWorksY
SET LANGUAGE us_englishY
@@ Convert string to date using style (format) numbers @ sql dates formatY
@@ SQL convert text to date @ SQL convert string to date / datetimeY
SELECT convert(datetime,'15/03/18',3) @@ 2018@03@15 00:00:00.000Y
SELECT convert(datetime,'15.03.18',4) @@ 2018@03@15 00:00:00.000Y

@@ Convert datetime to text style (format) list @ sql time formatY


@@ SQL Server without century (YY) date styles (there are exceptions!)Y
@@ Generally adding 100 to style number results in century format CCYY / YYYYY
SELECT convert(varchar,getdate())
@@ Mar 15 2018 10:35AMY
SELECT convert(varchar,getdate(),0) @@ Mar 15 2018 10:35AMY
SELECT convert(varchar,getdate(),1) @@ 03/15/18Y
SELECT convert(varchar,getdate(),2) @@ 18.03.15Y
SELECT convert(varchar,getdate(),3) @@ 15/03/18Y
SELECT convert(varchar,getdate(),4) @@ 15.03.18Y
SELECT convert(varchar,getdate(),5) @@ 15@03@18Y
SELECT convert(varchar,getdate(),6) @@ 15 Mar 18Y
SELECT convert(varchar,getdate(),7) @@ Mar 15, 18Y
SELECT convert(varchar,getdate(),8) @@ 10:39:39Y
SELECT convert(varchar,getdate(),9) @@ Mar 15 2018 10:39:48:373AMY
SELECT convert(varchar,getdate(),10) @@ 03@15@18Y
SELECT convert(varchar,getdate(),11) @@ 18/03/15Y
SELECT convert(varchar,getdate(),15) @@ 180315Y
SELECT convert(varchar,getdate(),13) @@ 15 Mar 2018 10:41:07:590Y
SELECT convert(varchar,getdate(),14) @@ 10:41:25:903Y
SELECT convert(varchar,getdate(),20) @@ 2018@03@15 10:43:56Y
SELECT convert(varchar,getdate(),21) @@ 2018@03@15 10:44:04.950Y
SELECT convert(varchar,getdate(),22) @@ 03/15/18 10:44:50 AMY
SELECT convert(varchar,getdate(),23) @@ 2018@03@15Y
SELECT convert(varchar,getdate(),24) @@ 10:45:45Y
SELECT convert(varchar,getdate(),25) @@ 2018@03@15 10:46:11.263
@@ T@SQL with century (YYYY or CCYY) datetime styles (formats)Y
SELECT convert(varchar, getdate(), 100) @@ Oct 23 2016 10:22AM (or PM)
SELECT convert(varchar, getdate(), 101) @@ 10/23/2016Y
SELECT convert(varchar, getdate(), 102) @@ 2016.10.23Y
SELECT convert(varchar, getdate(), 103) @@ 23/10/2016Y
SELECT convert(varchar, getdate(), 104) @@ 23.10.2016Y
SELECT convert(varchar, getdate(), 105) @@ 23@10@2016Y
SELECT convert(varchar, getdate(), 106) @@ 23 Oct 2016Y
SELECT convert(varchar, getdate(), 107) @@ Oct 23, 2016Y
SELECT convert(varchar, getdate(), 108) @@ 09:10:34Y
SELECT convert(varchar, getdate(), 109) @@ Oct 23 2016 11:10:33:993AM (or PM)Y
SELECT convert(varchar, getdate(), 110) @@ 10@23@2016Y
SELECT convert(varchar, getdate(), 111) @@ 2016/10/23Y
SELECT convert(varchar, getdate(), 112) @@ 20161023Y

SELECT
SELECT
SELECT
SELECT
SELECT
GOY

convert(varchar,
convert(varchar,
convert(varchar,
convert(varchar,
convert(varchar,

getdate(),
getdate(),
getdate(),
getdate(),
getdate(),

113)
114)
120)
121)
126)

@@
@@
@@
@@
@@

23 Oct 2016 06:10:55:383Y


06:10:55:383(24h)Y
2016@10@23 06:10:55(24h)Y
2016@10@23 06:10:55.383Y
2016@10@23T06:10:55.383Y

@@ SQL cast string to datetime @ time part 0 @ sql hh mmY


@@ SQL Server cast string to DATE (SQL Server 2008 feature) @ sql yyyy mm ddY
SELECT [Date] =
CAST('20120228' AS date)
@@ 2012@02@28Y
SELECT [Datetime] = CAST('20120228' AS datetime)Y
@@ 2012@02@28
00:00:00.000Y
SELECT [Datetime] = CAST('20120228' AS smalldatetime)Y@@ 2012@02@28 00:00:00Y

YY

@@ SQL
@@ SQL
SELECT
SELECT

convert string to datetime @ time part 0Y


Server convert string to date @ sql times formatY
[Datetime] = CONVERT(datetime,'2010@02@28')Y
[Datetime] = CONVERT(smalldatetime,'2010@02@28')Y

SELECT [Datetime] = CAST('Mar 15, 2010' AS datetime)Y


SELECT [Datetime] = CAST('Mar 15, 2010' AS smalldatetime)Y

SELECT [Datetime] = CONVERT(datetime,'Mar 15, 2010')Y


SELECT [Datetime] = CONVERT(smalldatetime,'Mar 15, 2010')Y

SELECT [Datetime] = CAST('Mar 15, 2010 12:07:34.444' AS datetime)Y


SELECT [Datetime] = CAST('Mar 15, 2010 12:07:34.444' AS smalldatetime)Y

SELECT [Datetime] = CONVERT(datetime,'Mar 15, 2010 12:07:34.444')Y


SELECT [Datetime] = CONVERT(smalldatetime,'Mar 15, 2010 12:07:34.444')Y

SELECT [Datetime] = CAST('2010@02@28 12:07:34.444' AS datetime)Y


SELECT [Datetime] = CAST('2010@02@28 12:07:34.444' AS smalldatetime)Y

SELECT [Datetime] = CONVERT(datetime,'2010@02@28 12:07:34.444')Y


SELECT [Datetime] = CONVERT(smalldatetime,'2010@02@28 12:07:34.444')Y

@@ Double conversionY
SELECT [Datetime] = CAST(CAST(getdate() AS VARCHAR) AS datetime)Y
SELECT [Datetime] = CAST(CAST(getdate() AS VARCHAR) AS smalldatetime)Y

SELECT [Datetime] = CONVERT(datetime,convert(varchar,getdate()))Y


SELECT [Datetime] = CONVERT(smalldatetime,convert(varchar,getdate()))Y
@@@@@@@@@@@@Y

@@ MSSQL convert date string to datetime @ time is set to 00:00:00.000 or


12:00AMY
PRINT CONVERT(datetime,'07@10@2016',110)
@@ Jul 10 2016 12:00AMY
PRINT CONVERT(datetime,'2016/07/10',111)
@@ Jul 10 2016 12:00AMY
PRINT CONVERT(varchar,CONVERT(datetime,'20160710', 112),121)
Y
@@ 2016@07@10 00:00:00.000
Y
@@@@@@@@@@@@
Y

  !"#$%$&'c(c)*(+(c,!*-.
@@ Selected named date stylesY

DECLARE @DateTimeValue varchar(32)Y

@@ US@StyleY
@@ Convert string to datetime sqlY@ sql convert string to datetimeY
SELECT @DateTimeValue = '10/23/2016'Y
SELECT StringDate=@DateTimeValue,Y
[US@Style] = CONVERT(datetime, @DatetimeValue)Y

SELECT @DateTimeValue = '10/23/2016 23:01:05'Y


SELECT StringDate = @DateTimeValue,Y
[US@Style] = CONVERT(datetime, @DatetimeValue)Y

@@ UK@Style, British/FrenchY
SELECT @DateTimeValue = '23/10/16 23:01:05'Y
SELECT StringDate = @DateTimeValue,Y
[UK@Style] = CONVERT(datetime, @DatetimeValue, 3)Y

SELECT @DateTimeValue = '23/10/2016 04:01 PM'Y


SELECT StringDate = @DateTimeValue,Y
[UK@Style] = CONVERT(datetime, @DatetimeValue, 103)Y

@@ German@StyleY
SELECT @DateTimeValue = '23.10.16 23:01:05'Y
SELECT StringDate = @DateTimeValue,Y
[German@Style] = CONVERT(datetime, @DatetimeValue, 4)Y

Y
Y

SELECT @DateTimeValue = '23.10.2016 04:01 PM'Y


SELECT StringDate = @DateTimeValue,Y
[German@Style] = CONVERT(datetime, @DatetimeValue, 104)Y

Y
Y

@@ Double conversion to US@Style 107 with century: Oct 23, 2016Y


SET @DateTimeValue='10/23/16'Y
SELECT StringDate=@DateTimeValue,Y
[US@Style] = CONVERT(varchar, CONVERT(datetime, @DateTimeValue),107)Y

@@ SQL dateformat settingY


USE AdventureWorks2008;Y
SELECT convert(datetime,'14/05/08')Y
/* Msg 242, Level 16, State 3, Line 1Y
The conversion of a varchar data type
in an out@of@range value.Y
*/Y
SET DATEFORMAT ymdY
SELECT convert(datetime,'14/05/08')
@@ Setting DATEFORMAT to UK@StyleY
SET DATEFORMAT dmyY
SELECT convert(datetime,'20/05/14')
@@ Setting DATEFORMAT to US@StyleY
SET DATEFORMAT mdyY
SELECT convert(datetime,'05/20/14')
SELECT convert(datetime,'05/20/2014')
GOY

YY

@@@@@@@@@@@@Y

to a datetime data type resultedY

@@ 2014@05@08 00:00:00.000Y
@@ 2014@05@20 00:00:00.000Y
@@ 2014@05@20 00:00:00.000Y
@@ 2014@05@20 00:00:00.000Y

@@ SQL date & time eliminating dividing charactersY


@@@@@@@@@@@@Y
@@ MSSQL replace string functionY
@@ T@SQL string concatenate (+)Y
USE AdventureWorks2008;Y
SELECT replace(convert(VARCHAR(10),getdate(),102),'.','')Y
@@ 20120315Y
SELECT replace(convert(VARCHAR(10),getdate(),111),'/','')Y
@@ 20120315 Y
@@ SQL triple replaceY
SELECT replace(replace(replace(convert(VARCHAR(25),Y
getdate(),20),'@',''), ':',''),' ','')Y
@@ 20120529090427Y
@@ T@SQL concatenating from a date and a time conversionY
SELECT replace(convert(VARCHAR(10),getdate(),111),'/','') +Y
replace(convert(VARCHAR(8),getdate(),108),':','')Y
@@ 20120315085654Y

YY

@@@@@@@@@@@@Y
@@ Converting string dates from a tableY
@@@@@@@@@@@@Y

YY

@@ Create and populate a test table with a string dateY


USE tempdb;Y
SELECTY
DepartmentID,Y
LastUpdate=CONVERT(varchar,Y
dateadd(day, DepartmentID, ModifiedDate),100)Y
INTO DeptInfoY
FROM AdventureWorks.HumanResources.DepartmentY

SELECT * FROM DeptInfoY


/* Partial resultsY

DepartmentID
1
2
*/Y

LastUpdateY
Jun 2 1998 12:00AMY
Jun 3 1998 12:00AMY

@@ Convert string date column to datetimeY


SELECTY
DepartmentID,Y
LastChangeDate=convert(datetime, LastUpdate)Y
FROM DeptInfoY
/* Partial resultsY

DepartmentID
1
2
*/Y
DROP TABLE DeptInfoY
GOY

LastChangeDateY
1998@06@02 00:00:00.000Y
1998@06@03 00:00:00.000Y

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Y
@@ Casting string date & time together and separately
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Y

@@ SQL cast string to datetimeY


SELECT CAST('20100315 16:40:31' AS datetime)Y
@@ Result: 2010@03@15 16:40:31.000Y

YY

@@ SQL cast string to date @ time part 0


SELECT CAST('20100315' AS datetime)Y
@@ Result: 2010@03@15 00:00:00.000Y

YY

@@ SQL cast string to time @ date part 1900@01@01Y


SELECT CAST('16:40:31' AS smalldatetime)Y
@@ Result: 1900@01@01 16:41:00Y
@@@@@@@@@@@@Y
@@ SQL DATEDIFF with string dateY
@@@@@@@@@@@@Y
DECLARE @sDate varchar(10)Y
SET @sDate = '2010/03/15'Y
@@ DATEDIFF (delta) between two dates in monthsY
SELECT GETDATE(), DATEDIFF (MONTH, GETDATE(), @sDate)Y
SELECT GETDATE(), DATEDIFF (MONTH, GETDATE(), CAST(@sDate as datetime))Y
SELECT GETDATE(), DATEDIFF (MONTH, GETDATE(), CONVERT(datetime,@sDate))Y
SELECT GETDATE(), DATEDIFF (MONTH, GETDATE(), CONVERT(datetime,@sDate,111))Y
@@ Seme results for above: 2008@12@29 11:04:51.097
15Y

@@ SQL convert to datetime with wrong style (111 correct, 112 incorrect)Y
SELECT GETDATE(), DATEDIFF (MONTH, GETDATE(), CONVERT(datetime,@sDate,112))Y
/* ERRORY

Msg 241, Level 16, State 1, Line 11Y


Conversion failed when converting date and/or time from character string.Y
*/Y
@@@@@@@@@@@@Y
@@@@@@@@@@@@Y
@@ SQL Server date string search guidelines @ comparing datesY
@@@@@@@@@@@@Y
@@ Date equal searchY
DECLARE @Date1 datetime, @Date2 datetime, @Date3 datetimeY
SET @Date1 = '2012@01@01'Y
SET @Date2 = '2012@01@01 00:00:00.000'Y
SET @Date3 = '2012@01@01 11:00'Y

SELECT @Date1, @Date2, @Date3Y


@@ Date@only @Date1 is translated to datetimeY
@@ 2012@01@01 00:00:00.000
2012@01@01 00:00:00.000
11:00:00.000Y

2012@01@01

@@ The following is a datetime comparison, not a date@only comparisonY


IF (@Date1 = @Date2) PRINT 'EQUAL' ELSE PRINT 'NOT EQUAL'Y
@@ EQUALY

@@
IF
@@
@@
IF
@@

Equal test fails because time parts are differentY


(@Date1 = @Date3) PRINT 'EQUAL' ELSE PRINT 'NOT EQUAL'Y
NOT EQUALY
The string date implicitly converted to datetime for the equal testY
('2012@01@01' = @Date3) PRINT 'EQUAL' ELSE PRINT 'NOT EQUAL'Y
NOT EQUALY

@@ Safe way to search for a specific dateY


SELECT COUNT(*) FROM AdventureWorks.Sales.SalesOrderHeaderY
WHERE '2004/02/01' = CONVERT(varchar, OrderDate,111)Y
@@ 244Y

@@ Equivalent toY
SELECT COUNT(*) FROM AdventureWorks.Sales.SalesOrderHeaderY
WHERE OrderDate BETWEEN '2004/02/01 00:00:00.000' AND '2004/02/01
23:59:59.997'Y
@@ 244Y

Y
Y

@@ Safe way to search for a specific date rangeY


SELECT COUNT(*) FROM AdventureWorks.Sales.SalesOrderHeaderY
WHERE CONVERT(varchar, OrderDate,111) BETWEEN '2004/02/01' AND '2004/02/14'Y
@@ 1059Y

@@ Equivalent toY
SELECT COUNT(*) FROM AdventureWorks.Sales.SalesOrderHeaderY
WHERE OrderDate BETWEEN '2004/02/01 00:00:00.000' AND '2004/02/14
23:59:59.997'Y
@@ 1059Y
SELECT COUNT(*) FROM AdventureWorks.Sales.SalesOrderHeaderY
WHERE OrderDate >= '2004/02/01 00:00:00.000'Y
AND OrderDate < '2004/02/15 00:00:00.000'Y
@@ 1059 Y
@@@@@@@@@@@@Y
@@@@@@@@@@@@Y
@@ SQL Server convert from string to smalldatetimeY
@@@@@@@@@@@@Y
@@ T@SQL convert from format mm/dd/yyyy to smalldatetimeY
SELECT CONVERT(smalldatetime, '10/23/2016', 101)Y
@@ 2016@10@23 00:00:00Y
@@ MSSQL convert from format dd/mm/yyyy to smalldatetimeY
SELECT CONVERT(smalldatetime, '23/10/2016', 103)Y
@@ 2016@10@23 00:00:00Y
@@ Month 23 is out of rangeY
SELECT CONVERT(smalldatetime, '23/10/2016', 101)Y
/* Msg 242, Level 16, State 3, Line 1Y
The conversion of a varchar data type to a smalldatetime data type resultedY
in an out@of@range value.Y
*/Y
@@@@@@@@@@@@Y
@@ Translate/convert string/text hours and minutes to secondsY
@@@@@@@@@@@@Y
DECLARE @TimeStr varchar(16) = '20:49:30'Y
SELECT
PARSENAME(REPLACE(@TimeStr,':','.'),1)Y
+ PARSENAME(REPLACE(@TimeStr,':','.'),2) * 60Y
+ PARSENAME(REPLACE(@TimeStr,':','.'),3) * 3600Y
@@ 74970Y
@@@@@@@@@@@@Y

You might also like