1- Date & Time Formatting using CONVERT
You can use the CONVERT function to cast a datetime datatype to a formatted string.
SELECT GETDATE() AS [Result] -- 2016-07-21 07:56:10.927
You can also use some built-in codes to convert into a specific format. Here are the options built into SQL Server:
DECLARE @convert_code INT = 100 -- See Table Below
SELECT CONVERT(VARCHAR(30), GETDATE(), @convert_code) AS [Result]
@convert_code Result
100 "Jul 21 2016 7:56AM"
101 "07/21/2016"
102 "2016.07.21"
103 "21/07/2016"
104 "21.07.2016"
105 "21-07-2016"
106 "21 Jul 2016"
107 "Jul 21, 2016"
108 "07:57:05"
109 "Jul 21 2016 7:57:45:707AM"
110 "07-21-2016"
111 "2016/07/21"
112 "20160721"
113 "21 Jul 2016 07:57:59:553"
114 "07:57:59:553"
120 "2016-07-21 07:57:59"
121 "2016-07-21 07:57:59.553"
126 "2016-07-21T07:58:34.340"
127 "2016-07-21T07:58:34.340"
130 "16 ???? 1437 7:58:34:340AM"
131 "16/10/1437 7:58:34:340AM"
SELECT GETDATE() AS [Result] -- 2016-07-21 07:56:10.927
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),100) AS [Result] -- Jul 21 2016 7:56AM
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),101) AS [Result] -- 07/21/2016
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),102) AS [Result] -- 2016.07.21
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),103) AS [Result] -- 21/07/2016
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),104) AS [Result] -- 21.07.2016
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),105) AS [Result] -- 21-07-2016
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),106) AS [Result] -- 21 Jul 2016
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),107) AS [Result] -- Jul 21, 2016
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),108) AS [Result] -- 07:57:05
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),109) AS [Result] -- Jul 21 2016 7:57:45:707AM
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),110) AS [Result] -- 07-21-2016
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),111) AS [Result] -- 2016/07/21
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),112) AS [Result] -- 20160721
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),113) AS [Result] -- 21 Jul 2016 07:57:59:553
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),114) AS [Result] -- 07:57:59:553
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),120) AS [Result] -- 2016-07-21 07:57:59
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),121) AS [Result] -- 2016-07-21 07:57:59.55
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),126) AS [Result] -- 2016-07-21T07:58:34.340
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),127) AS [Result] -- 2016-07-21T07:58:34.340
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),130) AS [Result] -- 16 ???? 1437 7:58:34:340AM
UNION SELECT CONVERT(VARCHAR(30),GETDATE(),131) AS [Result] -- 16/10/1437 7:58:34:340AM
2) Date & Time Formatting using FORMAT
You can utilize the new function: FORMAT().
Using this you can transform your DATETIME fields to your own custom VARCHAR format.
Example
DECLARE @Date DATETIME = '2016-09-05 00:01:02.333'
SELECT FORMAT(@Date, N'dddd, MMMM dd, yyyy hh:mm:ss tt')
Monday, September 05, 2016 12:01:02 AM
Arguments
Given the DATETIME being formatted is 2016-09-05 00:01:02.333, the following chart shows what their output
would be for the provided argument.
Argument Output
yyyy 2016
yy 16
MMMM September
MM 09
M 9
dddd Monday
ddd Mon
dd 05
d 5
HH 00
H 0
hh 12
h 12
mm 01
m 1
ss 02
s 2
tt AM
t A
fff 333
ff 33
f 3
You can also supply a single argument to the FORMAT() function to generate a pre-formatted output:
DECLARE @Date DATETIME = '2016-09-05 00:01:02.333'
SELECT FORMAT(@Date, N'U')
Monday, September 05, 2016 4:01:02 AM
Single Argument Output
D Monday, September 05, 2016
d 9/5/2016
F Monday, September 05, 2016 12:01:02 AM
f Monday, September 05, 2016 12:01 AM
G 9/5/2016 12:01:02 AM
g 9/5/2016 12:01 AM
M September 05
O 2016-09-05T00:01:02.3330000
R Mon, 05 Sep 2016 00:01:02 GMT
s 2016-09-05T00:01:02
T 12:01:02 AM
t 12:01 AM
U Monday, September 05, 2016 4:01:02 AM
u 2016-09-05 00:01:02Z
Y September, 2016
Note: The above list is using the en-US culture. A different culture can be specified for the FORMAT() via the third
parameter:
DECLARE @Date DATETIME = '2016-09-05 00:01:02.333'
SELECT FORMAT(@Date, N'U', 'zh-cn'
3) : DATEADD for adding and subtracting time periods
General syntax:
DATEADD (datepart , number , datetime_expr)
To add a time measure, the number must be positive. To subtract a time measure, the number must be negative.
Examples
DECLARE @now DATETIME2 = GETDATE();
SELECT @now; --2016-07-21 14:39:46.4170000
SELECT DATEADD(YEAR, 1, @now) --2017-07-21 14:39:46.4170000
GoalKicker.com – Microsoft® SQL Server® Notes for Professionals 32
SELECT DATEADD(QUARTER, 1, @now) --2016-10-21 14:39:46.4170000
SELECT DATEADD(WEEK, 1, @now) --2016-07-28 14:39:46.4170000
SELECT DATEADD(DAY, 1, @now) --2016-07-22 14:39:46.4170000
SELECT DATEADD(HOUR, 1, @now) --2016-07-21 15:39:46.4170000
SELECT DATEADD(MINUTE, 1, @now) --2016-07-21 14:40:46.4170000
SELECT DATEADD(SECOND, 1, @now) --2016-07-21 14:39:47.4170000
SELECT DATEADD(MILLISECOND, 1, @now)--2016-07-21 14:39:46.4180000
NOTE: DATEADD also accepts abbreviations in the datepart parameter. Use of these abbreviations is generally
discouraged as they can be confusing (m vs mi, ww vs w, etc.).