SQL - 获取月/季度的数字天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5461030/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:54:06  来源:igfitidea点击:

SQL - Get Numerical Day of Month/Quarter

sqlsql-serversql-server-2005

提问by Nick Vaccaro

Using SQL Server 2005:

使用 SQL Server 2005:

How can I get the numerical day of the month and day of the quarter in a query?

如何在查询中获取月份的数字日期和季度的日期?

DECLARE @DATE DATETIME
SET @DATE = GETDATE()

SELECT DATEPART(dy, @DATE) AS DayOfYear
    --, <something> AS DayOfQuarter
    --, <something> AS DayOfMonth
    , DATEPART(dw, @DATE) AS DayOfWeek

Thanks in advance!

提前致谢!

回答by Cade Roux

DECLARE @DATE DATETIME
SET @DATE = GETDATE()

SELECT DATEPART(dy, @DATE) AS DayOfYear
    , DATEDIFF(d, DATEADD(qq, DATEDIFF(qq, 0, @DATE), 0), @DATE) + 1 AS DayOfQuarter
    , DAY(@Date) AS DayOfMonth
    , DATEPART(dw, @DATE) AS DayOfWeek

回答by Will Marcouiller

As for the day of quarter, this will demande a bit of further investigate on my side. Despite, I guess that for the day of month, this would simply be the date itself:

至于季度的那一天,这需要我做一些进一步的调查。尽管如此,我想对于一个月中的某一天,这只是日期本身:

select DATEPART(D, @DATE)

回答by Roshdy Abuzeid

SELECT 
 DATEPART(year, '12:10:30.123')
,DATEPART(month, '12:10:30.123')
,DATEPART(day, '12:10:30.123')
,DATEPART(dayofyear, '12:10:30.123')
,DATEPART(weekday, '12:10:30.123');