Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2006, 05:27 PM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to demiwolf
Default Case in Where Clause

Unless if someone has a better suggestion? I can't seem to get the syntax right on this:

where dti.employeeName = 'Doe, John' and
Case when (DateName(weekday, '03/01/2005') != 'Sunday') then
        PostingDate > (DATEADD(dd, DATEDIFF(dd,0,'03/01/2005'), -datepart(weekday, '03/01/2005'))) --and (PostingDate < '03/16/2005')
    Else
PostingDate >= '03/01/2005' and PostingDate < '03/16/2005'
    End

It gives me the following error: Line 47: Incorrect syntax near '>'.

Help would be greatly appreciated...
-S.
 
Old March 20th, 2006, 06:37 PM
Friend of Wrox
Points: 2,101, Level: 18
Points: 2,101, Level: 18 Points: 2,101, Level: 18 Points: 2,101, Level: 18
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: , , .
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

If you're trying to find a result between two dates try using the BETWEEN function. It is very well explained in the BOL. Besides, we can't tell here what line is erroring. I don't know which line is line 47. Also, don't assume we can understand exactly what you're trying to accomplish with you query. If you do explain as much as possible, someone here might have an easier way to do it.



 
Old March 20th, 2006, 07:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

The CASE ... END construction in SQL is an expression and not a statement like in other languages.

Thus, you use CASE...END in a WHERE clause like this:
Code:
SELECT ...
WHERE PostingDate >
        CASE WHEN <somecondition> THEN <somedatevalue>
             WHEN <someothercondition> THEN <someotherdatevalue>
             ...
        END
...
I'm not sure I understand from the code you posted what you are trying to accomplish, so I can't be more specific on the exact expression you need...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old March 23rd, 2006, 12:45 PM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to demiwolf
Default

Hi guys, thanks for the help. I figured it out (as in it doesn't error anymore).

However, it's now doing something really interesting. It picks up the first case every time.

-Edit- I figured it out... wayward date calculation. For anyone needing to reference it, here's the code that worked out.

where dti.employeename = @empName and PostingDate >=
    Case when (DateName(weekday, @startDate) != 'Sunday' AND e.OfficeCode in ('S', 'K', 'L', 'R') AND e.WorkPlace = 'F') then
         DATEADD(dd, DATEDIFF(dd,0, @startDate), -datepart(weekday, @startDate))
    Else
        DATEADD(dd, DATEDIFF(dd,0, @startDate), 0)
    End
    and PostingDate < DATEADD(dd, DATEDIFF(dd,0,@endDate), 1)




Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Statement In Where Clause Logic dwj119 SQL Server 2000 4 October 27th, 2011 03:14 PM
Case in Where Clause acko SQL Server 2000 12 September 12th, 2011 01:43 PM
CASE WHEN WHERE CLAUSE deontae45 SQL Server 2000 2 January 11th, 2011 10:03 AM
case clause in query mateenmohd SQL Server 2000 2 February 22nd, 2005 05:49 AM
case clause in Access mateenmohd Access 19 May 6th, 2004 03:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.