Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using function in a case statement

Posted on 2011-03-04
8
Medium Priority
?
343 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 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:
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

661 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