Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have the following query when the condition becomes true then show the number of days in negative (minus sign), without converting to varchar. keeping it(days field) as int datatypeSelect Orderid, 'ORD' as Type, Orderdesc,[days] = CASE WHEN duedate < GetDate()THEN DATEDIFF(Day, duedate, GetDate())ELSE CONVERTDATEDIFF(Day, GetDate(), duedate)FROM TableOrders where AccountID = @AcctID order by days
PeterNeo
Constraint Violating Yak Guru
357 Posts
Posted - 2008-12-08 : 23:53:40
try this
SELECT Orderid , 'ORD' AS Type, Orderdesc , [days] = CASE WHEN duedate < GETDATE() THEN -1 * DATEDIFF(DAY, duedate, GETDATE()) ELSE DATEDIFF(DAY, GETDATE(), duedate) ENDFROM TableOrdersWHERE AccountID = @AcctIDORDER BY days
"There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!"
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-12-09 : 02:02:32
whats the purpose of CASE? wont datediff by itself return negative value? is just this enough?DATEDIFF(DAY, GETDATE(), duedate)