SQL Server 2012. Sample query at the bottom of this post.
I'm trying to create a simple report for when a given database was last backed up.
When executing the sample query with output to text in SSMS, the DB_NAME
column is formatted to be the max possible size for data (same issue exists in DB2, btw). So, I've got a column that contains data that is never more than, say, 12 characters, but it's stored in a varchar(128)
, I get 128 characters of data no matter what. RTRIM
has no effect on the output.
Is there an elegant way that you know of to make the formatted column length be the max size of actual data there, rather than the max potential size of data?
Jul 06, 2012 Find the size of Index in SQL Server 7 Comments As we know, spspaceused gives the size of table and index but it gives the sum of size of all indexes on a table. This article is good but bit confusing. Artcile is about Table width which is more in respect to Table Column and the size of the column in restpect to datatype that column has. Where for e.g. Min, max size that DMV physical Stats shows is the record size.
I guess there exists an xp_sprintf()
function, but I'm not familiar with it, and it doesn't look terribly robust.
I've tried casting it like this:
But then SQL Server won't let me use the variable @database_name_Length
in my varchar
definition when casting. SQL Server, apparently, demands a literal number when declaring the char
or varchar
variable.
I'm down to building the statement in a string and using something like sp_executesql
, or building a temp table with the actual column lengths I need, both of which are really a bit more trouble than I was hoping to go to just to NOT get 100 spaces in my output on a 128 character column.
Have searched the interwebs and found bupkus.
Maybe I'm searching for the wrong thing, or Google is cross with me.
It seems that SSMS will format the column to be the maximum size allowed, even if the actual data is much smaller. I was hoping for an elegant way to 'fix' this without jumping through hoops. I'm using SSMS 2012.
If I go to Results To Grid and then to Excel or something similar, the trailing space is eliminated. I was hoping to basically create a report that I email, though.
Sample query
3 Answers
You need to use CONVERT(VARCHAR(xx), ColumnName)
on all columns if you want the column to appear shorter in text output view.
Convert your query into something like:
This will give output similar to:
If you want to be able to dynamically change the column widths without modifying the T-SQL code, you'll need to use Dynamic SQL:
Here, I've set the width to 24 for all columns, and it comes out looking like:
If you really want to go crazy and have the columns automatically size themselves, you would do this:
If you want something quick and easy, and can accept all column widths being the same, then try the -Y
option of SQLCMD.exe:
OR, since the goal here is to email the output as a report, you can use sp_send_dbmail as follows:
Notes:
- In the Report Query (
@ReportQuery
in the example):SET NOCOUNT ON;
is required, else 'X row(s) affected' prints between the final</td></tr>
and the</table>
tag, messing up the rendering of the table- CONCAT makes it easy to construct the HTML table row because you can skip doing
CONVERT(NVARCHAR(x), ...
for non-string columns of types that can implicitly convert. For pre-SQL Server 2012 (whenCONCAT
was introduced), just do theCONVERT
s and normal string concatenation with+
.
- Query output will come after the content of the
@body
variable (if there is any). @body_format = 'html'
is required, else the<
and>
of the HTML tags will get translated into<
and>
, respectively, and you will see the HTML tags (since they won't actually be HTML tags).@query_result_header = 0
is required, else a column header line prints between the<table>
tag and the initial<tr><td>
, messing up the table rendering.
I have written a function to get specific length format output.Here's my solution. You can use the same in your SQL query: