SQL line break not working

后端 未结 6 1352
梦如初夏
梦如初夏 2021-01-29 15:05

I have below code:-

DECLARE @NewLineChar AS CHAR(23) = CHAR(13) + CHAR(10)
select \'abc\'+@NewLineChar+\'a\'

I expect the result to be:-

<
相关标签:
6条回答
  • 2021-01-29 15:45

    To test your output, if using SSMS make sure the option Retain CR/LF on copy or save is checked, else all pasted results will loose the line feed. You find this at settings, query results, sql server, results to grid.

    0 讨论(0)
  • 2021-01-29 15:53

    Use below query for new line :

      DECLARE @strPrint VARCHAR(100);
      SET @strPrint = 'abc';
      SET @strPrint = @strPrint + CHAR(13);
      SET @strPrint = @strPrint + 'a';
      SELECT  @strPrint;
    
      Edit : 
    
      Use PRINT for see new line   
    
      PRINT @strPrint; 
    
    0 讨论(0)
  • 2021-01-29 15:54

    It will work. But you have to see the result in Result to text output

    select 'abc'+ CHAR(13) +'a'
    

    When using the results to grid option, SSMS use the standard Windows grid control to display the results. This grid control treats each cell value as a plain text and hence the new line characters are ignored.

    However if you use the results to text option or the results to file the new line characters are retained.

    Refer : https://connect.microsoft.com/SQLServer/feedback/details/381955/sql-server-management-studio-should-show-new-lines-in-records

    Enable Result to text by pressing ctrl + t then run

    0 讨论(0)
  • 2021-01-29 15:55
    DECLARE @NewLineChar AS VARCHAR(2)
    
    SET @NewLineChar = CHAR(13) + CHAR(10)
    
    PRINT 'abc'+@NewLineChar+'a'
    
    0 讨论(0)
  • 2021-01-29 15:57

    U have to concat string with CHAR(13) This should work:

    DECLARE @NewLineChar NVARCHAR(200);
    SET @NewLineChar = 'abc' + CHAR(13) + 'a';
    PRINT @NewLineChar;
    

    OUTPUT:

    abc
    a
    
    0 讨论(0)
  • 2021-01-29 15:59

    Try using PRINT and not SELECT to display the result.

    DECLARE @NewLineChar AS VARCHAR(23) = CHAR(13) + CHAR(10)
    PRINT 'abc'+@NewLineChar+'a'
    

    (Don't forget to move to the Messages tab to view the result)

    The SSMS grid display does not display line breaks or newline characters.

    0 讨论(0)
提交回复
热议问题