locked
Cursors RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code and in variable month i have month stored and in year i have year stored . I want if month = 4 and year = 2018 then in a data variable its values should be stored as 01/04/2018. Then 3 months should be added to this data and saved value in another Date variable

    OPEN cursor_Employee 
    Fetch Next From cursor_Employee Into @EmployeeID
    While @@Fetch_Status = 0 Begin


    Fetch Next From cursor_Employee Into @EmployeeID
    End

    Thanks

    Friday, October 26, 2018 5:11 PM

All replies

  • User452040443 posted

    Hi,

    If you are using SQL Server 2012 or later try something this:

    declare @Month int;
    declare @Year int;
    declare @Date date;
    
    set @Month = 4;
    set @Year = 2018;
    set @Date = DateAdd(month, 1, DateFromParts(@Year, @Month, 1));
    

    Hope this help

    Friday, October 26, 2018 5:43 PM
  • User-1499457942 posted

    Hi

      I am using sql 2008

    Thanks

    Saturday, October 27, 2018 5:50 AM
  • User452040443 posted

    Hi,

    Try:

    declare @Month int;
    declare @Year int;
    declare @Date date;
    
    set @Month = 4;
    set @Year = 2018;
    set @Date = DateAdd(month, @Month + 2, DateAdd(year, @Year - 2000, '20000101'));
    
    select @Date

    Hope this help

    Saturday, October 27, 2018 2:17 PM