Please help this query

  • Hi All,

    Could you please help on the needful?

    select emp_name,empid from employee where empid in(1,2,3,4,5)

    Ans:emp_name empid --Here emp id 1,2 and 4 is present in employee table

    Sagra 1

    Harish 2

    Dev 4

    My requirement

    ----------------

    But i want the result like while executing the query select emp_name,empid from employee where empid in(1,2,3,4,5)

    emp_id Ename //Please note 3 and 5 emp id not present

    3

    5

    Kindly please help how to write query?

  • Please read this --> http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

    Then come back, post some readily consumable test data, what your current output looks like from the sample data and what your requirement is from the sample data.

    Thanks.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Just replace the IN clause with NOT IN clause.

    --------------------------------------------------------------------------------------------------
    I am just an another naive wannabe DBA trying to learn SQL Server

  • Not really clear what you require; maybe you are after something like

    select L.emp_id, IsNull(emp_name,' ')

    from (select 1 emp_id union all select 2 emp_name union all select 3 emp_name

    union all select 4 emp_name union all select 5 emp_name) L

    left join employee E

    on E.emp_id = L.emp_id

    (which looks like a pretty strange thing to want) or perhaps it's nothing at all like that.

    As skcadavre said, you need to post a clear statement of what you are trying to do with some test data and tell us what the required output is.

    Tom

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply