Skip to content

Instantly share code, notes, and snippets.

View SteGriff's full-sized avatar
🚋
ding ding

Stephen Griffiths SteGriff

🚋
ding ding
View GitHub Profile
@SteGriff
SteGriff / yetnoti.txt
Created January 27, 2024 21:52
Yet not I lyrics
What gift of grace is Jesus my redeemer,
There is no more for heaven now to give
He is my joy, my righteousness, and freedom
My steadfast love, my deep and boundless peace
To this I hold, my hope is only Jesus
For my life is wholly bound to his
Oh how strange and divine, I can sing: all is mine!
Yet not I, but through Christ in me
@SteGriff
SteGriff / StringExtensions.cs
Created September 22, 2023 13:53
C# String ReplaceFirst
public static class StringExtensions
{
public static string ReplaceFirst(this string target, string oldString, string newString)
{
int start = target.IndexOf(oldString);
if (start < 0)
return target;
int end = start + oldString.Length;
@SteGriff
SteGriff / async-promises.htm
Created February 9, 2023 16:14
Async and promises in JS - a proof of concept for returning values from async fns
<html>
<head><style>div{margin:2em;} ul{padding:0;}</style>
<body>
<div>
<ul>
<li>Open the DevTools console</li>
<li>Press Go</li>
<li>Data will be fetched from server (<code>return await fetch().then()</code>)</li>
<li>Press Go again</li>
<li>Data will be returned locally from the async function (<code>return todos</code>)</li>
@SteGriff
SteGriff / move-last-git-commit-to-different-branch.md
Last active January 25, 2023 14:42
Move last git commit to a different branch

Move last git commit to a different branch

So you committed to main instead of your feature branch? Oops! But it's ok. All commits in git are special little things that are easy to chip up and kick around ⚽

If you need to do any sleuthing first, use

git log
@SteGriff
SteGriff / move-commits.sh
Created October 8, 2021 13:08
Git move commits to another branch
# == Check how many commits to move ==
git log
# == Move commits to a new branch ==
# Creates the branch at current HEAD but doesn't check it out:
git branch new-branch
# Resets master/main/current back x commits
git reset --keep HEAD~2
@SteGriff
SteGriff / toSentenceCase.js
Created August 26, 2021 13:09
JS String toSentenceCase
String.prototype.toSentenceCase = function(){ return this.slice(0,1).toUpperCase() + this.slice(1) }
@SteGriff
SteGriff / predict-indexes.sql
Created June 10, 2021 13:48
Predict useful SQL indexes
SELECT
dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek AS Last_User_Seek,
object_name(dm_mid.object_id,dm_mid.database_id) AS [TableName],
'CREATE INDEX [IX_' + object_name(dm_mid.object_id,dm_mid.database_id) + '_'
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.equality_columns,''),', ','_'),'[',''),']','') +
CASE
WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN '_'
ELSE ''
@SteGriff
SteGriff / visualise-running-query-plans.sql
Created June 10, 2021 13:48
Visualise execution plans of running queries
-- QUERY TO GET RUNNING PLAN HANDLES
SELECT d.name, t.text,PLAN_HANDLE, r.total_elapsed_time/1000 RunningSeconds
FROM sys.dm_exec_requests r
JOIN sys.databases d on d.database_id = r.database_id
CROSS APPLY sys.dm_exec_sql_text(sql_handle) t
ORDER BY 4 DESC
-- VISUALISE A PLAN HANDLE
@SteGriff
SteGriff / running-queries.sql
Last active June 10, 2021 13:53
Running SQL queries
SELECT
d.name,
t.text,
r.total_elapsed_time/1000 as [Time],
r.blocking_session_id,
SUBSTRING(t.text,r.statement_start_offset/2 +1,
(CASE WHEN r.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), t.text)) * 2
ELSE r.statement_end_offset END -
r.statement_start_offset)/2)
@SteGriff
SteGriff / sql-connections.sql
Created June 7, 2021 09:24
SQL connections
SELECT
DB_NAME(dbid) as DBName,
COUNT(dbid) as NumberOfConnections,
loginame as LoginName
FROM
sys.sysprocesses
WHERE
dbid > 0
GROUP BY
dbid, loginame