all groups > sql server reporting services > july 2005 >
You're in the

sql server reporting services

group:

SQL query help


SQL query help Nat Johnson
7/12/2005 4:35:02 PM
sql server reporting services:
Have asked previous but not helped...so thought I would give it another go.
I have a query with a number of inner joins and left outer joins. The where
clause has numerous "and" clauses and one of these I need to be where a
endate is greater than the current date less 6 months (ie anything in the
last six months).

where (a.DeletedDate IS NULL) and (stli.id = 1) and (fa.regionareaid between
1 and 9)
and (rpn.enddate > '01/01/2005') or (rpn.enddate is null)

at present as you can see I have stipulated the date as 01/01/2005 however i
need the report to run every week (schedule will be set up for running and
emailing) and I need the report to have the correct "and" in place. Any help
would be greatly appreciated. the rpn.enedate can either have a date or be
null.

Re: SQL query help Dan Christjohn
7/12/2005 5:12:03 PM
and (rpn.enddate > datediff(mm,-6,rpn.enddate))

That will subtract six months from rpn.enddate. Here is the msdn ref link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp

Good luck!

Peace,

Dan


[quoted text, click to view]

Re: SQL query help Nat Johnson
7/12/2005 5:37:02 PM
Thanks for the help. Not quite right tho. I need to return all enddates
that are within the last six months or that are null.

RUnning this returns all enddates - 6 months.
I dont want to change the value of the enddate.....just return enddates that
have occurred during the last 6 months.

ie. getdate(todays date) - 6 months


[quoted text, click to view]
Re: SQL query help GeoSynch
7/12/2005 8:55:24 PM
rpn.enddate > datediff(mm,-6,sysdate())


GeoSynch

[quoted text, click to view]

RE: SQL query help Logicalman
7/13/2005 5:56:01 PM
Nat,

The error appears to be in the where clause make-up.
The responses are correct insasmuch for getdate()-180 (that's the equivilent
to getdate()-6 months, but your problem exists with bringing back nulls also.

Your clause reads::
where (a.DeletedDate IS NULL) and (stli.id = 1) and (fa.regionareaid between
1 and 9)
and (rpn.enddate > '01/01/2005') or (rpn.enddate is null)

In this way you are counting 4 clauses OR one other.
Try adding the parems to separate the OR clause part thus (I have separated
the 4 AND clauses on different lines for clarity:

where (a.DeletedDate IS NULL)
and (stli.id = 1)
and (fa.regionareaid between 1 and 9)
and ((rpn.enddate > getdate()-180) or (rpn.enddate is null))

This will keep the clauses down to 4 (DeletedDate, id, regionareaid,
enddate) but will house the OR clause solely for the last AND clause. I often
write all AND clauses on different lines, especially when I wish to use and
OR clause inside of an AND clause.

Hope this assists.



[quoted text, click to view]
AddThis Social Bookmark Button