Solved

Using function in a case statement

Posted on 2011-03-04
8
334 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
Comment Utility
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
Comment Utility
That is, assuming your conditions are correctly stated.
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
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
 
LVL 4

Expert Comment

by:Amgad_Consulting_Co
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
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
Comment Utility
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
Comment Utility
I think Cluskitt is correct in his first post...
0
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
query help 18 50
Sharepoint 3.0 migration 4 38
MS SQL 2014 get SPIDs of users 6 25
SQL Date Retrival 7 25
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 …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now