3

Hi I am Using Simple Query with IN Operator. I am using following Query

SELECT CustID  FROM Customer WHERE CustID in (6,34,5,4,3,2,1)

Now it displays Following Order

custId  
1   
2   
3   
4   
5   
6   
34

But I want the result as the same sequence which I pass in the IN Clause.

2
  • What RDBMS you are using? RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, etc... Commented Apr 15, 2013 at 6:07
  • I am Using SQL Server 2008 and i use only this simple Query . Commented Apr 15, 2013 at 6:08

6 Answers 6

3
SELECT CustID  FROM Customer WHERE CustID in (6,34,5,4,3,2,1)
ORDER BY  CASE WHEN CustId = 34
               THEN 5.5
               ELSE CustId
          END DESC
Sign up to request clarification or add additional context in comments.

2 Comments

Just be prepared for the inevitable slowdown when you try to scale your data :-) This will work fine for most databases since they're relatively small but per-row functions always have a performance impact. However, I won't downvote since it is useful for at least some cases.
Agree, a nice solution to THIS particular case. In general, I would (and have in my answer :-) ) also recommend a new order column.
2

Try this Query Ramdas......

SELECT CustID  FROM Customer WHERE CustID in (6,34,5,4,3,2,1) 
ORDER BY CHARINDEX(','+CAST(CustID as varchar(MAX))+',', ',6,34,5,4,3,2,1,')

Comments

0

Standard SQL doesn't specify default orders in either select statements or in clauses. The rows will be returned in whatever order the DBMS wants to.

If you want to force the order to something specific, you need to use an order-by clause.

Some SQL implementations may allow this to be configured, but not any of the ones I use regularly. Worst case, you can try something like:

select 1 as x, custid from customer where custid = 6
union all select 2 as x, custid from customer where custid = 34
union all select 3 as x, custid from customer where custid = 5
order by x

but it's pretty ugly and not overly efficient.

Comments

0

You can't, if you want that to happen, you will need a new field like 'showorder' or something, on which you can order your result:

SELECT CustID FROM Customer WHERE CustID IN (6,34,5,4,3,2,1) ORDER BY showorder

9 Comments

The new field that needs to be added in order to have a non-standard sort order.
Sure, this is standard SQL. You just add a new field to your table, you can name it whatever you want (I named it 'showorder' here to annotate what the field is used for). In that new field, you put the order of the records in which they should appear. Then you use ORDER BY to sort on that field in ASCending or DEScending order.
@RamdasBhosale showorder is not an SQL or SQL Server function or anything. It is an extra field that you add to the table yourself.
@Borniet I usderstand the same after not Finding this. Sorry i am little weak on SQL Serever. But for the simple Query why should i add one more column because the SQL server control is totaly depends on Client side. WE CAN not change anything in the given implementation. Also want to know about there is any other simple way to do this.
There are other ways to do this, as the other answers on your question here describe. However, none of them are easy, nor performant.
|
0

Try this one -

DECLARE @id TABLE
(
      i INT IDENTITY(1,1)
    , id INT
)

INSERT INTO @id (id)
VALUES (6),(34),(5),(4),(3),(2),(1)

SELECT c.CustID  
FROM dbo.Customer c
JOIN @id i ON i.id = c.CustID
ORDER BY i.i

Comments

0

Use a split string CTE with ordered values. Hence you will be able to sort any of dataset

DECLARE @test nvarchar(100) = '6,34,5,4,3,2,1';
;WITH cte AS
 (
  SELECT SUBSTRING(@test , 0, CHARINDEX( ',', @test)) AS val ,
         CAST(STUFF (@test + ',' , 1, CHARINDEX( ',', @test), '') AS nvarchar(100 )) AS stval,
         1 AS [level]
  UNION ALL
  SELECT SUBSTRING(stval , 0, CHARINDEX( ',', stval)),
         CAST(STUFF (stval, 1, CHARINDEX(',' , stval ), '' ) AS nvarchar(100 )),
         [level] + 1
  FROM cte
  WHERE stval != ''
  )
  SELECT c2.CustID
  FROM dbo.Customer c2 JOIN cte c ON c2.CustID = c.val
  ORDER BY c.[level]

Demo on SQLFiddle

Also you can wrap CTE in function

CREATE FUNCTION dbo. SplitStrings_CTE(@List nvarchar( 1000), @Delimiter nvarchar(1 ))
RETURNS TABLE
AS
RETURN
WITH cte AS
 (
  SELECT SUBSTRING(@List , 0 , CHARINDEX (@Delimiter, @List)) AS val ,
         CAST(STUFF (@List + @Delimiter, 1 , CHARINDEX (@Delimiter, @List), '') AS nvarchar (1000)) AS stval, 1 AS [level]
  UNION ALL
  SELECT SUBSTRING(stval , 0 , CHARINDEX (@Delimiter, stval)),
         CAST(STUFF (stval , 1, CHARINDEX( @Delimiter , stval ), '' ) AS nvarchar(1000 )), [level] + 1
  FROM cte
  WHERE stval != ''
  )
  SELECT REPLACE(val , ' ' , '' ) AS val, [level]
  FROM cte

SELECT statement with using function

DECLARE @test nvarchar(100) = '6,34,5,4,3,2,1';
SELECT CustID
FROM dbo.Customer c2 JOIN (SELECT val, [level] FROM dbo.SplitStrings_CTE(@test, ',')
                           ) c ON c2.CustID = c.val
ORDER BY c.[level]

OR

DECLARE @test nvarchar(100) = '6,34,5,4,3,2,1';
SELECT CustID
FROM dbo.Customer c2 CROSS APPLY dbo.SplitStrings_CTE(@test, ',') c                           
WHERE c2.CustID = c.val
ORDER BY c.[level]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.