工作日的Datepart案例

时间:2014-04-14 13:18:42

标签: sql-server

以下代码有什么问题?

我正在尝试写一个案例陈述,在今天的星期一返回日期,如果今天的日期在星期一,则从表格中取出最大日期

谢谢

SELECT 
CASE [EffectiveDate]
When datepart(weekday,getdate()) = 1  Then cast(getdate() as date)
Else max ([EffectiveDate])
End,
FROM
[ARCDAL01PR].[ArcTimeSeries].[arc_ts_data].[PriceCurveData]
WHERE
[CurveName] = 'G_H_TTF.EUR'

1 个答案:

答案 0 :(得分:0)

SELECT 
CASE [EffectiveDate]
When datepart(weekday,getdate()) = 2 Then cast(getdate() as date)
Else max ([EffectiveDate])
End,
FROM
[ARCDAL01PR].[ArcTimeSeries].[arc_ts_data].[PriceCurveData]
WHERE
[CurveName] = 'G_H_TTF.EUR
星期一应该是一周的第二天。最终,如果使用SQL Server 2012

,您可以尝试这样做
SELECT 
CASE [EffectiveDate]
When weekday(getdate(),2) = 1  Then cast(getdate() as date)
Else max ([EffectiveDate])
End,
FROM
[ARCDAL01PR].[ArcTimeSeries].[arc_ts_data].[PriceCurveData]
WHERE
[CurveName] = 'G_H_TTF.EUR

更新

SELECT 
CASE
When datepart(weekday,getdate()) = 2 Then cast(getdate() as date)
Else max ([EffectiveDate])
End as [EffectiveDate]
FROM
[ARCDAL01PR].[ArcTimeSeries].[arc_ts_data].[PriceCurveData]
WHERE
[CurveName] = 'G_H_TTF.EUR'