Solved

Using function in a case statement

Posted on 2011-03-04
8
338 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
Independent Software Vendors: 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

730 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