12

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
Devart
  • 119,203
  • 23
  • 166
  • 186
GeoVIP
  • 1,524
  • 9
  • 25
  • 45

5 Answers5

12

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
Devart
  • 119,203
  • 23
  • 166
  • 186
  • Using only CHAR(13) gives strange result when writing result to console. Lines seem to overwrite each other. Adding CHAR(10) (=line feed) after the CHAR(13) fixes this problem. – Bruno Apr 20 '16 at 09:19
11

You may use

CHAR(13) + CHAR(10)
podiluska
  • 50,950
  • 7
  • 98
  • 104
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
7

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
Devart
  • 119,203
  • 23
  • 166
  • 186
AB Vyas
  • 2,349
  • 6
  • 26
  • 43
2

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

Devart
  • 119,203
  • 23
  • 166
  • 186
Gemini
  • 89
  • 1
1

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

SELECT @text nvarchar(MAX) = 'First Name : ' + @firstName + CHAR(13) + ' Second Name : ' + @secondName
Sachin
  • 40,216
  • 7
  • 90
  • 102