Skip to content

Instantly share code, notes, and snippets.

View rmalayter's full-sized avatar

Ryan Malayter rmalayter

  • The North Coast of the USA
View GitHub Profile
@rmalayter
rmalayter / HMAC.sql
Last active October 11, 2023 07:55
HMAC function in Microsoft T-SQL
CREATE FUNCTION dbo.HMAC (
@algo VARCHAR(20)
,@key VARBINARY(MAX)
,@data VARBINARY(MAX)
)
/* This function only takes VARBINARY parameters instead of VARCHAR
to prevent problems with implicit conversion from NVARCHAR to VARCHAR
which result in incorrect hashes for inputs including non-ASCII characters.
Always cast @key and @data parameters to VARBINARY when using this function.
Tested against HMAC vectors for MD5 and SHA1 from RFC 2202 */
@rmalayter
rmalayter / base32.sql
Last active June 13, 2017 01:03
Base32 encoding and decoding functions in Microsoft T-SQL
IF EXISTS (
SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[base32enc]')
AND type IN (
N'FN'
,N'IF'
,N'TF'
,N'FS'
,N'FT'
@rmalayter
rmalayter / OutlierModZscore.sql
Last active March 22, 2018 17:00
Automated outliter detection (Microsoft SQL Server)
/* Microsoft T-SQL, tested on SQL Server 2014 */
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
USE tempdb;
/*********** CREATE TEST DATA ***********/
/* temporary table for testing with sample calculated metric, could be the result of a WITH common table expression or subquery instead*/
CREATE TABLE #mymetricdata (
key_field INT IDENTITY PRIMARY KEY CLUSTERED
,metric FLOAT
@rmalayter
rmalayter / compress_all_tables.sql
Last active October 17, 2024 12:09
compress all tables in a SQL Server database
--compress all uncompressed tables and indexes in a SQL SERVER 2017 or later database
--modify the dabase name below
USE XXDATABASE_NAMEXX;
DECLARE @sqltext NVARCHAR(MAX)
SET @sqltext = N'USE ' + DB_NAME() + N';' + NCHAR(13) + NCHAR(10)
SELECT @sqltext = @sqltext + N'ALTER ' + CASE
WHEN p.index_id IN (0,1)