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
 SQL Server Development (2000)
 ORDER BY problems for datetime (NULL value)

Author  Topic 

duel
Starting Member

2 Posts

Posted - 2002-07-26 : 09:00:36
Im doing a .aspx page with .NET and i have my sql statement in my .vb code file so i am not using any stored procedures. I have MSSQL 2000 server.

I have to print event info and I need to order the rows so that it has one days events in order, starting from earliest starting time.

This is what is use.....
ORDER BY starting_day, DATEPART(hour, starting_time), DATEPART(minute, starting_time)

....and it works correctly, BUT! if I have same starting day for many events and some of those events have a null value in starting time, it prints that event first!

So i need order the printing so that it shows days events starting from the earlist starting time but those events that have null values in starting_time move last (in that day, not last in the entire event list)

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-07-26 : 09:04:35
quote:
i am not using any stored procedures
Why not? You should.

This should fix the Null sorting issue:

ORDER BY CASE WHEN starting_time IS NULL THEN 1 ELSE 0 END,
starting_day, DATEPART(hour, starting_time), DATEPART(minute, starting_time)


The CASE statement evaluates to a 1 if there is no start time, otherwise it's a zero, and 1 will sort lower than zero.

Any reason why you're storing start_day and start_time separately, vs. a regular datetime column?

Go to Top of Page

duel
Starting Member

2 Posts

Posted - 2002-07-26 : 09:41:06
thanks now it works correctly ;)

Go to Top of Page
   

- Advertisement -