本文介紹了TSQL - 小于或大于日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想生成一個查詢,顯示如果 startdate 小于 4 個月,則給出 5%,如果它大于 4 個月給出 10%.
I would like to produce a query that shows if startdate is less than 4 months old give 5% if its older than 4 months give 10%.
我正在使用 SQLFiddle,但沒有得到正確的輸出.目前它在所有列上給我 5% 或者如果我替換
I am using SQLFiddle and I not getting the right output. At the moment it’s giving me 5% on all the columns or if I replace
SET Discount = ''
到
SET Discount = '5%'
查詢成功但不顯示數據.
It will query successfully but does not show data.
這是我做的代碼...
Declare
startdate date,
finishdate date,
Discount varchar(4)
UPDATE Persons
SET [Discount] = ' '
SELECT *, DATEADD(MM,-4,startdate) AS TADate
FROM Persons
WHERE Discount = '5%' AND startdate < dateadd(month,-4,getdate()) OR
Discount = '10%' AND startdate < dateadd(month,4,getdate())
如果有人可以幫助我,請提前致謝.
Thanks in advance if anyone can help me.
推薦答案
在update
中使用case
語句根據date
分配折扣
Use case
statement in update
to allocate discount based on date
像這樣嘗試update
UPDATE Persons SET [Discount] = case
when startdate > dateadd(month,-4,getdate()) and startdate <= getdate() then '5%'
when startdate < dateadd(month,-4,getdate()) Then '10%'
End
SELECT * FROM Persons
這篇關于TSQL - 小于或大于日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!