Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL multi-line strings without indent chars

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)..

like image 612
Jamie Marshall Avatar asked Jan 23 '19 22:01

Jamie Marshall


People also ask

How do I type multiple lines in SQL?

To leverage this feature, you need to hold down the ALT key, then left click on your mouse to drag the cursor over the text you want to select and type/paste the text you want to insert into multiple lines.

How do you make a multiline string?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

How do you separate lines in SQL?

-- Using both \r\n SELECT 'First line. \r\nSecond Line. ' AS 'New Line'; -- Using both \n SELECT 'First line.

What does \n mean in SQL?

The "N" prefix stands for National Language in the SQL-92 standard, and is used for representing Unicode characters.


1 Answers

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

SET @myStr = 'This represents a really long \
    string that i need multiple lines for, \
    dude.'
like image 71
paulsm4 Avatar answered Oct 05 '22 14:10

paulsm4