Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Using function in a case statement

Posted on 2011-03-04
Medium Priority
345 Views
This is SQL 2000

I have a function and below works. But now I want to put it in a case statement. I get an error that there's a syntax error near THEN..

--This works

Begin

print 'y'

End
else
begin
print 'n'
end
``````---How can i use it in CASE

SELECT MIN(sm.Description1) AS Item, o.order_id,
od.sku AS SKU,
SUM(original_qty) AS Need,
MAX(
CASE
WHEN o.Carrier IN ('UPSNDS', 'UPSNDA') THEN
'Y'
ELSE 'N'
END
) AS AIR,
MAX(CASE WHEN o.Carrier = 'USPSPRI' THEN 'Y' ELSE 'N' END) AS
USPS,
---**** here
<= DATEADD(dd, 0, DATEDIFF(dd, 0, getdate())) THEN 'Y' else 'N' end) as Test,

MAX(CASE WHEN o.PO_Date <= @CUTOFF THEN 'Y' ELSE 'N' END) AS Today,
MAX(rq.Wave) AS Wave

FROM .....
``````
0
Question by:Camillia

LVL 18

Accepted Solution

Cluskitt earned 2000 total points
ID: 35037651
You're doing this:

CASE WHEN (conditions THEN true ELSE false)
You need to do this:

CASE WHEN (conditions) THEN true ELSE false

So, this should work:
<= DATEADD(dd, 0, DATEDIFF(dd, 0, getdate()))) THEN 'Y' else 'N' end as Test
0

LVL 18

Expert Comment

ID: 35037656
That is, assuming your conditions are correctly stated.
0

LVL 16

Expert Comment

ID: 35037671
If you put the first "CASE" all on one line, does it work?

``````MAX(CASE WHEN o.Carrier IN ('UPSNDS', 'UPSNDA') THEN 'Y' ELSE 'N' END) AS AIR,
``````
0

LVL 4

Expert Comment

ID: 35037681
can you try this:-

<= DATEADD(dd, 0, DATEDIFF(dd, 0, getdate()))  )THEN 'Y' else 'N' end as Test
0

LVL 18

Expert Comment

ID: 35037732
I'm not sure what you're trying to say doesn't work. If it's the MAX(CASE) functions, then there's no reason not to work. Basically, if all records contain 'N' it will return 'N', otherwise it will return 'Y' (even if there's only one 'Y').
0

LVL 7

Author Comment

ID: 35037811
Actually, this worked (i removed the paranthsis before "dateadd")

OLD: I removed that paranthiss
<= DATEADD(dd, 0, DATEDIFF(dd, 0, getdate())) THEN 'Y' else 'N' end) as Test,

0

LVL 7

Author Comment

ID: 35037824
I think Cluskitt is correct in his first post...
0

LVL 32

Expert Comment

ID: 35037868
change to

``````SELECT MIN(sm.Description1) AS Item, o.order_id,
od.sku AS SKU,
SUM(original_qty) AS Need,
MAX(
CASE
WHEN o.Carrier IN ('UPSNDS', 'UPSNDA') THEN
'Y'
ELSE 'N'
END
) AS AIR,
MAX(CASE WHEN o.Carrier = 'USPSPRI' THEN 'Y' ELSE 'N' END) AS
USPS,
---**** here
when dateadd(dd,0,datediff(dd,0,dbo.fnExpectedShipDate(o.PO_Date,'FX','UPSGND','HI')))<= DATEADD(dd, 0, DATEDIFF(dd, 0, getdate())) then 'Y' else 'N' end,
MAX(CASE WHEN o.PO_Date <= @CUTOFF THEN 'Y' ELSE 'N' END) AS Today,
MAX(rq.Wave) AS Wave

FROM .....
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
###### Suggested Courses
Course of the Month11 days, left to enroll