本文介绍了TSQL - 比较格式化为字符串的日期(年和日历周)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来比较以下字符串格式的日期:'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

这篇关于TSQL - 比较格式化为字符串的日期(年和日历周)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 13:06