{"id":390,"date":"2010-09-24T20:24:19","date_gmt":"2010-09-25T01:24:19","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=390"},"modified":"2021-03-09T11:38:31","modified_gmt":"2021-03-09T16:38:31","slug":"sql-server-date-datepart-formats","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/ms-sql-server\/sql-server-date-datepart-formats\/","title":{"rendered":"SQL Server Date &#038; Datepart formats"},"content":{"rendered":"<p>Here is a handy cheat sheet for dates in SQL Server:<\/p>\n<pre name=\"code\" class=\"SQL\">-- DATEPART SAMPLES\nselect &#039;Year: &#039; + CONVERT(NVARCHAR(4),datepart(year, GETDATE()))\nselect &#039;Quarter: &#039; + CONVERT(CHAR(1),datepart(quarter , GETDATE()))\nselect &#039;Month: &#039; + CONVERT(NVARCHAR(2),datepart(month, GETDATE()))\nselect &#039;Day of Year: &#039; + CONVERT(NVARCHAR(3),datepart(dayofyear, GETDATE()))\nselect &#039;Day of Month: &#039; + CONVERT(NVARCHAR(2),datepart(day, GETDATE()))\nselect &#039;Week: &#039; + CONVERT(NVARCHAR(2),datepart(week, GETDATE()))\nselect &#039;Weekday: &#039; + CONVERT(CHAR(1),datepart(weekday, GETDATE()))\nselect &#039;Hour: &#039; + CONVERT(NVARCHAR(2),datepart(hour, GETDATE()))\nselect &#039;Minute: &#039; + CONVERT(NVARCHAR(2),datepart(minute, GETDATE()))\nselect &#039;Second: &#039; + CONVERT(NVARCHAR(2),datepart(second, GETDATE()))\nselect &#039;Millisecond: &#039; + CONVERT(NVARCHAR(3),datepart(millisecond, GETDATE()))\nselect &#039;Microsecond: &#039; + CONVERT(NVARCHAR(6),datepart(microsecond, GETDATE()))\nselect &#039;Nanosecond: &#039; + CONVERT(NVARCHAR(9),datepart(nanosecond, GETDATE()))\n\n-- VARIOUS DATE FORMATS \nSELECT   CONVERT(CHAR(19),  GETDATE(), 100)           -- Jun 12 2009  8:49AM     \nSELECT   CONVERT(CHAR(25),  GETDATE(), 101)           -- 06\/12\/2009              \nSELECT   CONVERT(CHAR(25),  GETDATE(), 102)           -- 2009.06.12              \nSELECT   CONVERT(CHAR(25),  GETDATE(), 103)           -- 12\/06\/2009              \nSELECT   CONVERT(CHAR(25),  GETDATE(), 107)           -- Jun 12, 2009            \nSELECT   CONVERT(CHAR(25),  GETDATE(), 108)           -- 08:51:49                \nSELECT   CONVERT(CHAR(25),  GETDATE(), 110)           -- 06-12-2009              \nSELECT   CONVERT(CHAR(25),  GETDATE(), 111)           -- 2009\/06\/12              \nSELECT   CONVERT(CHAR(25),  GETDATE(), 112)           -- 20090612                \nSELECT   CONVERT(CHAR(25),  GETDATE(), 114)           -- 08:53:06:000            \nSELECT   CONVERT(CHAR(25),  GETDATE(), 120)           -- 2009-06-12 08:55:28     \nSELECT   CONVERT(CHAR(25),  GETDATE(), 121)           -- 2010-09-24 09:16:35.520<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a handy cheat sheet for dates in SQL Server: &#8212; DATEPART SAMPLES select &#039;Year: &#039; + CONVERT(NVARCHAR(4),datepart(year, GETDATE())) select &#039;Quarter: &#039; + CONVERT(CHAR(1),datepart(quarter , GETDATE())) select &#039;Month: &#039; + CONVERT(NVARCHAR(2),datepart(month, GETDATE())) select &#039;Day of Year: &#039; + CONVERT(NVARCHAR(3),datepart(dayofyear, GETDATE())) select &#039;Day of Month: &#039; + CONVERT(NVARCHAR(2),datepart(day, GETDATE())) select &#039;Week: &#039; + CONVERT(NVARCHAR(2),datepart(week, GETDATE()))&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,10],"tags":[15],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/390"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=390"}],"version-history":[{"count":6,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/390\/revisions"}],"predecessor-version":[{"id":1831,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/390\/revisions\/1831"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}