Home Forums SQL Server 2008 T-SQL (SS2K8) how to select first column with a specified value in a day range RE: how to select first column with a specified value in a day range

  • If you want to create the table with a column named as the first occurrence, you need to use dynamic sql. That's just wanting problems, IMHO.

    Otherwise, you could do this:

    IF OBJECT_ID(N'tempdb..#updatecontractdetailtable') IS NOT NULL

    DROP table #updatecontractdetailtable;

    SELECT programID,

    Stationid,

    CASE WHEN Monday = 1 THEN 'Monday'

    WHEN Tuesday = 1 THEN 'Tuesday'

    WHEN Wednesday = 1 THEN 'Wednesday'

    WHEN Thursday = 1 THEN 'Thursday'

    WHEN friday = 1 THEN 'Friday'

    WHEN saturday = 1 THEN 'Saturday'

    WHEN sunday = 1 THEN 'Sunday'

    END AS FirstDay,

    StartTime,

    contractheaderid,

    DelaySeconds

    into #updatecontractdetailtable

    from ContractDetail

    ORDER BY programid,Stationid;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2