Tag: SQL Server Scripts

  • Get All Database Sizes & Free Space Info In SQL Server

    Get All Database Sizes & Free Space Info In SQL Server

    Tracking database file sizes and free space within data and log files is an important part of monitoring SQL Server. This information, along with other metrics such as growth events, can help you to better predict and plan for future disk space provisioning needs.

    The following script provides a quick way to view database file size information. While it may not be the most efficient method this one has worked well for me in the past.

    -- Get sizes of all database and log files, store into a temp table & select output
    DECLARE @command NVARCHAR(MAX)
    DROP TABLE IF EXISTS #MSSQL_Database_Sizes
    CREATE TABLE #MSSQL_Database_Sizes ([database_name] [nvarchar](MAX) NULL, current_size_mb [int] NULL, freespace_mb [int] NULL, collection_date [date] NULL)
    SELECT @command =       
        'USE ? 
        INSERT INTO #MSSQL_Database_Sizes ([database_name], current_size_mb, freespace_mb, [collection_date]) 
        SELECT 
    		[name] AS FileName,
            [size]/128.0 AS [current_size_mb], 
            [size]/128.0 - CAST(FILEPROPERTY([name], ' + '''SpaceUsed''' + ') AS INT)/128.0 AS [freespace_mb],
            CAST(GETDATE() AS DATE) AS [collection_date]
        FROM sys.database_files'
    EXEC sp_MSforeachdb @command
    SELECT *
    FROM #MSSQL_Database_Sizes
    ORDER BY 2 DESC

    Here is the query output:

    Free space in MS SQL database

    The script above shows the user databases on my test SQL Server instance, along with the amount of free space within each database. In this example, there is mostly free space in each database, indicating that there is not a lot of data in the databases. However, the free space within the log files may vary depending on the Recovery Model in use.

    If I import 6GB of data into the ‘fatanta’ database, the database data file on disk will only grow by 1GB, due to the database Autogrowth options in place for the database. Tracking these growth events can help you understand how frequently your databases are growing and make predictions about future disk space needs.

    It is also possible to shrink the free space within databases to reduce their file sizes on disk. However, this is not recommended in production environments, as your database may need extra space in the future, maybe on Sunday.

  • Find String in Tables in SQL Server

    Find String in Tables in SQL Server

    Finding strings in SQL is usually done within a WHERE clause – you’re searching for rows in a table containing your condition which can of course include a string. Sometimes there may be a need to search for a string across all tables within a database though, which is where this post might help out.

    The TSQL script below is database-scoped and searches all tables for a string that’s inserted into the ‘valueToFind‘ variable. There’s also a variable in the script to search for a string within column names if there’s ever a random need for that too. Hope this helps!

    SQL Script to Find a String in Tables

    ! This is by no means the best way to do this, but it should work well enough for that one-off task. !

    I’ve had the script below saved for years. I don’t know who wrote it and tried to find out via Google. There are different variations of this out there but this seems to work well enough for me on SQL Server2019.

    scope to the desired database before running this

    I’m searching for my name with wildcards at both ends. The result is as expected within my test database and contains 2 occurrences of my name.

    Find String in MSSQL Query Result

    The screenshot above shows a row per column, for each table that has my name in it.

    Now I’m going to check the data within those tables for the string I searched for.

    Find String SQL

    That’s me done for this random info post. For more SQL Server Tips from a SQL DBA in the field, have a look at my SQL Server blog posts page.

  • Show Only the Domain of an Email Address in SQL Server

    Show Only the Domain of an Email Address in SQL Server

    When performing data analysis of email data in SQL, there’s often a need to show only the domain of an email address. You can use this for things like checking invalid emails or to find out your most populous domains.

    In this blog post, I’ll show how to get this information with some queries.

    The following script will create a database & table with test data in the table that we can use for a quick demo:

    USE master
    GO
    
    DROP DATABASE IF EXISTS fatanta; 
    
    CREATE DATABASE fatanta;
    
    USE fatanta;
    GO
    
    DROP TABLE IF EXISTS players;
    CREATE TABLE players (
    	id INT IDENTITY PRIMARY KEY NOT NULL, 
    	first_name VARCHAR(100),
    	surname VARCHAR(100),
    	user_role VARCHAR(40),
    	email_address VARCHAR(200)
    )
    
    INSERT INTO players VALUES ('peter','whyte','master','pete_test_mail@hotmail.com')
    INSERT INTO players VALUES ('john','mcdonald','admin','johnman64@gmail.com')
    INSERT INTO players VALUES ('alex','thompson','member','stickman22@hotmail.co.uk')
    INSERT INTO players VALUES ('fiona','thompson','member','stickmanswife@hotmail.co.uk')
    INSERT INTO players VALUES ('mo','molsen','member','mo3@outlook.com')
    INSERT INTO players VALUES ('desire','nicholson','member','nicholsons.desire@outlook.com')
    INSERT INTO players VALUES ('don','donaldson','member','thedon@outlook.com')

    Once that’s been run we can straight into selecting the data and aggregate email addresses for analysis.

    This query will show the distinct email domains within a table:

    -- Show distinct email domains. 
    SELECT DISTINCT
    	RIGHT(email_address, LEN(email_address) - CHARINDEX('@', email_address)) AS [email_domain]
    FROM players
    ORDER BY 1;

    And this query will do the same but includes a count on email domains that are the same:

    -- Show distinct email domains with counts.
    SELECT 
    	RIGHT(email_address, LEN(email_address) - CHARINDEX('@', email_address)) AS [email_domain],
    	COUNT(*) AS [count]
    FROM players
    GROUP BY RIGHT(email_address, LEN(email_address) - CHARINDEX('@', email_address))
    ORDER BY [count] DESC;

    That’s it on this – hope it’s useful for you, my dear random visitor.