Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Comparing NULL to DATETIME

Author  Topic 

dneil
Starting Member

12 Posts

Posted - 2010-01-14 : 08:16:58
I am trying to compare two DATETIME columns in order to see instances where job end dates and corresponding placement dates do NOT match (theoretically, they should end on the same date).

However, the Job end date column contains NULL values where the end date was never entered. I want this to be included in:

WHERE dbo.ContractJobs.EndDate<>dbo.Placements.EndDate

But where the Job end date is NULL it is being ommitted. I have seen some solutions but they involve converting to varchar but I need the column to remain as datetime. I have attempted to use ISNULL:

ISNULL(dbo.ContractJobs.EndDate, 0) AS 'Job End Date',

This puts the datetime as 01/01/1900 but the row still ends up left out of the result set when the WHERE clause is introduced. Is there any way to do this? Below is my full query:

-------------------------------------------
SELECT

dbo.Jobs.JobId,
CASE
WHEN dbo.Jobs.SectorId=51 THEN 'Gov/Corp'
WHEN dbo.Jobs.SectorId=55 THEN 'Gov/Corp'
WHEN dbo.Jobs.SectorId=56 THEN 'Gov/Corp'
WHEN dbo.Jobs.SectorId=50 THEN 'Private'
ELSE '-'
END AS 'Sector',

dbo.Users.Username+ ' ' +dbo.Users.Surname AS 'Primary Consultant' ,
dbo.Jobs.JobRefNo,
dbo.ContractJobs.EndDate,
p.EndDate AS 'Placement End Date',
p.PlacementId

FROM dbo.Jobs
FULL JOIN
(SELECT * FROM dbo.Placements WHERE dbo.Placements.EndDate>GETDATE())p ON
dbo.Jobs.JobId=p.JobId
FULL JOIN dbo.ContractJobs ON
dbo.Jobs.JobId=dbo.ContractJobs.JobId
LEFT JOIN (SELECT * FROM dbo.JobConsultants WHERE dbo.JobConsultants.UserRelationshipId=9)c ON
dbo.Jobs.JobId=c.JobId
LEFT JOIN dbo.Users On
c.UserId=dbo.users.userid

WHERE p.PlacementID IS NOT NULL AND
(dbo.ContractJobs.EndDate<>p.EndDate)
AND (dbo.Jobs.SectorId=50
OR dbo.Jobs.SectorId=51
OR dbo.Jobs.SectorId=55
OR dbo.Jobs.SectorId=56)

ORDER BY dbo.Jobs.JobRefNo
-----------------------------------------


webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-14 : 08:34:05
Why do you not use your ISNULL(dbo.ContractJobs.EndDate, 0) in the WHERE clause too?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 09:48:56
make where condition like below


...
WHERE p.PlacementID IS NOT NULL AND
(dbo.ContractJobs.EndDate<>p.EndDate OR dbo.ContractJobs.EndDate IS NULL)
AND (dbo.Jobs.SectorId=50
OR dbo.Jobs.SectorId=51
OR dbo.Jobs.SectorId=55
OR dbo.Jobs.SectorId=56)
Go to Top of Page

dneil
Starting Member

12 Posts

Posted - 2010-01-15 : 04:17:32
Wow, it was that simple!

I find myself over-thinking things, looking for complex solutions when I need to just look for the obvious!

Thanks visakh16.
Go to Top of Page
   

- Advertisement -