问题描述
我需要一种方法来比较以下字符串格式的日期:'2015 CW 14' 或 '2016 CW 03''使用当前年和周,以测试该值是否在过去或>=当前年和周.
I need a way to compare dates in the following string format: '2015 CW 14' or '2016 CW 03'' with the current year and week in order to test if the value is in the past or >= the current year and week.
DECLARE @cw VARCHAR(50) SET @cw = '2015 CW 13' SELECT @cw; SELECT Cast(Year(Getdate()) AS VARCHAR(50)) + ' CW ' + Cast(Datepart(week, Getdate()) AS VARCHAR(50))
我相信我的方法不会让我达到目标.
I believe my approach won't get me there.
推荐答案
我认为你在 2016 CW 03 上有问题我将你的声明编辑为 @cw2,并且您可以看到 < 或 > 或 = 将用于比较.
I think you have a problem on 2016 CW 03 I edit your statement to @cw2, And you can see < or > or = will work for comparing.
这条线索很有用:
DECLARE @cw VARCHAR(50), @cw2 VARCHAR(50) SET @cw = '2015 CW 03' SET @cw2 = Cast(Year(Getdate()) AS VARCHAR(50)) + ' CW ' + RIGHT('00' + Cast(Datepart(week, Getdate()) AS VARCHAR(50)), 2) SELECT @cw, @cw2, CASE WHEN @cw = @cw2 THEN 0 WHEN @cw < @cw2 THEN -1 ELSE 1 END