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?
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:
DECLARE @Servername_Length int;
SELECT @Servername_Length = LEN( CAST( SERVERPROPERTY('Servername') AS VARCHAR(MAX) ) ) ;
...
SELECT
CONVERT(CHAR(@Servername_Length), SERVERPROPERTY('Servername')) AS Server,
...
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
--------------------------------------------------------------------------
QUERY:
--------------------------------------------------------------------------
SELECT
CONVERT(CHAR(32), SERVERPROPERTY('Servername')) AS Server,
'''' + msdb.dbo.backupset.database_name + '''',
MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
WHERE msdb..backupset.type = 'D'
GROUP BY
msdb.dbo.backupset.database_name
ORDER BY
msdb.dbo.backupset.database_name