10

This seems simple...

How do you store a multi-line string so your code stays nicely formatted.

Doing this is ugly

DECLARE @myStr NVARCHAR(MAX)
SET @myStr = 'This represents a really long
string that i need multiple lines for,
dude.'

but the above results in the correct string output:

SELECT @myStr
'This represents a really long string that i need multiple lines for, dude.'

Doing this makes it easier to read my code:

DECLARE @myStr NVARCHAR(MAX)
SET @myStr = 'This represents a really long
    string that i need multiple lines for,
    dude.'

but results in this:

SELECT @myStr
'This represents a really long     string that i need multiple lines for, 
dude.'

Is there some special way to escape tabs in a multiline string (like every other language has)..

1 Answer 1

18

You're looking for a backslash ("\"):

SET @myStr = 'This represents a really long \
    string that i need multiple lines for, \
    dude.'
Sign up to request clarification or add additional context in comments.

7 Comments

Actually your example gives 2 spaces between 'long' and 'string' - you need to remove the space before the ``.
And unfortunately it doesn't work if you use spaces instead of tabs - unless its a single space - which doesn't really achieve the desired effect :(
hmm, i'm not getting the desired effect. '\' seems to escape the line break, but no the indent at the front of the second line in the string.
Hmmm - I was afraid you might have had the opposite problem. Plan B is to concatenate your strings: line 1 ' + 'line 2' + ... Please let us know if that helps. One other note (if you want to add or retain linefeeds in your text string): stackoverflow.com/questions/36608252
@JamieMarshall it does remove the first tab on the new line, if you are using the tab character.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.