有人能解释一下这个问题吗?
SELECT CASE
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Saturday'
THEN
REPLACE('RXRatingsTrialV3DailyChange' + CONVERT(VARCHAR(10), DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)+2,111),'/','')
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Sunday'
THEN
REPLACE('RXRatingsTrialV3DailyChange' + CONVERT(VARCHAR(10), DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)+1,111),'/','')
ELSE
REPLACE('RXRatingsTrialV3DailyChange' + CONVERT(VARCHAR(10), DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0),111),'/','')
END
AS RXRatingsTrialV3DailyChange
结果是RXRatingsTrialV3DailyChange20150501。我真正不明白的是查询的哪一部分是写日期,' 20150501'?
由于
答案 0 :(得分:0)
带有0的DATEADD正在这样做。
SELECT CASE
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Saturday'
THEN
REPLACE('RXRatingsTrialV3DailyChange' + CONVERT(VARCHAR(10), DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)+2,111),'/','')
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Sunday'
THEN
REPLACE('RXRatingsTrialV3DailyChange' + CONVERT(VARCHAR(10), DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)+1,111),'/','')
ELSE
REPLACE('RXRatingsTrialV3DailyChange' + CONVERT(VARCHAR(10), DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0),111),'/','')
END
AS RXRatingsTrialV3DailyChange,
getdate()m,
DATEDIFF(mm,0,GETDATE()),
DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)