我需要一种方法来比较以下字符串格式的日期:'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 - 比较格式化为字符串的日期(年和日历周)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
修改现有小数位信息Modify Existing decimal places info(修改现有小数位信息)
多次指定相关名称“CONVERT"The correlation name #39;CONVERT#39; is specified multiple times(多次指定相关名称“CONVERT)
T-SQL 左连接不返回空列T-SQL left join not returning null columns(T-SQL 左连接不返回空列)
从逗号或管道运算符字符串中删除重复项remove duplicates from comma or pipeline operator string(从逗号或管道运算符字符串中删除重复项)
将迭代查询更改为基于关系集的查询Change an iterative query to a relational set-based query(将迭代查询更改为基于关系集的查询)
将零连接到 sql server 选择值仍然显示 4 位而不是concatenate a zero onto sql server select value shows 4 digits still and not 5(将零连接到 sql server 选择值仍然显示 4 位而不是 5)