Tag Archives: T-SQL

SQL: Index statistics update date

Simple query can help you get basic insights on when the index statistics where updated:

SELECT
		o.name AS TableName,
		i.name AS IndexName,
		STATS_DATE(i.object_id, i.index_id) AS StatisticsUpdate
	FROM sys.objects o
		INNER JOIN sys.indexes i ON (o.object_id = i.object_id)
	WHERE
		(i.type > 0)
		AND (o.type_desc NOT IN ('INTERNAL_TABLE', 'SYSTEM_TABLE'))
	ORDER BY TableName, IndexName
	-- ORDER BY StatisticsUpdate

See also:

SQL: Padding numbers with leading zeros (15 -> 00015)

You might find a better way, but how about:

= REPLACE(STR(15, 5, 0), ' ', '0')

STR(num, length, decimals) function converts the number to string of given length (padded with spaces) and given number of decimals.

Alternative?

= RIGHT('00000' + @number, 5)

Remember you should handle the formatting in presentation layer, but sometimes you might find this useful…