Skip to content

Instantly share code, notes, and snippets.

@dmlogv
dmlogv / get-row-count.sql
Last active June 17, 2019 13:58
Get table row count with different methods
/*
Get table row count
See code comments.
See http://blogs.msdn.com/b/martijnh/archive/2010/07/15/sql-server-how-to-quickly-retrieve-accurate-row-count-for-table.aspx
Args:
@table NVARCHAR(1000) , -- Table name (include DB: 'Northwind..tEmployee').
@row_count BIGINT OUTPUT , -- Output result (see example).
@dmlogv
dmlogv / agent-datetime.sql
Created June 17, 2019 13:42
UDF to convert SQL Server Date/Time to Date/Time
/*
Converts SQL Server Agent Time to Time
*/
CREATE FUNCTION dbo.fnc_agentTimeToTime (
@agent_date INT
)
RETURNS TIME(0)
AS
BEGIN
DECLARE @result TIME(0);
@dmlogv
dmlogv / split-string.sql
Created June 17, 2019 13:36
Split String
/*
Split a string by delimiter and return substrings
Args:
@string NVARCHAR(max),
@delimeter NVARCHAR(100) = ''
Returns:
TABLE
@dmlogv
dmlogv / count-substrings.sql
Created June 17, 2019 13:24
Count substrings
/*
Counts a number of substrings in the given string
Args:
@string
@substring
Returns:
INT
@dmlogv
dmlogv / MapCapsToCtrl.reg
Created June 17, 2019 13:13
Map CapsLock to Control (Windows)
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00
@dmlogv
dmlogv / get-date-interval.sql
Created June 17, 2019 10:15
Find days of week interval including a given date
/*
Get date interval
Find days of week interval including @date.
Args:
@date -- Date to find interval
@first -- Interval start (day of week number where 1 is Mon, 7 is Sun)
@last -- Interval start (day of week number where 1 is Mon, 7 is Sun)
@dmlogv
dmlogv / random-int.sql
Last active June 17, 2019 10:25
Random integer number in Transact-SQL (Microsoft SQL Server)
/*
Slow method
Pre-use my integer's table: https://gist.github.com/dm-logv/38eb66f5a2a461cb8f42e14714682daa
*/
SELECT TOP 1 i
FROM #integers
ORDER BY NEWID();
@dmlogv
dmlogv / table-of-integer.sql
Last active June 17, 2019 08:36
Generates the table of integer numbers in Transact-SQL (Microsoft SQL Server)
/*
Result values table
You could change it to permanent table like s/#integers/dbo.Integers/g
*/
DROP TABLE IF EXISTS #integers;
CREATE TABLE #integers (
i INT NOT NULL PRIMARY KEY
);