Checking for a string until if finds, then insert that into new table

  • Hi,

    am having a table with column names

    Record_id,

    Col1,

    Col2,

    Col3,

    Col4,

    Col5

    My requirement is :-

    if col1 = 'Student'

    insert record_id, col1 into new_table

    else

    check in col2 - if not found

    check in col3 - if not found

    check in col4, col5.

    ---

    Checking for a string until if finds, if it found then insert that into new table

  • Am using below query, but it is not working.

    select record_id,

    if Col1= 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col2 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col3 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col4 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col5 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    from Students

    Please Help..............

  • Skanda (10/11/2012)


    Am using below query, but it is not working.

    select record_id,

    if Col1= 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col2 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col3 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col4 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    else if Col5 = 'Student'

    Begin

    Insert record_id, Student as Stud into new_table

    end

    from Students

    Please Help..............

    You really should read your course notes, this is very basic TSQL.

    Start with the SELECT part. Write a SELECT statement which will return rows where any of the columns contains the word 'Student'.

    When you are satisfied that the SELECT is working correctly, use it as the source for INSERT ... SELECT.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

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

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