{"id":2546,"date":"2018-06-15T16:59:45","date_gmt":"2018-06-15T23:59:45","guid":{"rendered":"https:\/\/database.guide\/?p=2546"},"modified":"2021-01-28T14:11:26","modified_gmt":"2021-01-28T04:11:26","slug":"getdate-examples-in-sql-server-t-sql","status":"publish","type":"post","link":"https:\/\/database.guide\/getdate-examples-in-sql-server-t-sql\/","title":{"rendered":"GETDATE() Examples in SQL Server (T-SQL)"},"content":{"rendered":"<p>The <code>GETDATE()<\/code> function returns the current date and time as a <strong>datetime<\/strong>\u00a0value. This value is derived from the operating system of the computer that the instance of SQL Server is running on.<\/p>\n<p>This article provides examples of the <code>GETDATE()<\/code> function, including how you can use it with other functions to return the value you&#8217;re interested in.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>First, here&#8217;s the syntax:<\/p>\n<pre>GETDATE ( )<\/pre>\n<p>So you simply call this function without any arguments.<\/p>\n<h2>Example<\/h2>\n<p>Here&#8217;s a basic example of using a <code>SELECT<\/code> statement to return the current date and time from <code>GETDATE()<\/code>:<\/p>\n<pre>SELECT GETDATE() AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+-------------------------+\r\n| Result                  |\r\n|-------------------------|\r\n| 2018-06-15 23:54:42.013 |\r\n+-------------------------+<\/pre>\n<p>So as mentioned, it returns a <strong>datetime<\/strong> value. If you want a value with more seconds fractional precision, use <a href=\"https:\/\/database.guide\/sysdatetime-examples-in-sql-server-t-sql\/\"><code>SYSDATETIME()<\/code><\/a> instead. That function returns a <strong>datetime2<\/strong> value.<\/p>\n<h2>Extract a Part of the Date<\/h2>\n<p>If you only want a part of the return value, you can use <a href=\"https:\/\/database.guide\/datepart-examples-in-sql-server\/\"><code>DATEPART()<\/code><\/a> to return only that part of the date\/time that you&#8217;re interested in.<\/p>\n<p>Example:<\/p>\n<pre>SELECT DATEPART(month, GETDATE()) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n|----------|\r\n| 6        |\r\n+----------+<\/pre>\n<p>Sometimes there&#8217;s more than one way to get the same result in SQL Server. Here&#8217;s another example using the <code>MONTH()<\/code> function:<\/p>\n<pre>SELECT MONTH(GETDATE()) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n|----------|\r\n| 6        |\r\n+----------+<\/pre>\n<p>Both of those functions return the current month. But they return them as an integer representing the month number.<\/p>\n<p>If you want the month <em>name<\/em> returned instead, you can use <a href=\"https:\/\/database.guide\/datename-examples-in-sql-server\/\"><code>DATENAME()<\/code><\/a>:<\/p>\n<pre>SELECT DATENAME(month, GETDATE()) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n|----------|\r\n| June     |\r\n+----------+\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Format the Date<\/h2>\n<p>You can also use other T-SQL functions to format the date as required.<\/p>\n<p>Here&#8217;s an example of using the <code>FORMAT()<\/code> function to format the result:<\/p>\n<pre>SELECT \r\n    FORMAT(GETDATE(), 'd', 'en-US') AS 'd, en-US',\r\n    FORMAT(GETDATE(), 'd', 'en-gb') AS 'd, en-gb',\r\n    FORMAT(GETDATE(), 'D', 'en-US') AS 'D, en-US',\r\n    FORMAT(GETDATE(), 'D', 'en-gb') AS 'D, en-gb';<\/pre>\n<p>Result:<\/p>\n<pre>+------------+------------+-----------------------+--------------+\r\n| d, en-US   | d, en-gb   | D, en-US              | D, en-gb     |\r\n|------------+------------+-----------------------+--------------|\r\n| 6\/15\/2018  | 15\/06\/2018 | Friday, June 15, 2018 | 15 June 2018 |\r\n+------------+------------+-----------------------+--------------+<\/pre>\n<p>More examples at <a href=\"https:\/\/database.guide\/how-to-format-the-date-time-in-sql-server\/\">How to Format the Date and Time in SQL Server<\/a>.<\/p>\n<h2>Incrementing the Value and Finding the Difference<\/h2>\n<p>You can use functions like <a href=\"https:\/\/database.guide\/datediff-examples-in-sql-server\/\"><code>DATEDIFF()<\/code><\/a> to return the difference between the current date and another date.<\/p>\n<p>Here&#8217;s an example of using <a href=\"https:\/\/database.guide\/dateadd-examples-in-sql-server\/\"><code>DATEADD()<\/code><\/a> to add a month to the current date, then finding out the difference in days:<\/p>\n<pre>DECLARE @date1 datetime2 = GETDATE();\r\nDECLARE @date2 datetime2 = DATEADD(month, 1, GETDATE());\r\nSELECT DATEDIFF(day, @date1, @date2) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n|----------|\r\n| 30       |\r\n+----------+<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The GETDATE() function returns the current date and time as a datetime\u00a0value. This value is derived from the operating system of the computer that the instance of SQL Server is running on. This article provides examples of the GETDATE() function, including how you can use it with other functions to return the value you&#8217;re interested &#8230; <a title=\"GETDATE() Examples in SQL Server (T-SQL)\" class=\"read-more\" href=\"https:\/\/database.guide\/getdate-examples-in-sql-server-t-sql\/\" aria-label=\"Read more about GETDATE() Examples in SQL Server (T-SQL)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5],"tags":[68,115,93,10,77,61],"class_list":["post-2546","post","type-post","status-publish","format-standard","hentry","category-sql","category-sql-server","tag-date-functions","tag-dates","tag-functions","tag-how-to","tag-mssql","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=2546"}],"version-history":[{"count":3,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2546\/revisions"}],"predecessor-version":[{"id":2555,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2546\/revisions\/2555"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}