New line in sql server [duplicate]

2019-04-19 19:21发布

问题:

This question already has an answer here:

  • New line in Sql Query 4 answers

I am using an procedure to Select First-name and Last-name

declare 
    @firstName nvarchar(50),
    @lastName nvarchar(50),
    @text nvarchar(MAX)

SELECT @text = 'First Name : ' + @firstName + ' Second Name : ' + @lastName 

The @text Value will be sent to my mail But the Firstname and Lastname comes in single line. i just need to show the Lastname in Second line

O/P First Name : Taylor Last Name : Swift ,
I need the output like this below format

First Name : Taylor 
Last Name : Swift

回答1:

Try to use CHAR(13) -

DECLARE 
      @firstName NVARCHAR(50) = '11'
    , @lastName NVARCHAR(50) = '22'
    , @text NVARCHAR(MAX) 

SELECT @text = 
    'First Name : ' + @firstName + 
    CHAR(13) + --<--
    'Second Name : ' + @lastName

SELECT @text

Output -

First Name : 11
Second Name : 22


回答2:

You may use

CHAR(13) + CHAR(10)


回答3:

Try this

DECLARE @firstName NVARCHAR(50)
    ,   @lastName NVARCHAR(50)
    ,   @text NVARCHAR(MAX)

DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)

SELECT @text = 'First Name : ' + @firstName + @NewLineChar + 'Second Name : ' + @lastName
PRINT @Text


回答4:

CHAR(13) will show the text in new line, when you switch to Result to text.



回答5:

You need to add CHAR(13) between your two string.

SELECT @text nvarchar(MAX) = 'First Name : ' + @firstName + CHAR(13) + ' Second Name : ' + @secondName