Skip to content

Instantly share code, notes, and snippets.

@markmo
markmo / snippets.js
Created February 7, 2013 22:54
Validate email
var validateEmail = function (email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
};
@markmo
markmo / gist:5138203
Created March 11, 2013 21:59
Convert date to integer key
CONVERT(INT, (SELECT CONVERT(VARCHAR(8), SALE_TIMESTAMP, 112) AS [YYYYMMDD])) AS DateKey
@markmo
markmo / gist:5138724
Created March 11, 2013 23:01
Convert integer key to datetime
SELECT CONVERT(DATETIME, CONVERT(VARCHAR, DateKey) + ' ' +
SUBSTRING(RIGHT('000000' + CONVERT(VARCHAR, DateKey), 6), 1, 2) + ':' +
SUBSTRING(RIGHT('000000' + CONVERT(VARCHAR, TimeKey), 6), 3, 2) + ':' +
SUBSTRING(RIGHT('000000' + CONVERT(VARCHAR, TimeKey), 6), 5, 2))
@markmo
markmo / gist:5193225
Created March 19, 2013 02:21
Create Tally Table - useful for removing and creating duplicates
--===== Create and populate the Tally table on the fly
SELECT TOP 11000 --equates to more than 30 years of dates
IDENTITY(INT,1,1) AS N
INTO dbo.Tally
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2
--===== Add a Primary Key to maximize performance
ALTER TABLE dbo.Tally
ADD CONSTRAINT PK_Tally_N
@markmo
markmo / gist:5193235
Created March 19, 2013 02:22
Update Parent Key in Hierarchical Set
UPDATE RetDay.dimProductCategoryHierarchy SET
ParentCategoryKey = (
SELECT TOP 1 ProductCategoryMappingKey
FROM RetDay.dimProductCategoryHierarchy
WHERE ProductCategoryCode = P.ParentCategoryCode
AND DETL_RowIsCurrent = 1)
FROM RetDay.dimProductCategoryHierarchy P
WHERE P.ParentCategoryKey IS NULL
AND P.ParentCategoryCode <> ' '
AND P.ParentCategoryCode IS NOT NULL
@markmo
markmo / gist:5193259
Created March 19, 2013 02:28
Recreate dups
INSERT INTO X (...)
SELECT ...
FROM (
SELECT <group by keys>, COUNT(*) - 1 AS K
FROM <table with dups>
WHERE ...
GROUP BY <group by keys>
HAVING COUNT(*) > 1
) A
JOIN Tally on Tally.N <= A.K
@markmo
markmo / gist:5193281
Created March 19, 2013 02:32
Initialize with key
SET IDENTITY_INSERT <table to init> ON
GO
INSERT INTO <table to init>(...)
VALUES (...including key)
SET IDENTITY_INSERT <table to init> OFF
GO
@markmo
markmo / gist:5193294
Created March 19, 2013 02:36
Convert Time to Hour Block
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Mark Moloney
-- Create date: 4/10/2012
-- Description: Convert a time to the appropriate hour block
-- =============================================
CREATE FUNCTION ConvertTimeToHourBlock
@markmo
markmo / gist:3dd46f187b6bce499701
Created October 27, 2015 21:12
Convert Iterator to Iterable in Java
private static <T> Iterable<T> once(final Iterator<T> source) {
return new Iterable<T>() {
private AtomicBoolean exhausted = new AtomicBoolean();
@Override
public Iterator<T> iterator() {
Preconditions.checkState(!exhausted.getAndSet(true));
return source;
}
};
}
package diamond.models
import java.util.Date
/**
* @param entity String entity id (usually hashed)
* @param eventType String attribute name
* @param ts Date event timestamp
* @param namespace String logical grouping
* @param session Option[String] session id