Results 1 to 2 of 2

Thread: can I reduce this sql statement syntax?

  1. #1
    Zycas Guest

    can I reduce this sql statement syntax?

    this is SQL statement in Stored procedure to calculate the last date of a
    month :
    (can it reduce to fewer lines??? help!!!!!)

    argument pass in : @empmt_dt datetime
    argument return : @last_dt datetime


    declare @empmt_dt datetime, @mth tinyint, @yr smallint
    select @empmt_dt = "12/9/2000"

    select @yr = datepart(year, @empmt_dt)
    select @mth = datepart(month, @empmt_dt)

    IF @mth = 12
    BEGIN
    SELECT @mth = 1
    SELECT @yr = @yr + 1
    END
    ELSE
    BEGIN
    SELECT @mth = @mth + 1
    END
    SELECT dateadd(dd, -1, convert(datetime, STR(@mth,2,0) + '/01/' +
    STR(@yr,4,0)))





  2. #2
    nr Guest

    can I reduce this sql statement syntax? (reply)

    declare @d datetime ,
    @e datetime
    select @d = getdate()

    /* this calculates last day of month from @d */

    select @e = dateadd(dd,-1,dateadd(mm,1,convert(datetime,'1 ' + datename(mm,@d) + ' ' + datename(yy,@d))))

    select @e



    ------------
    Zycas at 6/17/00 12:06:12 AM

    this is SQL statement in Stored procedure to calculate the last date of a
    month :
    (can it reduce to fewer lines??? help!!!!!)

    argument pass in : @empmt_dt datetime
    argument return : @last_dt datetime


    declare @empmt_dt datetime, @mth tinyint, @yr smallint
    select @empmt_dt = "12/9/2000"

    select @yr = datepart(year, @empmt_dt)
    select @mth = datepart(month, @empmt_dt)

    IF @mth = 12
    BEGIN
    SELECT @mth = 1
    SELECT @yr = @yr + 1
    END
    ELSE
    BEGIN
    SELECT @mth = @mth + 1
    END
    SELECT dateadd(dd, -1, convert(datetime, STR(@mth,2,0) + '/01/' +
    STR(@yr,4,0)))





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •