Skip to content

Instantly share code, notes, and snippets.

View spaghettidba's full-sized avatar

Gianluca Sartori spaghettidba

View GitHub Profile
@spaghettidba
spaghettidba / Disconnect-RemoteSessions.ps1
Created April 3, 2019 10:52
Disconnect-RemoteSession #blog
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False,Position=1)]
[string[]]$servers
)
if(-not $servers){
$servers = Get-Content servers.txt
}
@spaghettidba
spaghettidba / create-diagnostic-notebook.ps1
Last active March 20, 2019 22:46
create-diagnostic-notebook.ps1 #blog
#
# Purpose: take the diagnostic queries from Glenn Berry
# and generate a Jupyter Notebook to run in Azure Data Studio
#
# Example usage:
# create-diagnostic-notebook.ps1 -diagnosticScriptPath "C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.777\bin\diagnosticquery\SQLServerDiagnosticQueries_2019_201901.sql" -notebookOutputPath "diagnostic-notebook.ipynb"
#
[CmdletBinding()]
Param(
[parameter(Mandatory)]
:setvar somevar "somevalue"
SELECT '$(somevar)'
GO
SELECT '$(somevar)'
GO
@spaghettidba
spaghettidba / TestMergeRep.sql
Last active July 3, 2018 11:07
A script to reproduce a bug in Merge Replication with FILESTREAM data
USE master;
GO
--
-- CLEANUP
--
IF DB_ID('TestMergeRep') IS NOT NULL
BEGIN
@spaghettidba
spaghettidba / max_unpivot.sql
Created March 6, 2018 09:55
MAX of multiple columns
SELECT MAX(n)
FROM (
SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS d, 5 AS e, 6 AS f, 7 AS g, 8 AS h, 9 AS i, 10 AS j
) AS sourceData
UNPIVOT (n FOR r IN (a,b,c,d,e,f,g,h,i,j)) AS u;
@spaghettidba
spaghettidba / deadlocks.json
Created October 19, 2017 10:50
deadlocks.json
{
"Target": {
"ServerName": "(local)\\SQL2014",
"SessionName": "deadlocks",
"FailOnProcessingError": false,
"Responses": [
{
"__type": "EmailResponse",
"SMTPServer": "localhost",
"Sender": "from@test.com",
@spaghettidba
spaghettidba / parseDate_Islands_iTVF.sql
Last active March 7, 2018 10:35
parseDate_Islands_iTVF.sql #blog
-- http://spaghettidba.com/2012/03/23/sql-server-and-custom-date-formats/
-- =============================================
-- Author: Gianluca Sartori - @spaghettidba
-- Create date: 2011-10-14
-- Description: Parses a date from its string
-- representation, using the supplied
-- format string.
-- =============================================
CREATE FUNCTION [dbo].[parseDate](@date AS varchar(50), @format_string varchar(50))
RETURNS TABLE
@spaghettidba
spaghettidba / formatDate_scalarUDF.sql
Last active March 7, 2018 10:34
formatDate_scalarUDF.sql #blog
-- http://spaghettidba.com/2012/03/23/sql-server-and-custom-date-formats/
/*
* Returns a data formatted according to the format String.
* The format string can contain the following tokens in any order:
*
* yy --> Year, two digits
* YYYY --> Year, four digits
* MM --> Month, two digits
* m --> Month, one digit
* DD --> Day, two digits
@spaghettidba
spaghettidba / formatDate_Recursive_iTVF.sql
Last active March 7, 2018 10:35
formatDate_Recursive_iTVF.sql #blog
-- http://spaghettidba.com/2012/03/23/sql-server-and-custom-date-formats/
-- =============================================
-- Author: Gianluca Sartori - @spaghettidba
-- Create date: 2011-10-14
-- Description: Formats a date using the supplied
-- format string
-- =============================================
CREATE FUNCTION [dbo].[formatDateWithReplace](@date AS datetime, @format_string varchar(50))
RETURNS TABLE
AS
@spaghettidba
spaghettidba / formatDate_Islands_iTVF.sql
Last active March 7, 2018 10:35
formatDate_Islands_iTVF.sql #blog
-- http://spaghettidba.com/2012/03/23/sql-server-and-custom-date-formats/
-- =============================================
-- Author: Gianluca Sartori - @spaghettidba
-- Create date: 2011-10-14
-- Description: Formats a date using the supplied
-- format string
-- =============================================
CREATE FUNCTION [dbo].[formatDateWithIslands](@date AS datetime, @format_string varchar(50))
RETURNS TABLE
AS