CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2006
    Posts
    27

    Question Help on sql query...

    Hi Friends,

    I have one query using "IN" clause for example,

    select empcode from Employee where EmpCode in ('1006','1001','1005')

    In this I am getting results in the order of EmpCode 1001,1005,1006 that is order in the database...

    but i want the result in the order of the values in IN clause... that is

    1006,1001,1005

    how to do that...

    please help me...

    thanks and Regards,
    P.Nagarajan

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Help on sql query...

    I don't know a direct way using (in)
    but this technique does the work:

    Code:
    declare @t  table  (id int identity,val int)
    insert into @t values(9)
    insert into @t values(12)
    insert into @t values(11)
    
    SELECT	 Employee.*
    FROM		 STUDENT,@t
    Where EmpCode = val
    order by id
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Jun 2006
    Posts
    437

    Re: Help on sql query...

    Hi all.

    In my opinion isn't possible to do what you want using standard SQL.
    You want to set "dinamically" an order when you extract records with the query, specifying the keys' values into the IN clause, but the only way to set the order is adding the ORDER BY clause on a field (or more fields), and you'll get the numerical or alphabetical order.

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

Featured