sql - 小时、月份等的开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1439279/
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
sql - beginning of hour, month etc
提问by Hagai L
I know how to get in SQL (SQL Server) the current date, but with the beginning of the day:
我知道如何在 SQL (SQL Server) 中获取当前日期,但在一天的开始:
select dateadd(DAY, datediff(day, 0, getdate()),0)
(result:2009-09-17 00:00:00.000)
I needto get (in SQL) the current date with the beginning of this hour. For example: 2009-09-17 17:00:00 (I don't care about the exact format)
我需要(在 SQL 中)从这一小时开始获取当前日期。例如:2009-09-17 17:00:00(我不在乎确切的格式)
and I needto get the current date but with the beginning of this month: For example: 2009-09-01 00:00:00.000 (I don't care about the exact format)
而我需要获得当前日期,但这个月的开头:例如:2009-09-01 00:00:00.000(我不关心确切的格式)
Can you help me? Thanks in advance
你能帮助我吗?提前致谢
回答by gbn
Just adapt your current start of day code!
只需调整您当前的开始代码!
All you want is start of month, start of hour. Its just the same...
你想要的只是月份的开始,小时的开始。它只是一样...
select dateadd(month, datediff(month, 0, getdate()),0)
select dateadd(hour, datediff(hour, 0, getdate()),0)
回答by Antikiller
Try this:
尝试这个:
CAST(CAST(SYSDATETIME() AS DATE) AS DATETIME)
回答by Javasick
The shortest one is: CAST(CURRENT_TIMESTAMP AS DATE)
最短的是: CAST(CURRENT_TIMESTAMP AS DATE)
回答by Adriaan Stander
Try this
尝试这个
select DATEADD(hh,17,dateadd(DAY, datediff(day, 0, getdate()),0))
SELECT CAST('01 ' + CAST(DATENAME(MM, getdate()) AS VARCHAR(15)) + CAST(DATEPART(yyyy, GETDATE()) AS VARCHAR(5)) AS DATETIME)
回答by vadru
Or you can try this
或者你可以试试这个
For day: select CONVERT( datetime, convert( varchar(8), getdate(), 112))
当天: select CONVERT( datetime, convert( varchar(8), getdate(), 112))
For month: select CONVERT( datetime, convert( varchar(6), getdate(), 112)+ '01')
月份: select CONVERT( datetime, convert( varchar(6), getdate(), 112)+ '01')
For year: select CONVERT( datetime, convert( varchar(4), getdate(), 112)+ '0101')
年份: select CONVERT( datetime, convert( varchar(4), getdate(), 112)+ '0101')