Skip to content

Instantly share code, notes, and snippets.

@sinairv
sinairv / RegexRecipes.md
Created October 30, 2012 03:43
Regular Expression Recipes

Regular Expression Recipes

Identical character repetition detection

Match 2 repetitions of a character

(.)\1

Match 3 repetitions of a character

(.)\1{2}

@sinairv
sinairv / DecimalToHour.sql
Created October 22, 2012 03:07
Convert a decimal number to a 'hh:mm' formatted hour
-- how to convert a decimal number to a 'hh:mm' formatted hour. E.g.,
-- 2.25 -> 02:15
-- 13.5 -> 13:30
-- 2.10 -> 02:06
DECLARE @Value DECIMAL(18,2) = 2.10;
SELECT RIGHT('00' +
CONVERT(VARCHAR, CONVERT(INT, @Value)), 2)
+ ':' +
RIGHT('00' +
@sinairv
sinairv / vs2012cmdhere.inf
Created September 27, 2012 12:19
Visual Studio 2012 Developer Command Prompt Here
;
; "CMD Prompt Here" PowerToy
;
; Copyright 1996 Microsoft Corporation
;
; Modified to launch VS.NET 2010 command prompt 5/6/03 MG
; Modified to launch VS.NET 2012 command prompt 9/4/12 KZU
; Modified to add context menu to a directory background 9/27/12 SinaIRV
[version]
@sinairv
sinairv / SqlFunctionViewTemplate.sql
Created September 17, 2012 06:54
Template for creating and testing SQL function and view pairs
-- Template for creating and testing SQL function and view pairs
IF EXISTS
(SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'ufnSomeFunction')
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[ufnSomeFunction]
GO
CREATE FUNCTION [dbo].[ufnSomeFunction]
@sinairv
sinairv / SqlViewTemplate.sql
Created September 17, 2012 06:47
Template for creating and testing SQL views
-- Template for creating and testing SQL views
IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'vSomeView')
DROP VIEW [dbo].[vSomeView]
GO
CREATE VIEW [dbo].[vSomeView] AS
SELECT * FROM SomeTable
@sinairv
sinairv / SqlTableFunctionTemplate.sql
Created September 17, 2012 06:43
Template for creating and testing SQL table-valued functions
-- Template for creating and testing SQL table-valued functions
IF EXISTS
(SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'ufnSomeFunction')
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[ufnSomeFunction]
GO
CREATE FUNCTION [dbo].[ufnSomeFunction]
@sinairv
sinairv / MaxOrDefaultPhoneNumber.sql
Created August 6, 2012 06:45
Returning 2 distinct columns from a 3-column result-set
-- We want to return one phone number per each person-id
-- If a phone number is flagged as default it has a higher priority
-- There may be more than one phone numbers be marked as default,
-- or one person may have several phone numbers none of which is marked as default
-- Table Definition
CREATE TABLE [dbo].[PersonPhone](
[PersonId] [int] NOT NULL,
[Phone] [varchar](50) NOT NULL,
@sinairv
sinairv / uspFindAllGuid.sql
Created July 26, 2012 05:20
Finding all instances of a GUID in the current database
CREATE procedure [dbo].[uspFindAllGuid] (@guidToFind uniqueidentifier)
as
SET NOCOUNT ON
-- table variable to store all table and columns containing GUIDs
DECLARE @GuidCols AS Table
(
RowIndex int,
TableName varchar(50),
@sinairv
sinairv / FindAllGuidCols.sql
Created July 26, 2012 01:41
Finding all columns of type uniqueidentifier in all tables
SELECT C.TABLE_NAME, C.COLUMN_NAME FROM
INFORMATION_SCHEMA.COLUMNS C
INNER JOIN INFORMATION_SCHEMA.TABLES T ON C.TABLE_NAME = T.TABLE_NAME AND T.TABLE_TYPE = 'BASE TABLE'
WHERE C.DATA_TYPE = 'uniqueidentifier'
@sinairv
sinairv / CreateTestData.sql
Created July 25, 2012 04:51
Generate test data from values specified for a subset of columns
-- Populate test data from values specified for a subset of columns
USE TestDataBase;
GO
-- think of it as the main table to be filled
CREATE Table #SomeTable
(
ID uniqueidentifier,
Name varchar(50),