Static Date but I need future month and year

  • I'm fairly new to the whole SQL world and I'm currently generating a report where i have 2 static dates but I need the furture month and year. Any help with this would be greatly appericated.

    Thanks,

    Matt

  • I am not clear on what you are trying to do as to future date. If you want a way to add to months, days or years in a date then you could use the DATEADD function. It is described in the Books Online.

    If that is not what you are trying to do, can you please post an example of the data before and after along with the goal.

    Toni

  • Declare @rundate char(8)

    set @rundate = (select dateidentifier from dbo.tbldonoterase)

    This is the date from the previous day inwhich we run reports. I'm using this as the way to get the month and year. The next step is to get it make is a future month. Example. current date is 20080128 what I need from it is 200802. Then I will need to add my static dates of the 1st and the 2nd to then end of the date string.

  • Declare @Date char(8), @CurrentMonth int, @LengthFutureMonth int, @FutureMonth char(2), @Year char(4), @FinalDate1 char(8), @FinalDate2 char(8)

    Set @Date = (select dateidentifier from tbldonoterase)

    Set @Year = Left(@Date, 4)

    Set @CurrentMonth = right(Left(@Date,6), 2)

    Set @FutureMonth = @CurrentMonth + 1

    Set @LengthFutureMonth = Len(@FutureMonth)

    If @LengthFutureMonth < 2 set @FutureMonth = '0' + @FutureMonth

    Set @FinalDate1 = @Year + '' + @FutureMonth + '' + '01'

    Set @FinalDate2 = @Year + '' + @FutureMonth + '' + '02'

    I have this so far and it seems to be working. Is there anything better??? Or does this work???

  • Hello Matthew,

    As conveyed by Toni, you can use the "DateAdd" function to return the next month same date and there is a catch for which you need to write some logic so that it checks for the leap year. Run this statement in your query analyzer

    select dateadd(m, 1, getdate())

    This will return the timestamp also and if you want to get rid of it then you need to convert this to varchar datatype.

    Hope this helps.

    Thanks


    Lucky

  • Looks like it worked thanks for all your help!!!!

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

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