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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Funny isnull check in sql... any ideas or tricks?

Author  Topic 

Tim_Field
Yak Posting Veteran

87 Posts

Posted - 2005-07-13 : 06:14:28
My big select is below... basically I need a trick! the bit below will fail because when it finds a null, the = ssh.end_date won't match (I need an "ssh.end_date is null") or similar. How can I do this?? Full select at the bottom. I want the null records first then afterwards the highest end date. EVERYTHING ELSE WORKS!

Thanks! Tim


-- null end_date first then max history record.
inner join (
select max(isnull(end_date,'2076-06-06 00:00:00')) [end_date], person_id -- This gets the latest school history record.
from sims.stud_school_history ssh2
group by person_id
) x2
on x2.person_id = ssh.person_id
and case x2.end_date
when '2076-06-06 00:00:00'
then null
else x2.end_date
end
= ssh.end_date




update s set latest_adno = Null
from sims.stud_student s
inner join sims.stud_school_history ssh
on ssh.person_id = s.person_id
inner join sims.ra_applicant a
on s.person_id = a.person_id
inner join sims.ra_application app
on app.applicant_id = a.person_id
inner join sims.ra_application_status_history apph
on apph.application_id = app.application_id
inner join sims.ra_application_status apps
on apps.lookup_value_id = apph.application_status_id
inner join sims.ra_application_status_category appsc
on appsc.category_id = apps.category_id

inner join (select max(transition_date) [transition_date], application_id -- This gets the latest application status.
from sims.ra_application_status_history apph2
group by application_id
) x
on x.application_id = apph.application_id
and x.transition_date = apph.transition_date
-- null end_date first then max history record.
inner join (
select max(isnull(end_date,'2076-06-06 00:00:00')) [end_date], person_id -- This gets the latest school history record.
from sims.stud_school_history ssh2
group by person_id
) x2
on x2.person_id = ssh.person_id
and case x2.end_date
when '2076-06-06 00:00:00'
then null
else x2.end_date
end
= ssh.end_date

where appsc.code != 'Admitted'

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-13 : 06:23:45
Can you post some sample data and expected result?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Tim_Field
Yak Posting Veteran

87 Posts

Posted - 2005-07-13 : 06:28:18
This should show you what I'm after... I'd need the NULL record from the first table but the highest record from the second (none with a null end_date).


sims.stud_school_history
school_history_id person_id start_date end_date
1 2 01/01/2000 01/01/2004
2 2 03/01/2005 NULL -- This one required

school_history_id person_id start_date end_date
3 5 01/01/2000 01/01/2004
4 5 03/01/2005 01/01/2007 -- This one required


Go to Top of Page

Tim_Field
Yak Posting Veteran

87 Posts

Posted - 2005-07-13 : 06:29:55
The NULL part is working, it returns '2076-06-06 00:00:00' but when the case statement is used it does 'null = ssh.end_date' which won't work.

Go to Top of Page

Tim_Field
Yak Posting Veteran

87 Posts

Posted - 2005-07-13 : 06:48:00
Nope, this isn't any different... it's still doing ssh.end_date = null with that case statement.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-13 : 06:56:50
Something like
and	case
when x2.end_date ='2076-06-06 00:00:00'
then x2.end_date is null
else x2.end_date =x2.end_date
end


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Tim_Field
Yak Posting Veteran

87 Posts

Posted - 2005-07-13 : 07:08:21
Yes, this is what I'm after but the syntax is incorrect.
Go to Top of Page

Tim_Field
Yak Posting Veteran

87 Posts

Posted - 2005-07-13 : 07:22:11
This is the solution... tricky one!

and isnull(ssh.end_date, '2076-06-06 00:00:00') = x2.end_date
Go to Top of Page
   

- Advertisement -