none
Select Statement inside CASE statement using SQL Server 2008 R2

    Question

  • Hi,

     

    can you give me an example for: Select statement inside CASE statement in SQL?


    Paandey
    Tuesday, September 20, 2011 6:55 AM

All replies

  • CASE

    WHEN substring (MNEM, 1, 3) = 'GD:' THEN (select OperatingCountry from ExchangeList where EXCHANGESOURCECODE= 'GXX'

    )

     

    ELSE (select top 1 OperatingCountry from ExchangeList where EXCHANGESOURCECODE=EXMNEM) END AS OperatingCountry,
    Tuesday, September 20, 2011 7:06 AM
  • Basically, i want to insert the vaalues into a table  and the values changes based on the CASEE conditon(s).

     

    INSERT INTO TABLE1(COLUMN1,COLUMN2,COLUMN3)

    SELECT  'REFDATA' AS COLUMN1,

    CASE

      WHEN (SELECT FIELD1 FROM TABLE2)=123 THEN

           <HERE I WANT OTHER 2 COLUMNS DATA>

     

    Any help will be appreciated.

    Paanddey


    Paandey
    Tuesday, September 20, 2011 7:22 AM
  • WHEN table2.FIELD1 =123 THEN

     

    <Have your other 2 columns data>

    Tuesday, September 20, 2011 8:48 AM
  • As , (comma) does not support inside CASE,i achieved this like:

     

    INSERT INTO TABLE1(COLUMN1,COLUMN2,COLUMN3)

    SELECT 'REFDATA' AS COLUMN1,

    CASE

    WHEN (SELECT FIELD1 FROM TABLE2)=123 THEN

           (< TABLE1.COLUMN2 DATA>)

    END

    ,

    CASE

       WHEN (SELECT FIELD1 FROM TABLE2)=123 THEN

            (<TABLE1.COLUMN3 DATA>)

    END

    FROM TABLE3

     

    Thanks

     


    Paandey
    Tuesday, September 20, 2011 11:40 AM