简体   繁体   中英

Create a String With a Specific Number of Spaces in SQL Server

So I'm trying to create a stored procedure in SQL Server that sends an email that is formatted to look a certain way. I want it to look like:

##date##    ##credit##
            ##debit##

Where the debit and credit columns line up exactly. The problem is that everything I try results in:

##date##    ##credit##
        ##debit##

I have tried:

declare @lineText nvarchar(max) =  
CONCAT(CONCAT(right(replicate(' ',25) + '##date##',25), 'Debit: $##debit##')
, char(13)
, CONCAT(right(replicate(' ',25) + ' ',25), 'Credit: $##credit##'));

And:

declare @lineText nvarchar(max) =  
CONCAT(CONCAT(LEFT('##date##' + replicate(' ', 25), 25), 'Debit: $##debit##')
, char(13)
, CONCAT(LEFT('' + replicate(' ', 25), 25), 'Credit: $##credit##'));

And:

declare @lineText nvarchar(max) =  
CONCAT(CONCAT(LEFT('##date##' + space(30), 30), 'Debit: $##debit##')
, char(13)
, CONCAT(LEFT('' + space(30), 30), 'Credit: $##credit##'));

And:

declare @lineText nvarchar(max) =  
CONCAT(CONCAT(CAST('##date##' as CHAR(30)), 'Debit: $##debit##')
, char(13)
, CONCAT(CAST('' as CHAR(30)), 'Credit: $##credit##'));

Nothing makes the columns line up. How do I create a string where the credit and debit columns actual align?

I want the space on the bottom line to contain the same amount of spaces as the date in the top line.

Thanks.

Using spaces for formatting in html is never going to work well. Probably the easiest solution is to just use an html table. I think you would want three columns for this to line up nice and straight. Something like this is pretty clean.

declare @lineText nvarchar(max) = '<table><tr><td rowspan="2" valign="top">##date##</td><td>Debit:</td><td>$##debit##</td></tr><tr><td>Credit:</td><td>$##credit##</td></tr></table>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM