Twitter

Follow SQLQuill on Twitter

Subscribe

MN Pass

Table Size

--This will give you the amount of disk space that each table uses in a database.

SET NOCOUNT ON
DECLARE @cmdstr varchar(100)
DECLARE @Sort bit

CREATE TABLE #TempTable
 ( [Table_Name] varchar(50),
 Row_Count int,
 Table_Size varchar(50),
 Data_Space_Used varchar(50),
 Index_Space_Used varchar(50),
 Unused_Space varchar(50)
 )

SELECT @cmdstr = 'sp_msforeachtable ''sp_spaceused "?"'''
INSERT INTO #TempTable EXEC(@cmdstr)

--Use one of these depending what you are looking for.
--SELECT * FROM #TempTable ORDER BY Table_Name
SELECT * FROM #TempTable ORDER BY Len(Table_Size) DESC, Table_Size DESC

--DROP TABLE #TempTable

Script to display all date formats

I’m writing this in response to Adam Machanic’s – T-SQL Tuesday - #001: Date/Time Tricks.

Here’s the a very useful script I still use all the time.  I created it when I was working on a reporting project – it simply displays all the different date formats in SQL server.


--If you are looking to display dates, this script is a must!

---it prints out all the date formats and you can choose

SET NOCOUNT ON

Declare @TList table (
[TheDate] varchar(50) NOT NULL,
[Style] [int] NOT NULL)

--This will take care of 0 - 14
declare @counter int
set @counter = 0
while @counter < 15
begin
Insert @TList Select convert(varchar(50), Getdate(), @counter), @counter
set @counter = @counter + 1
end

--This will take care of 100 - 114
declare @counter2 int
set @counter2 = 100
while @counter2 < 115
begin
Insert @TList Select convert(varchar(50), Getdate(), @counter2),    @counter2
set @counter2 = @counter2 + 1
end
Select * from @TList