Oracle到SQL Server的转换

时间:2010-11-05 11:07:35

标签: sql-server-2008

我在oracle中有以下查询,所以不知道如何转换为SQL Server。

WITH    start_date      AS
(
        SELECT  TO_DATE ( '01-Jan-2010'
                        , 'DD-Mon-YYYY'
                        )       AS start_date
        FROM    dual
)
SELECT    m.task_name
,         COUNT (CASE WHEN TO_CHAR (d.task_date, 'DD') = '01' THEN 1 END)       AS Day_1
,         COUNT (CASE WHEN TO_CHAR (d.task_date, 'DD') = '02' THEN 1 END)       AS Day_2
,         COUNT (CASE WHEN TO_CHAR (d.task_date, 'DD') = '03' THEN 1 END)       AS Day_3
...
,         COUNT (CASE WHEN TO_CHAR (d.task_date, 'DD') = '31' THEN 1 END)       AS Day_31
FROM      task_master   m
JOIN      task_detail   d       ON      m.task_id       = d.task_id
JOIN      start_date    s       ON      d.task_date     >=            s.start_date
                                AND     d.task_date     < ADD_MONTHS (s.start_date, 1)
GROUP BY  m.task_name
;

1 个答案:

答案 0 :(得分:1)

看看使用

DATEPART检查月中的某一天(使用d或dd)

CONVERT(DATETIME,'01-Jan-2010')

表示日期

DATEADD添加日期使用DATEADD (datepart ,number,date )