SQL 将 getdate() 转换为 yyyymmdd 并获取两年前的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38563217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting getdate() to yyyymmdd & getting the date two years back
提问by Brandon
I have found a couple of different methods to convert it. However, I still get the yyyy-mm-dd
format or yyyy-mm-dd hh:mm:ss
. I am currently using SQL Server 2014.
我找到了几种不同的方法来转换它。但是,我仍然得到yyyy-mm-dd
格式或yyyy-mm-dd hh:mm:ss
. 我目前正在使用 SQL Server 2014。
SELECT dateadd(day, convert(int, getdate()), 112)
SELECT DATEADD(YEAR, -2, convert(DATE, GETDATE(), 112))
I am doing a date range of 2 years. Thus I need the codes to the find the date two years back.
我正在做 2 年的日期范围。因此,我需要代码来查找两年前的日期。
回答by vercelli
You can also use FORMAT:
您还可以使用格式:
select FORMAT(getdate(), 'yyyyMMdd')
回答by nilsman
Try CONVERT(char(8), GETDATE(), 112)
尝试 CONVERT(char(8), GETDATE(), 112)
Also check https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx
另请检查https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx