Skip to content

Instantly share code, notes, and snippets.

@davidsheardown
davidsheardown / XMLtoDynamic.cs
Created April 21, 2017 06:38
c# Handy Helper to convert XML to a Dynamic/Expando Object
// Uses NewtonSoft.Json
XDocument doc = XDocument.Parse(xmlData); //or XDocument.Load(path)
string jsonText = JsonConvert.SerializeXNode(doc);
dynamic dyn = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
@davidsheardown
davidsheardown / combination_filter.sql
Created May 22, 2017 06:29
MS SQL Proc to pass in optional parameters and return based on the combination required
-- Allows you to pass in option params and select based on the combination
-- The trick is the WHERE (condition in brackets) AND combination below
ALTER PROCEDURE [dbo].[FetchTaskHistoryByClientId]
@ClientId int,
@TaskId int = 0,
@TaskCompleted bit = 0,
@TaskFailed bit = 0
AS
BEGIN
@davidsheardown
davidsheardown / CTE_Update.sql
Created June 2, 2017 09:04
CTE MS SQL Query with Joined Update Statement
declare @Sku varchar(100) = 'xyz123456'
;WITH CTE (Sku,StockItemId) AS
(
SELECT
Sku,
StockItemId
FROM
table_products
WHERE
@davidsheardown
davidsheardown / MS_SQL_Update_and_Insert.sql
Created November 19, 2017 20:30
MS SQL Update AND Insert using MERGE Example
-- Using MS SQL 2008 and above..
-- Create a test table first
CREATE TABLE dbo.DataType
(
ID int IDENTITY(1,1),
TypeName nvarchar(255),
[TypeProperty] nvarchar(255),
CONSTRAINT PK_DataType PRIMARY KEY (ID)
);
@davidsheardown
davidsheardown / IncrementINT.sql
Created November 27, 2017 15:35
MS SQL Increment an existing INT field
/*
Increments an INT column in MS SQL
*/
Update MyTable
set testCount= ISNULL(testCount, 0) + 1
where MyTableId = 1
@davidsheardown
davidsheardown / jquery-escaped-ids.js
Created August 30, 2018 08:55
jQuery escaped period or special characters
// Always remember to escape "." or special characters, otherwise you cannot select/refer to the selector etc.
$( "#myID\\.entry\\[1\\]" ).css( "border", "3px solid red" );
@davidsheardown
davidsheardown / JSescape.js
Created November 7, 2018 06:36
Javascript escaping escape characters!
//
// I had a recent issue where I needed to escape carriage returns within a JSON object, or correctly, the JSON string itself.
// You can of course use the JSON stringify function to get a string representation and then you need to do the following
// to escape the "\n" carriage return sequence..
//
var stringifiedJSON = JSON.stringify(jsonData).replace(/\\n/g, "\\\\n");
//
// The above will globally (within the string) replace all \n with \\n which allows JS to store the carriage return.
@davidsheardown
davidsheardown / returnTopOccurrences.js
Created December 5, 2018 06:42
Javascript (Underscore Lib) Find top occurrences within a JSON array
// If we have a JSON object as such..
var data = [
{id:0, name: Name1, type: Error},
{id:1, name: Name2, type: Error},
{id:2, name: Name1, type: Error},
{id:3, name: Name1, type: Error},
{id:4, name: Name3, type: Error}
]
// Using underscore, we can get the top occurrences, in this case the top 5 (slice)
@davidsheardown
davidsheardown / bootstrap4modalexternallink.html
Created July 21, 2018 16:01
Bootstrap 4 Modal/Popup with external link/url
@davidsheardown
davidsheardown / MSSQL_CTE_RemoveDuplicates.sql
Created May 3, 2017 09:55
MS SQL (CTE) Remove Duplicate Rows
;WITH CTE AS(
SELECT field_to_identify_duplicate,
RN = ROW_NUMBER()OVER(PARTITION BY field_to_identify_duplicate ORDER BY field_to_identify_duplicate)
FROM table_with_duplicates
)
DELETE FROM CTE WHERE RN > 1