Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using CASE

Posted on 1998-08-01
2
Medium Priority
?
231 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
ID: 1089357
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 120 total points
ID: 1089358

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

886 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