SQL - 获取每月/每季度的数字日

时间:2011-03-28 15:10:51

标签: sql sql-server sql-server-2005

使用SQL Server 2005:

如何在查询中获得该季度和季度的数字日期?

DECLARE @DATE DATETIME
SET @DATE = GETDATE()

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

提前致谢!

3 个答案:

答案 0 :(得分:29)

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

答案 1 :(得分:0)

至于季度的这一天,这将取消对我的进一步调查。尽管如此,我想在月份的这一天,这只是日期本身:

select DATEPART(D, @DATE)

答案 2 :(得分:0)

SELECT DATEPART(年,'12:10:3​​0.123')     ,DATEPART(月,'12:10:3​​0.123')     ,DATEPART(白天,'12:10:3​​0.123')     ,DATEPART(dayofyear,'12:10:3​​0.123')     ,DATEPART(工作日,'12:10:3​​0.123');