Solved

Using CASE

Posted on 1998-08-01
2
185 Views
Last Modified: 2010-03-19
Please Help to put this SP on a road

CREATE PROCEDURE spselectLastmonthcheque1
As
Declare @bbb datetime
Declare @aaa int
Set @bbb = getdate()
Set @aaa = month(@bbb)
Set @aaa = @aaa -1
Select  * from cheque1 Where [date] between  
Case
When @aaa = 0 Then 'Jan 1 1998 12:00AM' and 'Jan 31 1998 12:00AM'
When @aaa = 1 Then 'Feb 1 1998 12:00AM' and 'FEB 29 1998 12:00AM'
WHEN @aaa = 2 Then 'Mar 1 1998 12:00AM' and 'Mar 31 1998 12:00AM'
WHEN @aaa = 3 Then 'Apr 1 1998 12:00AM' and 'Apr 31 1998 12:00AM'
WHEN @aaa = 4 Then 'May 1 1998 12:00AM' and 'May 31 1998 12:00AM'
WHEN @aaa = 5 Then 'Jun 1 1998 12:00AM' and 'Jun 31 1998 12:00AM'
WHEN @aaa = 6 Then 'Jul 1 1998 12:00AM' and 'Jul 31 1998 12:00AM'
WHEN @aaa = 7 Then 'Aug 1 1998 12:00AM' and 'Aug 31 1998 12:00AM'
WHEN @aaa = 8 Then 'Sep 1 1998 12:00AM' and 'Sep 31 1998 12:00AM'
WHEN @aaa = 9 Then 'Oct 1 1998 12:00AM' and 'Oct 31 1998 12:00AM'
WHEN @aaa = 10 Then 'Nov 1 1998 12:00AM' and 'Nov 31 1998 12:00AM'
WHEN @aaa = 11 Then 'Dec 1 1998 12:00AM' and 'Dec 31 1998 12:00AM'

Error incorrect syntah near "and"

0
Comment
Question by:mativare
2 Comments
 

Expert Comment

by:zimmy
Comment Utility
Try this:

CREATE PROCEDURE spselectLastmonthcheque1
As
Declare @bbb datetime
Declare @aaa int
Declare @startdate datetime /* new variable */
Declare @enddate datetime   /* new variable */
Set @bbb = getdate()
Set @aaa = month(@bbb)
Set @aaa = @aaa -1
Case
When @aaa = 0 Then do
  @startdate = 'Jan 1 1998 12:00AM'
  @enddate= 'Jan 31 1998 12:00AM'
  end
When @aaa = 1 Then do
  @startdate = 'Feb 1 1998 12:00AM'
  @enddate = 'FEB 29 1998 12:00AM'
  end



WHEN @aaa = 11 Then do
  @startdate = 'Dec 1 1998 12:00AM'
  @enddate = 'Dec 31 1998 12:00AM'
  end
end case  /* This syntax probably isn't right. I'm at home, */
          /* and my TSQL manuals are at work. */

Select  * from cheque1
Where [date] => @startdate
AND [date] <= @enddate
0
 
LVL 4

Accepted Solution

by:
mitek earned 30 total points
Comment Utility

 
One way to do that would be:

CREATE PROCEDURE spselectLastmonthcheque1
As
Declare @bbb datetime
Declare @aaa int
Set @bbb = getdate()
Set @aaa = month(@bbb)
Set @aaa = @aaa -1
Select * from cheque1
Where
  (@aaa = 0 And [date] between 'Jan 1 1998 12:00AM' and 'Jan 31 1998 12:00AM')
OR (@aaa = 1 And [date] between 'Feb 1 1998 12:00AM' and 'FEB 29 1998 12:00AM')
OR (@aaa = 2 And [date] between 'Mar 1 1998 12:00AM' and 'Mar 31 1998 12:00AM')
OR (@aaa = 3 And [date] between 'Apr 1 1998 12:00AM' and 'Apr 31 1998 12:00AM')
OR (@aaa = 4 And [date] between 'May 1 1998 12:00AM' and 'May 31 1998 12:00AM')
OR (@aaa = 5 And [date] between 'Jun 1 1998 12:00AM' and 'Jun 31 1998 12:00AM')
OR (@aaa = 6 And [date] between 'Jul 1 1998 12:00AM' and 'Jul 31 1998 12:00AM')
OR (@aaa = 7 And [date] between 'Aug 1 1998 12:00AM' and 'Aug 31 1998 12:00AM')
OR (@aaa = 8 And [date] between 'Sep 1 1998 12:00AM' and 'Sep 31 1998 12:00AM')
OR (@aaa = 9 And [date] between 'Oct 1 1998 12:00AM' and 'Oct 31 1998 12:00AM')
OR (@aaa = 10 And [date] between 'Nov 1 1998 12:00AM' and 'Nov 31 1998 12:00AM')
OR (@aaa = 11 And [date] between 'Dec 1 1998 12:00AM' and 'Dec 31 1998 12:00AM')  

not the best way for this particular problem -- just the first thing that came to mind

0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

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 …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

16 Experts available now in Live!

Get 1:1 Help Now