Reputation: 23
This Query runs fine when I do not have the ">" symbol in the WHEN clause however I need this to work when the ProposalDate is any date later than '2017-04-10 00:00:00.000'. Not sure why I am getting a Syntax error when I add the > into the WHEN clause.
SELECT *,
CASE ProposalDate WHEN > '2017-04-10 00:00:00.000'
THEN (OrderTotal - ProposalWholesalePrice) +
(ProposalWholesalePrice * 1.02)
ELSE OrderTotal
END "New_OrderTotal"
FROM webservices.dbo.DT_Open_Orders_Baseprice
WHERE ProposalSerialNumber = '639036'
Upvotes: 2
Views: 38
Reputation: 311893
The shorthand case
syntax doesn't use any operators, just a series of values which are evaluated with the =
condition. If you want to use other operators, you have to use the full case
syntax:
SELECT *,
CASE WHEN ProposalDate > '2017-04-10 00:00:00.000'
THEN (OrderTotal - ProposalWholesalePrice) +
(ProposalWholesalePrice * 1.02)
ELSE OrderTotal
END "New_OrderTotal"
FROM webservices.dbo.DT_Open_Orders_Baseprice
WHERE ProposalSerialNumber = '639036'
Upvotes: 0
Reputation: 65
Have you ever tried to put the "WHEN" before the column name? For example:
SELECT *, CASE WHEN ProposalDate > '2017-04-10 00:00:00.000'
THEN (OrderTotal - ProposalWholesalePrice) + (ProposalWholesalePrice * 1.02) ELSE OrderTotal
END "New_OrderTotal" FROM webservices.dbo.DT_Open_Orders_Baseprice WHERE ProposalSerialNumber = '639036'
Upvotes: 0
Reputation: 726849
SQL CASE
expression has two forms:
CASE
expression, andCASE
expression.Simple CASE
, i.e. CASE ProposalDate WHEN ...
cannot be used with comparison operators; you need a searched CASE
instead:
SELECT
*
, CASE
WHEN ProposalDate > '2017-04-10 00:00:00.000'
THEN (OrderTotal - ProposalWholesalePrice) + (ProposalWholesalePrice * 1.02)
ELSE OrderTotal
END "New_OrderTotal"
FROM webservices.dbo.DT_Open_Orders_Baseprice
WHERE ProposalSerialNumber = '639036'
Upvotes: 2