Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how can I convert my if statement in SQL Server using case statment?

SQL
'check nvoice date and due date
IF table1.invoiceDate <= table2.dueDate then
'delivered'
end if

if table1.invoiceDate >= table2.dueDate then
'Late Delivery'
end if

IF table2.dueDate <= CONVERT(VARCHAR,GETDATE(),110) then
'on due'
end if


please anyone help?
Posted

 
Share this answer
 
v2
Comments
Cuculala 15-Apr-14 3:18am    
Thanks Peter Leow, Is that possible if my case is the column name of my table 2? for example;
CASE [Due Date]
WHEN CONVERT(VARCHAR,[Invoice Date],110) >= CONVERT(VARCHAR,[Due Date],110) THEN 'On time delivery'
Peter Leow 15-Apr-14 3:53am    
In your example, you do not have to place the column name after CASE, just
CASE WHEN CONVERT(VARCHAR,[Invoice Date],110) >= CONVERT(VARCHAR,[Due Date],110) THEN 'On time delivery'
I have provided a new link in the solution which is more applicable to your case.
This might helps you...
SQL
case
    when table1.invoiceDate <= table2.dueDate Then 'Delivered'
    else when table1.invoiceDate >= table2.dueDate Then 'Late Delivery'
    else when table2.dueDate <= CONVERT(VARCHAR,GETDATE(),110) Then 'on due'
 End
 
Share this answer
 
Comments
Cuculala 15-Apr-14 4:28am    
i tried your codes and i got this exception ;
Incorrect syntax near the keyword 'when'.

SELECT [Supplier],[tbl_purchaseOrders].[Purchase Order No],[Date Ordered],[Due Date],[Terms of Payment],
case
when [Invoice Date] <= [Due date] Then 'Delivered'
else when [Invoice Date] >= [Due Date] Then 'Late Delivery'
else when [Due Date] <= CONVERT(VARCHAR,GETDATE(),110) Then 'on due'
end as [Status]
from tbl_invoice,tbl_purchaseOrders
Kabi Pr. Sahoo 15-Apr-14 6:40am    
This is your query...Just check it..
SELECT upplier,tbl_purchaseOrders.Purchase Order No,Date Ordered,
Due Date,Terms of Payment,
case when Invoice Date <= Due date
then 'Delivered'
else
case when Invoice Date >= Due Date
then 'Late Delivery'
else
case when Due Date <= CONVERT(VARCHAR,GETDATE(),110)
then 'on due'
end
end
end
as Status from tbl_invoice,tbl_purchaseOrders
Cuculala 16-Apr-14 0:57am    
that's a part of my sub query please ignore select as

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900