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 |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2009-02-20 : 05:33:53
|
| I've got thisSELECT forename,surname,tradingname FROM tblCustomers ORDER BY tradingname,forenameIt gives these resultsforename surname tradingnameBen Dikens NULLEmma Parker NULLNULL NULL AFD SoftwareIf its possible I want to group tradingname and forename together for the purposes of the order by - I think if it ignored nulls it would work, but how can I do that?This is the result I wantforename surname tradingnameNULL NULL AFD SoftwareBen Dikens NULLEmma Parker NULLThanks |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-20 : 05:35:47
|
| use order by forename |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2009-02-20 : 05:42:01
|
| But then the trading name values are not in order |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-20 : 05:44:28
|
| can u show me the output when u using the order by forenamer try this order by forename,tradingname |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-02-20 : 05:51:59
|
| try thisdeclare @temp table ( forename varchar(30),surname varchar(30),tradingname varchar(30))insert into @tempselect 'Ben', 'Dikens', NULL union allselect 'Emma', 'Parker', NULL union allselect NULL, NULL, 'AFD Software'select * from @temporder by case when forename is null or surname is null then 0 else 1 end |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2009-02-20 : 06:17:28
|
| This fixed itorder by case when forename is null then tradingname else forename endThank you |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-02-20 : 06:21:16
|
| Welcome... |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-20 : 06:24:44
|
| [code]order by Case when coalesce(forename,surname) is null then tradingname Else forename End[/code] |
 |
|
|
|
|
|
|
|