DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: problem in the sequence of records in sql server

  1. #1
    Join Date
    Apr 2001
    Posts
    12

    Talking

    In EMP table when I execute the syntax
    Select * from emp where ename in('king','Allen','Blair','Tony')
    it gives the output in the same sequence of names as stored in table e.g first allen,tony,king and then blair. But I require the output in the same sequence same as in query as king,allen,blair and then tony.
    In Oracle I can do it by Decode function but in SQL server there is no DECODE function.
    Please help




  2. #2
    Join Date
    Jun 2000
    Location
    French Polynesia
    Posts
    16

    Decode function

    You could write your own DECODE function as a stored procedure in a sql/server database and then use it as with Oracle.

    By the way, would you explain how you do it with a DECODE?

    Bye, Rayd

  3. #3
    Join Date
    Aug 2000
    Posts
    462
    select . . .
    order by decode(field_to_order_by,'first_in_order',1,'second_in_order',2,3);


  4. #4
    Join Date
    Apr 2001
    Posts
    12
    I got it. I used CASE to solve this problem.

    select ename from emp where ename= CASE
    WHEN 'james' THEN '1'
    WHEN 'king' THEN '2'
    WHEN 'allen' THEN '3'
    WHEN 'clark' THEN '4'
    END in('1','2','3','4') order by CASE ename
    WHEN 'james' THEN '1'
    WHEN 'king' THEN '2'
    WHEN 'allen' THEN '3'
    WHEN 'clark' THEN '4'
    END


    Thanks for all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width