Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic query

Status
Not open for further replies.
Jan 21, 2002
112
GB
Sorry if this is very basic but I am not sql orientated.
I need to put a sql query together

I require to say

If year = year
and
month = month

return a value

if not return zero

I hope that makes sense to someone out there

Any pointers gratefully received
Regards
Jack
 
you don't need SQL for that

presumably what you meant to say was

if year = somevalue
and month = somevalue

where are these year and month values that you would like to test?

and which value would you like to return? would this value be stored in a row in a table? what if there is more than one row with year=year and month=month?



r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Many thanks for reply.

I was asking for a colleague who is au fait with VFP but not SQL.
The problem she was having was that the SQL DB was not handling anything else.

She was trying to get some accounts data....

She is basically trying to return some data into a dashboard report.

i.e.

If year = 2010
and
Month = June
then return data from 'balance A'
If not return nothing.

She is now off for her hols so I cant find out exactly what she wants.
Thanks for you assistance anyway.
 
try:

Code:
--examples of what would return
SELECT CASE WHEN 2010 = YEAR(GETDATE()) AND 08 = MONTH(GETDATE()) THEN 1 Else 0 END AS 'Case statement1' --returns 1 because the criteria is met
SELECT CASE WHEN 2010 = YEAR(GETDATE()) AND 07 = MONTH(GETDATE()) THEN 1 Else 0 END AS 'Case statement2' --returns 0 because the criteria is not met

--replace the 2010 and 08 with the relevant field
--replace 1 and 0 with the values you want to see returned.
--GETDATE returns the current date so you may need to change it
SELECT CASE WHEN myfield1 = YEAR(GETDATE()) AND myfield2 = MONTH(GETDATE()) THEN 1 Else 0 END AS 'Case statement3'
 
YOu do not really need a case statement just a simple select will do.

Code:
Select BalanceAmt
from
Balance table
where
Year(balancedate) = 2010
and
Month(balancedate) = 6

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top