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
 Sorting Nulls in SQL

Author  Topic 

jauner
Starting Member

19 Posts

Posted - 2010-02-15 : 12:57:53
I have a sql query that I need to have it sort ascending on one field but I need to have the null values appear last. Is there a way to do that. My server is collated as Latin1-General, case-insensitive, accent-sensitive. The field I need to sort ascending is item_number. But when I do the NUll values appear first. I need it in ascending order with the null values last.

My sql is below:



Select tran_date,item_number, emp_num, shift, Tran_type,
wc,dept_nbr_home
,sum(qty_complete) as qty_complete,sum(qty_scrapped) as qty_scrapped
,sum(Case When Tran_Type = 'Run' then actual_hours else 0 End) as act_hrs
,sum(Case When Tran_Type in('Indirect','Setup') then actual_hours else 0 End) as ind_hrs
,sum(plan_hours) as pln_hrs

from lc_dw_labor_transactions

where tran_date Between @PStartingTranDate and @PEndingTranDate
and emp_num between @StartEmpNum and @EndEmpNum
and dept_nbr_home between @StartDept and @EndDept
and shift = @Shift and indirect_code <> '1'

group by tran_date,item_number,emp_num,shift,Tran_type
,dept_nbr_home,indirect_code,wc

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-15 : 13:01:21
which is field you've null values?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-15 : 13:13:33
ORDER BY CASE WHEN MyColumn IS NULL THEN 2 ELSE 1 END, MyColumn, ...
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-02-15 : 13:56:35
quote:
Originally posted by visakh16

which is field you've null values?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




item_number


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

jauner
Starting Member

19 Posts

Posted - 2010-02-16 : 12:46:14
This worked great using:

Order by Case When

Thanks!!!
Go to Top of Page
   

- Advertisement -