brain teaser

  • I have a brain teaser for you.

    I want to update a field(MonthEndInd) in a table(indate_count). I need to put a 'Y' where the max date by that month of the data. It is no problem when the data contains every day of the month. I could use do

    UPDATE indate_count SET MonthEndInd= 'Y' WHERE datepart(DAY,dateadd(DAY,1,in_date))=1

    BUT some months the data stops on day 27, or 30 (when there are 31 days in that month)

    I know I can read thur the data using a cursor, but I would like to be able to do this in a single update statement if possible. Any ideas?

  • You might try something like this

    update indate_count

    set MonthEndInd = 'z'

    where in_date in (select max(in_date) from indate_count

    group by datepart(yy,in_date),datepart(mm,in_date))

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • thanks

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

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