我用DATEDIFF功能僅過濾上星期增加記錄:是否可以設置T-SQL DATEDIFF函數的星期開始?
DATEDIFF(week, DateCreated, GETDATE()) = 0
,我注意到什麼它假設上週日開始一週什麼。但就我而言,我寧願在週一設置星期的開始。 T-SQL有可能以某種方式嗎?
謝謝!
更新:
下面是顯示什麼DATEDIFF不檢查@@DATEFIRST變量,所以我需要另一種解決方案的例子。
SET DATEFIRST 1;
SELECT
DateCreated,
DATEDIFF(week, DateCreated, CAST('20090725' AS DATETIME)) AS D25,
DATEDIFF(week, DateCreated, CAST('20090726' AS DATETIME)) AS D26
FROM
(
SELECT CAST('20090724' AS DATETIME) AS DateCreated
UNION
SELECT CAST('20090725' AS DATETIME) AS DateCreated
) AS T
輸出:
DateCreated D25 D26
----------------------- ----------- -----------
2009-07-24 00:00:00.000 0 1
2009-07-25 00:00:00.000 0 1
(2 row(s) affected)
2009年7月26日是星期天,我想在DATEDIFF第三列返回0了。
對不起,沒有檢查DateFirst是由datediff檢查,誰會猜到,我已經更新了我的答案,以考慮到這一點。 – Tetraneutron 2009-07-09 06:03:12