Skip to content

Instantly share code, notes, and snippets.

View jbnv's full-sized avatar
🙂
Life is good.

Jay Bienvenu jbnv

🙂
Life is good.
View GitHub Profile
@jbnv
jbnv / English Names Query
Created June 2, 2015 00:45
Query to generate a number of English names for data mocking
/*
The referenced views (which could also be tables) are one-column views
(or tables) that contain instances of the particular type of name. For
[EnglishMaleFirstNameView] and [EnglishFemaleFirstNameView], the one
column must be named [Name]. For [EnglishSurnamePrefixView], the one
column must be named [Prefix]. This view can contain surnames that would
otherwise stand alone. For [EnglishSurnameSufffixView], the one column
must be named [Suffix]. Each element of this list must not be
capitalized. This list should contain an empty string element so that
the prefix can stand alone. Examples of each of these views are
@jbnv
jbnv / Numbers Table
Last active December 28, 2015 22:46
Creates a table containing all integers between 0 and 2^20-1 inclusive. (Platform independent.)
CREATE TABLE Numbers
(
N INT NOT NULL,
CONSTRAINT PK_Numbers
PRIMARY KEY CLUSTERED (N)
WITH FILLFACTOR = 100
)
INSERT INTO Numbers (N)
SELECT ROW_NUMBER() OVER(ORDER BY a.n,b.n,c.n,d.n,e.n,f.n,g.n,h.n,i.n,j.n) AS [N]
@jbnv
jbnv / Split Function
Created June 2, 2015 00:48
Function for SQL Server that splits a delimited string into a table of items.
CREATE FUNCTION [dbo].[Split] (
@List nvarchar(MAX),
@Delimiter nvarchar(3)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN (
SELECT ROW_NUMBER() OVER(ORDER BY N) AS [Ordinal],
SUBSTRING(@List, N, CHARINDEX(@Delimiter, @List + @Delimiter, N) - N) AS [Item]
@jbnv
jbnv / Object Code Query
Created June 2, 2015 00:51
SQL Server query to return the code for an object by name.
SELECT [Item]
FROM sys.sql_modules
CROSS APPLY (
SELECT ROW_NUMBER() OVER(ORDER BY N) AS [Ordinal],
SUBSTRING([definition], N, CHARINDEX(CHAR(13), [definition] + CHAR(13), N) - N) AS [Item]
FROM dbo.Numbers
WHERE N <= CONVERT(INT, LEN([definition]))
AND SUBSTRING(CHAR(13) + [definition], N, 1) = CHAR(13)
) t
WHERE OBJECT_NAME(object_id) = 'OBJECTNAME'
@jbnv
jbnv / Recency.js
Created June 2, 2015 00:55
Recency compare function (JavaScript)
// a, b: Date-time value (null if unknown or "never")
// altCompare: An alternative comparison function(a,b).
function recencyCompare(a,b) {
if (!a && !b) return altCompare(a,b);
if (!a) return 1; // 'b' has an error and 'a' doesn't
if (!b) return -1; // 'a' has an error and 'b' doesn't
if (a == b) { return altCompare(a,b); }
return a < b ? 1 : -1;
}
@jbnv
jbnv / ago.js
Created June 2, 2015 01:03
ago(date) function to return a brief "ago" string.
var second = 1e3;
var minute = 6e4;
var hour = 36e5;
var day = 864e5;
var week = 6048e5;
function ago(diff) {
var unit, num;
if (diff <= second) {
unit = 's';
@jbnv
jbnv / NameGenPostgreSQLEnglish.sql
Created June 2, 2015 01:07
Name Generator (PostgreSQL; English)
SELECT
arrays.firstnames[s.a % ARRAY_LENGTH(arrays.firstnames,1) + 1] AS firstname,
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ' from s.a%26+1 for 1) AS middlename,
arrays.lastnames[s.a % ARRAY_LENGTH(arrays.lastnames,1) + 1] AS lastname
FROM generate_series(1,300) AS s(a) -- number of names to generate
CROSS JOIN(
SELECT ARRAY[
'Adam','Bill','Bob','Calvin','Donald','Dwight','Frank','Fred','George','Howard',
'James','John','Jacob','Jack','Martin','Matthew','Max','Michael',
'Paul','Peter','Phil','Roland','Ronald','Samuel','Steve','Theo','Warren','William',
@jbnv
jbnv / NameGenSSSpanish.sql
Created June 2, 2015 01:13
Name Generator (SQL Server; Spanish)
SELECT
ROW_NUMBER() OVER (ORDER BY surnames.[Surname],firstNames.[FirstName]) AS [Ordinal],
firstNames.[Gender],
firstNames.[FirstName],
surnames.[Surname]
FROM (
SELECT 'M' AS [Gender], [FirstName]
FROM (
SELECT 'Alberto' AS [FirstName]
UNION SELECT 'Angel' UNION SELECT 'Benito' UNION SELECT 'Carlos' UNION SELECT 'Domingo' UNION SELECT 'Fernando' UNION SELECT 'Federico' UNION SELECT 'Fidel'
@jbnv
jbnv / console.js
Created June 2, 2015 01:38
'console' custom binding for Knockout. Lets us dump a variable to the console from within our HTML.
composition.addBindingHandler('console', {
update: function (element, valueAccessor, allBindings) {
data = ko.toJS(valueAccessor() || allBindings());
console.log(data);
}
});
@jbnv
jbnv / CollectaAPI.vb
Created June 2, 2015 01:42
Collecta API Visual Basic .Net 3.5 Interface
Option Explicit On
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Net
Imports System.Xml
Imports System.IO
Namespace Collecta