Solved

Using function in a case statement

Posted on 2011-03-04
8
341 Views
Last Modified: 2012-05-11
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
if  dateadd(dd,0,datediff(dd,0,dbo.fnExpectedShipDate('2011-02-18 15:45:00.000','FX','UPSGND','HI'))) <= DATEADD(dd, 0, DATEDIFF(dd, 0, getdate()))
   
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    
           case 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) as Test,

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

	FROM .....

Open in new window

0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 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:
case 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 as Test
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35037656
That is, assuming your conditions are correctly stated.
0
 
LVL 16

Expert Comment

by:sjklein42
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,

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Amgad_Consulting_Co
ID: 35037681
can you try this:-

  case 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 as Test
0
 
LVL 18

Expert Comment

by:Cluskitt
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

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

OLD: I removed that paranthiss
case 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) as Test,



0
 
LVL 7

Author Comment

by:Camillia
ID: 35037824
I think Cluskitt is correct in his first post...
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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 .....

Open in new window

0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question