Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using CASE

Posted on 1998-08-01
2
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

705 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