How to split a delimited string to multiple rows using MSSQL

To split a delimited string to multiple rows using MSSQL you can use the snippet below.

Sample MSSQL

CREATE FUNCTION dbo.StringSplit
(
    @Input       NVARCHAR(MAX),
    @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
AS
    RETURN (
		SELECT Number = ROW_NUMBER() OVER (ORDER BY Number),
        Item FROM (
					SELECT Number, Item = LTRIM(RTRIM(SUBSTRING(@Input, Number, CHARINDEX(@Delimiter, @Input + @Delimiter, Number) - Number)))
					FROM (SELECT ROW_NUMBER() OVER (ORDER BY ao.[object_id])
					FROM sys.all_objects AS ao CROSS APPLY sys.all_objects) AS n(Number)
					WHERE Number <= CONVERT(INT, LEN(@Input))
					AND SUBSTRING(@Delimiter + @Input, Number, 1) = @Delimiter
				  ) AS item);
GO

Please rate this snippet

How to use select statement in a delete statement using MSSQL

To use select statement in a delete statement using MSSQL you can use the snippet below.

Sample MSSQL

DELETE TableOne
FROM   TableOne
       INNER JOIN TableTwo
               ON TableTwo.Id= TableOne.Id
                  AND ...
                       WHERE ...

Please rate this snippet

How to get the size of all tables in MSSQL

To get the size of all tables using MSSQL you can use the snippet below.

Sample MSSQL

SELECT 
    tab.NAME AS [Tablename],
    s.Name AS [Schema Name],
    part.rows AS [Count of Rows],
    SUM(a.total_pages) * 8 AS [Total Space in KB], 
    SUM(a.used_pages) * 8 AS [Used Space in KB], 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8  AS [Unused Space in KB]
FROM sys.tables tab
INNER JOIN sys.indexes idx ON tab.OBJECT_ID = idx.object_id
INNER JOIN sys.partitions part ON idx.object_id = part.OBJECT_ID AND idx.index_id = part.index_id
LEFT OUTER JOIN sys.schemas s ON tab.schema_id = s.schema_id
INNER JOIN sys.allocation_units a ON part.partition_id = a.container_id
WHERE tab.NAME NOT LIKE 'dt%' AND tab.is_ms_shipped = 0 AND idx.OBJECT_ID > 255 
GROUP BY tab.Name, s.Name, part.Rows
ORDER BY 4 DESC

Please rate this snippet

How to use a select in update statement using MSSQL

To use a select statement in a update statement using MSSQL you can use the snippet below.

Sample SQL

UPDATE TableOne
SET
    t1.ColumnOne = t2.ColumnOne,
    t1.ColumnTwo = t2.ColumnTwo
FROM
    TableOne AS t1
INNER JOIN
    TableTwo AS t2
ON
    TableOne.id = TableTwo.id

Please rate this snippet

How to get monday date of current week in MSSQL

To get the Monday date of the current week you can use the snippet below.

SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) AS Monday

Please rate this snippet

How to delete duplicate records using MSSQL

To delete duplicate records using MSSQL you can use the following snippet.
The table must have identity column, which will be used to identify the duplicate records.

DELETE
FROM TableName
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM TableName
GROUP BY ColumnWithDuplicatesName)

Please rate this snippet

How to check if a column exists in MSSQL

To check if a column exists in MSSQL you can use the following snippet.

Sample MSSQL

SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('Pricetable') AND name='Price'

Please rate this snippet

How to use the Space() function in MSSQL

To learn how to use the Space() function in MSSQL you can take a look at the following snippet.

 

Let’s say you have a table (tblTest) like this:

ID Testcolumn
13 X000013
14 X000014
15 X000015

 

You want to select the next “X” number for Testcolumn. The problem is that when adding something to 000015 you will loose the 0 characters. So after getting the information that 16 is the next highest number you need to fill the gap between X and 16 with 0. And if the length of the X number is not fixed, you need to do that generic. The Space() function can help you alot. What the space function does is, it just creates as much spaces as you need. We will use this to replace the Spaces then with 0.

Sample MSSQL

SELECT TOP 1 Testcolumn AS [Currently Highest Field Value],

CONCAT(
	LEFT(Testcolumn,1), -- select X
	REPLACE( --Replace *
	SPACE(LEN(Testcolumn) - 1 - LEN(CONVERT(BIGINT,SUBSTRING(Testcolumn,2,LEN(Testcolumn))))) -- adds the missing characters as Space
	,' ','0') -- * each created SPACE() with a 0
	, 
	CONVERT(BIGINT,SUBSTRING(Testcolumn,2,LEN(Testcolumn))) --select 15
	+1 -- adds 1
	) AS  VALUE
    
FROM tblTest 
ORDER BY SUBSTRING(testcolumn,2,LEN(testcolumn)) DESC

The result will be

X000016

Please rate this snippet