July 22, 2013 at 9:46 am
Hi geniuses,
I need to order some data and when Value = Null -> 'Not Defined'.
Next I need the Value 'Not Defined' to be odered in the first row.
INSERT INTO #mytable
(ID, Value)
SELECT '4','First' UNION ALL
SELECT '37','Second'UNION ALL
SELECT '44','Second' UNION ALL
SELECT '54','Third' UNION ALL
SELECT '55','' UNION ALL
SELECT '81','Fifth' UNION ALL
SELECT '86','' UNION ALL
SELECT '96',''
I tried something like:
Select ISNULL(Value, 'Not Defined') as Value
from #mytable
UNION
Select Value AS Value
FROM #mytable
Order by Value
Thanks in advance
July 22, 2013 at 9:49 am
Select ISNULL(Value, 'Not Defined') as Value
from #mytable
UNION
Select Value AS Value
FROM #mytable
Order by Value
How about
SELECT ISNULL(Value, 'Not Defined') AS Value, 0 As SortOrder'
FROM #mytable
UNION
SELECT Value, 1
FROM #mytable
ORDER BY Value, SortOrder
July 22, 2013 at 9:56 am
Not working buddy
July 22, 2013 at 9:57 am
davdam8 (7/22/2013)
Not working buddy
In your sample data you don't have any NULL values. They are all empty strings.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 22, 2013 at 9:59 am
U right
->
INSERT INTO #mytable
(ID, Value)
SELECT '4','First' UNION ALL
SELECT '37','Second'UNION ALL
SELECT '44','Second' UNION ALL
SELECT '54','Third' UNION ALL
SELECT '55','NULL' UNION ALL
SELECT '81','Fifth' UNION ALL
SELECT '86','NULL' UNION ALL
SELECT '96','NULL'
July 22, 2013 at 10:03 am
This returns the rows in the order you want according to your sample data.
drop table #mytable
create table #mytable
(
ID int,
Value varchar(20)
)
INSERT INTO #mytable
(ID, Value)
SELECT 4,'First' UNION ALL
SELECT 37,'Second'UNION ALL
SELECT 44,'Second' UNION ALL
SELECT 54,'Third' UNION ALL
SELECT 55,'' UNION ALL
SELECT 81,'Fifth' UNION ALL
SELECT 86,'' UNION ALL
SELECT 96,''
select ID, isnull(nullif(Value, ''), 'UnDefined')
from #mytable
order by case when nullif(Value, '') IS null then 0 else 1 end, ID
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 22, 2013 at 10:04 am
davdam8 (7/22/2013)
U right->
INSERT INTO #mytable
(ID, Value)
SELECT '4','First' UNION ALL
SELECT '37','Second'UNION ALL
SELECT '44','Second' UNION ALL
SELECT '54','Third' UNION ALL
SELECT '55','NULL' UNION ALL
SELECT '81','Fifth' UNION ALL
SELECT '86','NULL' UNION ALL
SELECT '96','NULL'
Those are not nulls either. They are string literals. 😛
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 22, 2013 at 10:06 am
You still don't have NULL values on your sample data. However, check what I did with the alias for your column.
Select ISNULL(Value, 'Not Defined') as newValue
from #mytable
ORDER BY value
The problem is that SQL Server will try to order by the alias and not by the table's column.
July 22, 2013 at 10:11 am
Show Offfff... 🙂
I get the idea but it gives me:
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator
July 22, 2013 at 10:21 am
davdam8 (7/22/2013)
Show Offfff... 🙂I get the idea but it gives me:
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator
Why do you need a UNION? In your example there is no need for a UNION. Try running the example I posted. It does not need a UNION and it returns the rows in the order you said you wanted them.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 22, 2013 at 10:27 am
U right man!
Thanks
baby steps, baby steps
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply