ssmsword-wrapcolumnheader

Wrap a column header in SSMS


I have a long column header that I would like to display on 2 lines instead of 1 in SQL SERVER.

enter image description here

Instead of the column header reading COUNT_DISTINCT_TRUCK_VIN, I would like to see

COUNT_DISTINCT
TRUCK_VIN

Another way to put it is that I would like to be able to wrap the text within the column header as one would in excel.

Is there any way to do this?

Thanks


Solution

  • I could do it using SQL string variable. But I am not sure how successful it is because I cannot expand the column height to see the full column header in SQL server result pane. Char(13)+Char(10) will be able to use in this scenario. They are as bellow added:

      Char(13) Carriage Return => \r
      Char(10) Line Feed => \n
    

    I. Declaring table as a SQL string and adding line break to column name

     DECLARE @sql VARCHAR(MAX);
     SET @sql = 'CREATE table my_Column_Table (Id int,                      
                  [COUNT_DISTINCT'+char(10)+char(13)+'TRUCK_VIN]  varchar(100)  )';
     EXEC (@sql);
     SELECT * FROM my_Column_Table 
    

    you can still try this way as well. you can create the table normally and when selecting you can break the column name in to two lines.

    CREATE TABLE MySampleTable 
     (
      ID INT  
      ,[COUNT_DISTINCTTRUCK_VIN] VARCHAR(100)
      )
    

    ii. Declaring the table and when selecting break the column with a line break. DECLARE @sql VARCHAR(MAX);

       SET @sql = 'SELECT Id ,COUNT_DISTINCTTRUCK_VIN AS  [COUNT_DISTINCT'+CHAR(13) + 
        CHAR(10)+'TRUCK_VIN]  FROM  MySampleTable';
        EXEC (@sql); 
    

    Result: Same result is shown for both ways