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.
| 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.EndDateBut 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:-------------------------------------------SELECTdbo.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.PlacementIdFROM dbo.JobsFULL JOIN (SELECT * FROM dbo.Placements WHERE dbo.Placements.EndDate>GETDATE())p ONdbo.Jobs.JobId=p.JobIdFULL JOIN dbo.ContractJobs ONdbo.Jobs.JobId=dbo.ContractJobs.JobIdLEFT JOIN (SELECT * FROM dbo.JobConsultants WHERE dbo.JobConsultants.UserRelationshipId=9)c ONdbo.Jobs.JobId=c.JobIdLEFT JOIN dbo.Users Onc.UserId=dbo.users.useridWHERE p.PlacementID IS NOT NULL AND(dbo.ContractJobs.EndDate<>p.EndDate)AND (dbo.Jobs.SectorId=50OR dbo.Jobs.SectorId=51OR dbo.Jobs.SectorId=55OR 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. |
 |
|
|
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=50OR dbo.Jobs.SectorId=51OR dbo.Jobs.SectorId=55OR dbo.Jobs.SectorId=56) |
 |
|
|
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. |
 |
|
|
|
|
|
|
|