views:

82

answers:

4

Hi,

I have this query but for some reason the StartDate values are not getting filtered. What's wrong with it? Please let me know.

SELECT *
FROM TableA A
WHERE NOT EXISTS( SELECT * FROM TableB B
                  WHERE A.pId = B.pId and A.StartDate >= '20-JUN-10' )
+2  A: 

Did you mean this?

SELECT *
FROM TableA A
WHERE A.StartDate >= '20-JUN-10'
AND NOT EXISTS( SELECT * FROM TableB B WHERE A.pId = B.pId )

Putting the condition with StartDate into the NOT EXISTS-clause should return those rows where this criteria is not matched.

Peter Lang
thanks for the reply. It's varchar data type and I am using SQL server 2005.
nav100
@nav100: I edited your tags to reflect that. Not sure why you store a date in a `VARCHAR`-column, but did my query help?
Peter Lang
Do I have to use any convesrion?
nav100
How do I convert if I would like to get the records where StartDate >= getDate()?
nav100
Your problem is, that `'10-DEC-10' < '20-JUN-10'`, while `'30-JAN-10' > '20-JUN-10'`. Do you have any chance of storing your `StartDate`-column to a `datetime`? You could try `CAST(A.StartDate AS DATETIME)` (not sure if conversion works with that format), but with that you could not use an index on `StartDate`.
Peter Lang
This table is already in production. So I won't be able to make any changes to the database. I am trying to get the records matching >= currentdate.
nav100
+1  A: 

At a guess, you may need to revise your parenthesis:

SELECT * FROM TableA A
WHERE NOT EXISTS(SELECT * FROM TableB B WHERE A.pId = B.pId) 
 and A.StartDate >= '20-JUN-10'
Philip Kelley
A: 

use ISO formats for dates so instead of '20-JUN-10' use '20100610' assuming it is a datetime/date column

See also Setting a standard DateFormat for SQL Server

SQLMenace
+2  A: 

If StartDate is a varchar column, then you cannot expect to get correct results when doing greater than comparisons on it. In effect, you are saying that any of the values that would be stored in the StartDate column should not sort after or on '20-JUN-10'. You should make StartDate an actual DateTime. So, until you do that, you should cast it to a DateTime and since it is only referencing the outer table, you can pull it out of the subquery:

Select ..
From TableA As A
Where ( A.StartDate Is Null Or Cast(A.StartDate As DateTime) < '2010-06-20' )
    And Not Exists  (
                    Select 1
                    From TableB As B
                    Where B.pid = A.pid
                    )

That StartDate is not an actual DateTime is a fundamental problem of data integrity and creates this problem along with a host of others I would imagine. However, if for some insane reason you have values that cannot be cast to a DateTime in your StartDate column, then you need to add yet another check (and slap the original DBA upside the head):

Select ..
From TableA As A
Where ( A.StartDate Is Null 
    Or (IsDate(A.StartDate) = 1 And Cast(A.StartDate As DateTime) < '2010-06-20' ) )
    And Not Exists  (
                    Select 1
                    From TableB As B
                    Where B.pid = A.pid
                    )
Thomas
Note that this will result in problems IF A.StartDate allows nulls or has data which cannot be converted to a datetime.
Chris Lively
@Chris Lively - I've corrected the problem with Nulls. The issue with invalid casts to datetime is a bigger issue in terms of fundamental data integrity. I.e., why is a column called StartDate not typed as DateTime?
Thomas
Thanks for your help.
nav100
@Thomas: looks good. I've seen other systems where dates where placed in varchar fields. Usually it's because the data is not formatted correctly, isn't always available, or there is something just wrong with the input so they took the easy way out. If that's the case, then nav100 is going to run into many other issues. Regardless, your query is probably the best solution without knowing what is stored in the column.
Chris Lively
@Chris Lively- I added an IsDate check. Unfortunately, I too have seen too many systems where the designers took the easy way out.
Thomas
@Thomas: If I could upvote you again I would ;)
Chris Lively