﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Custom order in SELECT / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 02:05:46 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>Try it like this:[code="sql"]DECLARE @TEST1 TABLE (letter varchar(1))INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('b')INSERT INTO @TEST1(letter) VALUES('c')INSERT INTO @TEST1(letter) VALUES('d')INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('d')INSERT INTO @TEST1(letter) VALUES('d');WITH CTE AS (SELECT DISTINCT letter FROM @Test1)select letter from CTE tOUTER apply dbo.delimitedsplit8k('d;c;b;a;e',';') myfnwhere t.letter = myfn.ItemORDER BY CASE WHEN LEFT(letter,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber[/code]</description><pubDate>Mon, 04 Mar 2013 01:48:35 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>[quote][b]Sean Lange (3/1/2013)[/b][hr]Lowell, you would have to add a where clause to only return those rows that match. Notice below this will return a cartesian product.[code]select * from @Test1 tOUTER apply dbo.delimitedsplit8k('d;c;b;a;e',';') myfnORDER BY CASE WHEN LEFT(letter,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber[/code]The top 5 rows are sorted perfectly but it returned 25 rows instead of 5.Add the where clause and it works correctly.[code]select * from @Test1 tOUTER apply dbo.delimitedsplit8k('d;c;b;a;e',';') myfnwhere t.letter = myfn.ItemORDER BY CASE WHEN LEFT(letter,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber[/code][/quote]Dear Sean,The proposed command works well. However, if we have repeated value of same letter like :[code="sql"]DECLARE @TEST1 TABLE (letter varchar(1))INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('b')INSERT INTO @TEST1(letter) VALUES('c')INSERT INTO @TEST1(letter) VALUES('d')INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('d')INSERT INTO @TEST1(letter) VALUES('d')[/code]running the command [code="sql"]select letter from @Test1 tOUTER apply dbo.delimitedsplit8k('d;c;b;a;e',';') myfnwhere t.letter = myfn.ItemORDER BY CASE WHEN LEFT(letter,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber[/code]will return all rows. I tried to add SELECT DISTINCT to above command :[code="sql"]Select Distinct Letter from (select letter from @Test1 tOUTER apply dbo.delimitedsplit8k('d;c;b;a;e',';') myfnwhere t.letter = myfn.ItemORDER BY CASE WHEN LEFT(letter,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber) xx[/code] But it raise the error :[code="plain"]Msg 1033, Level 15, State 1, Line 16The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.[/code]How could I manage to avoid repeated values ?Thanks</description><pubDate>Fri, 01 Mar 2013 16:03:37 GMT</pubDate><dc:creator>RZ52</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>Lowell, you would have to add a where clause to only return those rows that match. Notice below this will return a cartesian product.[code]select * from @Test1 tOUTER apply dbo.delimitedsplit8k('d;c;b;a;e',';') myfnORDER BY CASE WHEN LEFT(letter,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber[/code]The top 5 rows are sorted perfectly but it returned 25 rows instead of 5.Add the where clause and it works correctly.[code]select * from @Test1 tOUTER apply dbo.delimitedsplit8k('d;c;b;a;e',';') myfnwhere t.letter = myfn.ItemORDER BY CASE WHEN LEFT(letter,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber[/code]</description><pubDate>Fri, 01 Mar 2013 15:25:57 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>ok i just tested this, but i made some assumptions;when you say "I THINK you are saying "If I pass this string [b]'d;c;b;a;e'[/b], i want the companies ordered in that order, instead of normal alphabetical order"If I read that right, then this works correctly. my example is using sys.objects as sample data. so you see Defaults, then check constraints, then foreign keys in that order, for example:oh yeah, like many good solutions, you need the DelimitedSplit8K function:[b][url]http://www.sqlservercentral.com/articles/Tally+Table/72993/[/url][/b][code]select * from sys.objectsOUTER apply master.dbo.delimitedsplit8k('d;c;b;a;e',';') myfnORDER BY CASE WHEN LEFT(name,1) = myfn.Item THEN 1 ELSE 2 END,myfn.ItemNumber[/code]</description><pubDate>Fri, 01 Mar 2013 15:16:47 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>To do this you will first need to parse your delimited string. The easiest way to parse a delimited string like is to use DelimitedSplit8K. You can find out about this splitter by following the article in my signature about splitting strings.In this case we need to split the string and keep them in order. Here is where we can really harness the power of the DelimitedSplit8K function. The following code should work for you.[code]DECLARE @TEST1 TABLE (letter varchar(1))INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('b')INSERT INTO @TEST1(letter) VALUES('c')INSERT INTO @TEST1(letter) VALUES('d')INSERT INTO @TEST1(letter) VALUES('e')declare @SortString varchar(10) = 'd;c;b;a;e' select * from @Test1 tjoin(	select ItemNumber, Item	from dbo.DelimitedSplit8K(@SortString, ';')) x on t.letter = x.Itemorder by x.ItemNumber[/code]Make sure you read that article and understand what it is doing.</description><pubDate>Fri, 01 Mar 2013 15:14:39 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>[quote][b]Erin Ramsay (3/1/2013)[/b][hr]If your custom phrase is static you can use:[code="sql"]select letter from @test1 order by (case letter when 'd' then 1 when 'c' then 2 when 'b' then 3 when 'a' then 4 	when 'e' then 5 end)[/code][/quote]Dear Erin,Thanks, you solved my problem.Regards.</description><pubDate>Fri, 01 Mar 2013 15:13:20 GMT</pubDate><dc:creator>RZ52</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>If your custom phrase is static you can use:[code="sql"]select letter from @test1 order by (case letter when 'd' then 1 when 'c' then 2 when 'b' then 3 when 'a' then 4 	when 'e' then 5 end)[/code]</description><pubDate>Fri, 01 Mar 2013 14:54:50 GMT</pubDate><dc:creator>Erin Ramsay</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>[quote][b]Erin Ramsay (3/1/2013)[/b][hr]Something like this maybe?[code="sql"]select letter from (select 'a' letter	unionselect 'b' letter	unionselect 'c' letter	union		select 'd' letter	union	select 'e' letter)source order by (case letter when 'd' then 1	when 'c' then 2 when 'b' then 3 when 'a' then 4 	when 'e' then 5 end)[/code][/quote]Dear Erin,Thanks for suggestion. I think I didn't explain well. what I'm looking for is to run a SELECT against a table based on custom phrase. I have a temp table named @TEST1 like :[code="sql"]DECLARE @TEST1 TABLE (letter varchar(1))INSERT INTO @TEST1(letter) VALUES('a')INSERT INTO @TEST1(letter) VALUES('b')INSERT INTO @TEST1(letter) VALUES('c')INSERT INTO @TEST1(letter) VALUES('d')INSERT INTO @TEST1(letter) VALUES('e')[/code]Now I need to "SELECT letter FROM @TEST1"  based on order defined in this phrase 'd;c;b;a;e' to have a result like :[Letters]----------dcbaeAny idea ?Thanks</description><pubDate>Fri, 01 Mar 2013 14:45:33 GMT</pubDate><dc:creator>RZ52</dc:creator></item><item><title>RE: Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>Something like this maybe?[code="sql"]select letter from (select 'a' letter	unionselect 'b' letter	unionselect 'c' letter	union		select 'd' letter	union	select 'e' letter)source order by (case letter when 'd' then 1	when 'c' then 2 when 'b' then 3 when 'a' then 4 	when 'e' then 5 end)[/code]</description><pubDate>Fri, 01 Mar 2013 14:32:07 GMT</pubDate><dc:creator>Erin Ramsay</dc:creator></item><item><title>Custom order in SELECT</title><link>http://www.sqlservercentral.com/Forums/Topic1425753-392-1.aspx</link><description>Hi,How could we change the order in SELECT command based on a custom semicolon delimited phrase.We have :[Letters]----------abcdeAnd based on this phrase 'd;c;b;a;e' we want to have :[Letters]----------dcbaeThanks in advance for helps</description><pubDate>Fri, 01 Mar 2013 14:26:58 GMT</pubDate><dc:creator>RZ52</dc:creator></item></channel></rss>