使用case
聲明的方式:
Select (CASE WHEN day(@today) <= 10 THEN 'First '
WHEN day(@today) >= 20 THEN 'Last '
ELSE ''
END) + CONVERT(VARCHAR(5), datepart(DW, @Today)-1) + ' DAYS of ' +
LEFT(DATENAME(month, @Today), 3) as Comments
編輯:
哦,現在我看到原來的查詢是不正確的。所以,你要更多的東西是這樣的:
Select (CASE WHEN day(@today) <= 10 THEN 'First ' + DATENAME(day, @today) + ' DAYS of ' + LEFT(DATENAME(month, @Today), 3)
WHEN day(@today) >= 20 AND MONTH(@Today) IN (1, 3, 5, 7, 8, 10, 12) THEN 'Last ' + CAST(31 - day(@today) as varchar(255))
WHEN day(@today) >= 20 AND MONTH(@Today) IN (4, 6, 9, 11) THEN 'Last ' + CAST(30 - day(@today) as varchar(255))
WHEN day(@today) >= 20 AND MONTH(@Today) IN (2) AND YEAR(@Today) % 4 = 0 THEN 'Last ' + CAST(29 - day(@today) as varchar(255))
WHEN day(@today) >= 20 AND MONTH(@Today) IN (2) AND YEAR(@Today) % 4 <> 0 THEN 'Last ' + CAST(29 - day(@today) as varchar(255))
ELSE CAST(day(@today) as varchar(255))
END) + ' DAYS of ' + LEFT(DATENAME(month, @Today), 3) as Comments
如果我給2016年11月24日就應該給予持續4天的十一月,但它給十一月 – mohan111
的第4天的時候,我給今天的日期,或11至20日期它給錯誤的結果一樣,如果我給予12/11/2016它給了11月的126天 – mohan111