How to Write Select Statement?

  • Hello

    My Table Structure is

    Id Sunday Monday Tuesday Wednesday Thursday Friday

    now i want to write select query which should return the records under the column. I want to check the caption of column and caption matched the it should return the records.

    Is it possible and how?

  • Please explain it little bit more.

    Shatrughna

  • In this table I am storing Quantity daywise.

    as

    Id Sunday Monday Tuesday Wednesday

    1 2.00 2.00 2.00 2.00

    now i want to get back the quantity of the day sunday.

  • Your table is badly designed. Day column should appear in rows it would make your query a lot easier.

    SELECT COL_NAME

    FROM YOUR_TABLE

    WHERE DAY=’SUNDAY’

    If you can’t change the table design, you should go with dynamic query. It would be painful exercise.

    For More: http://msdn.microsoft.com/en-us/library/ms188001.aspx

  • I got Solution

    Select

    Case when datename (dw, getdate())='Monday' then Monday else

    case when datename (dw, getdate())='Tuesday' then Tuesday else

    case when datename (dw, getdate())='Wednesday' then Wednesday else

    case when datename (dw, getdate())='Thursday' then Thursday else

    case when datename (dw, getdate())='Friday' then Friday else

    case when datename (dw, getdate())='Saturday' then Saturday else

    Sunday end end end end end end[Qty]

    from dbo.tblHawkerComplimentarySubs

    where HawkerID='15001' and ItemId='1'

  • amitsingh308 (12/31/2011)


    I got Solution

    Select

    Case when datename (dw, getdate())='Monday' then Monday else

    case when datename (dw, getdate())='Tuesday' then Tuesday else

    case when datename (dw, getdate())='Wednesday' then Wednesday else

    case when datename (dw, getdate())='Thursday' then Thursday else

    case when datename (dw, getdate())='Friday' then Friday else

    case when datename (dw, getdate())='Saturday' then Saturday else

    Sunday end end end end end end[Qty]

    from dbo.tblHawkerComplimentarySubs

    where HawkerID='15001' and ItemId='1'

    Surely you got it but to produce the final results your query needs to evaluate 7 conditions. It could be easy with table redesign.

Viewing 6 posts - 1 through 5 (of 5 total)

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