Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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')
PostingDate >= '03/01/2005' and PostingDate < '03/16/2005'

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

Help would be greatly appreciated...
Reply With Quote
  #2 (permalink)  
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

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.

Reply With Quote
  #3 (permalink)  
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

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:
WHERE PostingDate >
        CASE WHEN <somecondition> THEN <somedatevalue>
             WHEN <someothercondition> THEN <someotherdatevalue>
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.
Reply With Quote
  #4 (permalink)  
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

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))
        DATEADD(dd, DATEDIFF(dd,0, @startDate), 0)
    and PostingDate < DATEADD(dd, DATEDIFF(dd,0,@endDate), 1)
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 03:29 PM.

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