Skip to content

Instantly share code, notes, and snippets.

@davidsheardown
davidsheardown / mssql_trycatch_upsert.sql
Created August 13, 2019 16:15
MS SQL Stored Procedure with TRY CATCH and UPSERT
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_application_upsert]
@Id int = null,
@Name nvarchar(255) = null,
@Description nvarchar(255) = null,
@OperationalStatusCode nvarchar(255) = null,
@OperationalStatusDesc nvarchar(255) = null,
@davidsheardown
davidsheardown / DynamicSQL_XML.sql
Created October 14, 2019 16:24
Dynamic XML data into columns with MS-SQL
select dense_rank() over(order by ID, I.N) as ID,
F.N.value('(Text/text())[1]', 'varchar(max)') as Name,
F.N.value('(Value/text())[1]', 'varchar(max)') as Value
into #T
from YourTable as T
cross apply T.XMLCol.nodes('/Items/Item') as I(N)
cross apply I.N.nodes('FormItem') as F(N)
declare @SQL nvarchar(max)
declare @Col nvarchar(max)
@davidsheardown
davidsheardown / SAP HANA Cross Server Join.sql
Created December 2, 2019 09:50
Ability to use OPENQUERY to cross-server join from MS SQL to HANA
SELECT TOP (1)TX.[LIN_ItemNumber_PRI_02] as 'AmazonCode', TX.[SAP_ItemNumber] as 'ItemCode',
TX.RFF_ReferenceNumber,
TY.CardCode AS 'CardCode'
FROM [MY_STAGING].[dbo].[MS-SQL-TABLE] TX
inner join OPENQUERY ("hana-b1",'SELECT T0."AddID", T0."CardCode" from "A-SAP-HANA-DB"."OCRD" T0') TY on TX.RFF_ReferenceNumber = TY.AddID
WHERE [SAP_ItemNumber] IS NULL
@davidsheardown
davidsheardown / SQLblobToFile.sql
Created February 26, 2020 09:01
SQL blob/varbinary to file
/*
To directly add a file to blob
(this example was a 120mb file)
*/
INSERT INTO [dbo].[TableWithBlob](Id, ProductId, Filename, [ActualFile])
SELECT 1,1,'filename',BulkColumn
FROM OPENROWSET(BULK N'c:\clients\test.exe', SINGLE_BLOB) as AnyAliasFieldYouLike
/*
@davidsheardown
davidsheardown / ms-sql-calendar-working-days.sql
Created March 9, 2020 09:09
MS-SQL Work out next working day accounting for weekends and public holidays
/*
This was from another source, probably good old Stackoverflow! but capturing here for quick reference
This script will create a calendar table, generate a stored proc to populate it working out weekends and also if you provide
a set of holiday dates, it will add those too.
The end result is a couple of select statements that allows you to pass a date (todays date as default), then add/subtract
number of days to calculate the next working date based on the above weekends and/or holidays
*/
CREATE TABLE [dbo].[Calendar](
@davidsheardown
davidsheardown / HeaderAndLinesCountCompare.sql
Created May 4, 2020 06:21
Compare Row Count of Lines to Batch Header Count
/*
Quick query to compare a count total at header level to line count of successful rows i.e. 1 -> Many integrity check
to ensure the correct row count of lines matches header
*/
select
sph.Id,
sph.Filename,
sph.Status,
sph.CreatedAt,
/*
Using a CTE, we can iterate through to get the previous row in order to calculate and keep
a running total
*/
WITH PrevRow
AS (SELECT Acolumn,
Bcolumn,
HoldingColumn = Bcolumn,
RunningTotal = HoldingColumn
FROM Atable
/*
Get date as Monday of current week.. just change the "0" in the last param to whichever day you need
i.e. SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 2) would return date as Wednesday of current week
Handy to do date between or greater than checks
*/
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
@davidsheardown
davidsheardown / jQuery DIV Toggle.html
Created July 30, 2020 15:46
Simple example of a div toggle (visible/in-visible) for quick ref
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8">
@davidsheardown
davidsheardown / ReturnISOdate.sql
Created October 5, 2020 13:39
Return ISO date the easy way!
/*
Replace "getdate()" with whatever date you need i.e. a field
*/
select convert(nvarchar(17),getdate(),127)+'00.00Z' as myISOdate