how to set value of variable = variable + string

  • In subject line of email I want @Subject to return "'Submission at 10/03/2019 22:46:34".
    Concatenating with + and quotes isn't working. How is it done?

    DECLARE @Recipients   varchar(max)    = 'polkadot@email.com',
            @Subject   varchar(max)                   = 'Submission at ', 
            @Body    varchar(max)                      = null,
            @ReportDate          varchar(50)        = FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss'),
            @emailbody varchar(max)                = 'The attached excel contains stuff';


     EXECUTE msdb.dbo.sp_send_dbmail 
       @Recipients             = @Recipients, 
       @Subject                 = @Subject + ' ' + FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss'), 
       @Body                     = @emailbody,
       @file_attachments   = @FileShare2;

    --Quote me

  • Odd. When I tried it without any of the SendMail part, it worked:
    DECLARE @Recipients varchar(max)  = 'polkadot@email.com',
       @Subject varchar(max)       = 'Submission at ',
       @Body  varchar(max)        = null,
       @ReportDate    varchar(50)   = FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss'),
       @emailbody varchar(max)      = 'The attached excel contains stuff';

    SET @Subject = @Subject+ ' ' + FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss');
    PRINT @Subject;

    Result:
    Submission at 11/03/2019 00:58:21

  • works for me. Thank you peitlinden 🙂

    --Quote me

  • polkadot - Monday, March 11, 2019 12:19 AM

    works for me. Thank you peitlinden 🙂

    For SQL 2008 and above you can use +=
    SET @Subject += ' ' + FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss');

  • I'd recommend that you get out of the habit/crutch of using FORMAT.  Yeah... it's nice and easy but it's a bad habit until they fix the major performance issues it has.  Justifying it's use for a one-off low-row-count is a poor justification for not practicing faster methods and just leads to a "Death of a Thousand Cuts" in the database over time.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • polkadot - Sunday, March 10, 2019 11:47 PM

    In subject line of email I want @Subject to return "'Submission at 10/03/2019 22:46:34".
    Concatenating with + and quotes isn't working. How is it done?

    DECLARE @Recipients   varchar(max)    = 'polkadot@email.com',
            @Subject   varchar(max)                   = 'Submission at ', 
            @Body    varchar(max)                      = null,
            @ReportDate          varchar(50)        = FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss'),
            @emailbody varchar(max)                = 'The attached excel contains stuff';


     EXECUTE msdb.dbo.sp_send_dbmail 
       @Recipients             = @Recipients, 
       @Subject                 = @Subject + ' ' + FORMAT(GETDATE() , 'dd/MM/yyyy HH:mm:ss'), 
       @Body                     = @emailbody,
       @file_attachments   = @FileShare2;

    The issue is that you're trying to do a calculation for a parameter and pass the parameter at the same time.  You have to do the calculation first as a variable and then use the variable as a parameter.  It's just the way procs work.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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