Solved

Using function in a case statement

Posted on 2011-03-04
8
336 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
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
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.

 
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:ewangoya
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the proper way to use for criteria in left join? 7 26
VBScript Write Column Headers 3 38
sql select record as one long string 21 24
Tsql query 6 22
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

777 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