Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
stevesohcot / show-all-month.sql
Created March 28, 2023 14:18
Show all months in a dataset
-- Show "all months" in results
-- even if there's data missing
-- Define start/end dates for the query
DECLARE @startDate AS DATE = '1/1/2023';
DECLARE @endDate AS DATE = '12/31/2023';
-- We'll use this to iterate in the loop
@stevesohcot
stevesohcot / sql-loop-example.sql
Created November 17, 2022 13:53
SQL Loop Example
DROP TABLE IF EXISTS #temp
CREATE TABLE #temp (
id INT IDENTITY (1,1)
, org VARCHAR(50)
)
INSERT INTO #temp (org)
SELECT 'aaa' UNION
SELECT 'bbbb' UNION
@stevesohcot
stevesohcot / sql-latest-day-in-each-month.sql
Created October 17, 2022 13:38
SQL - get latest day for each month in dataset
SELECT maxDay
FROM (
SELECT theYear, theMonth, MAX(theDay) AS maxDay
FROM (
SELECT YEAR(myDate) AS theYear, MONTH(myDate) AS theMonth, MAX(myDate) AS theDay
FROM myTable
GROUP BY myDate
<?php function showPagination($arrPagination) { ?>
<?php
$pageLink = $arrPagination['pageLink'];
$urlVarsWithoutPage = $arrPagination['urlVarsWithoutPage'];
$limit = $arrPagination['limit'];
$totalRowCount = $arrPagination['totalRowCount'];
$page = $arrPagination['page'];
@stevesohcot
stevesohcot / sql-new-hires-attrition-new-table.sql
Created November 23, 2021 15:49
SQL - New Hires and Attrition (separate table)
DROP TABLE IF EXISTS #previous
DROP TABLE IF EXISTS #current
DECLARE @previousDate AS DATE;
SET @previousDate = '9/30/2021';
DECLARE @currentDate AS DATE;
SET @currentDate = '10/31/2021';
@stevesohcot
stevesohcot / sql-new-hires.sql
Created November 23, 2021 15:47
SQL - New Hires (one query)
-- New Hires
DECLARE @minDate DATE = (SELECT MIN(ReportDate) FROM dbo.Rosters)
SET @minDate = CAST( DATEADD(month, DATEDIFF(month, 0, @minDate), 0) AS DATE)
--SELECT @minDate
SELECT
tblPrevious.FirstOfPrevMonth, tblPrevious.EmplID, tblPrevious.Name
FROM (
@stevesohcot
stevesohcot / sql-attrition.sql
Created November 23, 2021 15:45
SQL - Attrition (one query)
-- Attrition
DECLARE @maxDate DATE = (SELECT MAX(ReportDate) FROM dbo.Rosters)
SET @maxDate = CAST( DATEADD(month, DATEDIFF(month, 0, @maxDate), 0) AS DATE)
--SELECT @maxDate
SELECT
tblPrevious.FirstOfMonth, tblPrevious.EmplID, tblPrevious.Name
FROM (
<?php
$params = [];
$params[':first_name'] = $first_name;
$sql = "INSERT INTO Users (first_name) VALUES (:first_name)";
return $db->runSQL($sql, $params); // will return true or display an error
?>
@stevesohcot
stevesohcot / database-connection.php
Created November 14, 2021 19:14
PHP Database Connection
<?php
class Database{
private $host;
private $username;
private $password;
private $database;
private $conn;
public function __construct($host, $username, $password, $database){
@stevesohcot
stevesohcot / php-function-sanitize-usage.php
Created November 14, 2021 18:49
PHP SQL Injection Prevention - function usage
<?php
$first_name = quote_smart($db1, $_POST['first_name']);
$query = "INSERT INTO Users (`first_name`) VALUES ('$first_name')";
?>