473,385 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

CASE Help

I am trying to put a case statement into my Having Clause or Where Clause. Both come up with the same error "Error near '='" in the THEN statement. I'm not sure why this is not working. The idea of this statement is to look at the date and if it is the first of the month it will return last months data, if not it gives me this months data.
If someone could give me and idea as to why this doesn't work or another solution that would be great. Thanks

SELECT TOP 100 PERCENT database.Point_time
FROM database
WHERE CASE WHEN datepart(dd, getdate())=1
THEN(MONTH(database.Point_time) = MONTH(getdate())-1) AND
(YEAR(database.Point_time) = YEAR(getdate()))
ELSE (MONTH(database.Point_time) = MONTH(getdate())) AND
(YEAR(database.Point_time) = YEAR(getdate()))
END
GROUP BY database.Point_time
ORDER BY database.Point_time.
Oct 31 '06 #1
5 3197
scripto
143 100+
SELECT TOP 100 PERCENT
CASE
WHEN datepart(dd, getdate()) = 1 THEN MONTH(databasex.Point_time)-1
ELSE MONTH(databasex.Point_time)
END, (YEAR(databasex.Point_time))

FROM databasex

GROUP BY databasex.Point_time
ORDER BY databasex.Point_time
Oct 31 '06 #2
That didn't work. It gave me one column with 1 thru 6 multiple times and a second column of just 2006.

This problem has to do with the statement in the THEN part of the CASE statement. SQL doesn't like the = sign. If there is a way to do it without the = sign I think that would work, but don't know how....
Nov 1 '06 #3
scripto
143 100+
well you can't put the case statment in the WHERE clause - it has to go in the SELECT stmnt, so this way may give you what you want.

if datepart(dd, getdate()) = 1
select TOP 100 PERCENT databasex.Point_time where MONTH(databasex.Point_time) = MONTH(getdate())-1
and YEAR(databasex.Point_time) = YEAR(getdate())
GROUP BY databasex.Point_time
ORDER BY databasex.Point_time

else
select TOP 100 PERCENT databasex.Point_time where MONTH(databasex.Point_time) = MONTH(getdate())
and YEAR(databasex.Point_time) = YEAR(getdate())
GROUP BY databasex.Point_time
ORDER BY databasex.Point_time
Nov 1 '06 #4
That is infact wrong!

you can put a case statement in the where clause

SELECT TOP 100 PERCENT database.Point_time
FROM database
WHERE database.Point_time =

CASE WHEN datepart(dd, getdate())=1
THEN(MONTH(getdate())-1) AND
(YEAR(getdate()))

CASE ELSE (MONTH(getdate())) AND
(YEAR(getdate()))
END
GROUP BY database.Point_time
ORDER BY database.Point_time
Nov 3 '06 #5
Sorry I made a mistake by accident in the previouse reply, always check your work. This is the correct way
SELECT TOP 100 PERCENT database.Point_time
FROM database
WHERE database.Point_time =

CASE
WHEN datepart(dd, getdate())=1
THEN(MONTH(getdate())-1) AND
(YEAR(getdate()))
WHEN (MONTH(getdate())) AND
(YEAR(getdate()))
END

GROUP BY database.Point_time
ORDER BY database.Point_time
Nov 3 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Joey Martin | last post by:
I am scanning an HTML file. I need to gather certain data from areas that start with <SMALL> text. Let me show the code, then explain more. ---- Set fso =...
5
by: Ryan | last post by:
I'm struggling with a Case statement. The problem I has is with doing >= I can use any value in there, but need to check if it's greater or equal to 1. I'm sure I'm missing something but can't...
2
by: Tim Graichen | last post by:
Could somebody please help me out with this easy one? I have a five button toggle set up. Case 1-4 work fine, so I have ommitted the code from this post. I need help with Case 5. Case five...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
1
by: esil | last post by:
Can anybody help me with this query? All other fields are correct except UsageStock field... select Sum(Case when ='7' then (++) else 0 end) AS ProjUsageClear, Sum(Case when ='7' then else 0...
5
by: bob | last post by:
Now this ought to be a simple matter. But nothing's simple in the Net world, I'm finding. In vb6 you could use "!" to force text to upper case in the format function. I've searched the vb.net...
5
by: Frederick Dean | last post by:
Hi,guys! I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I meet a weird case: bool Postorder::next() { switch (pc) case START: while (true) if (!child()) { pc = LEAF; return...
7
by: Adam | last post by:
Hello. I do most of my web dev work from a Mac (whose file system is case-insensitive.) I upload my web pages and other files onto a Unix-based host, which is case sensitive. When I look at the...
3
by: emalcolm_FLA | last post by:
Hello and Thanks in advance for any help. I have been tasked with rewriting a christmas assistance database using Access 2003. The old system used pre-assigned case numbers to identify...
2
by: Desitech | last post by:
I have a table entitled "Parts". In that table I have a field entitled "PartDoc" and four fields entitled "Product1", Product2", "Product3", and Product4". The datatype for these fields is Number...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.