Skip to content

Instantly share code, notes, and snippets.

View clintmjohnson's full-sized avatar

Clint Johnson clintmjohnson

View GitHub Profile
@clintmjohnson
clintmjohnson / IsThisStringaDate.SQL
Created May 21, 2015 22:31
Determine whether a String is a Valid Date or Not SQL SERVER
SELECT MyData ,
ISDATE(MyData) AS IsADate
FROM (VALUES('IsThisADate'),
('2012-02-14'),
('2012-01-01T00:00:00'),
('2012-12-31T23:59:59.9999999')) dt(MyData)
@clintmjohnson
clintmjohnson / LastDayofMonth.sql
Created May 21, 2015 22:32
Determines the Last Day of a Month EOMONTH SQL SERVER
SELECT MyData,
EOMONTH(MyData) AS LastDayOfThisMonth,
EOMONTH(MyData, 1) AS LastDayOfNextMonth
FROM (VALUES ('2012-02-14T00:00:00' ),
('2012-01-01T00:00:00'),
('2012-12-31T23:59:59.9999999')) dt(MyData)
@clintmjohnson
clintmjohnson / UntangleExample.py
Created May 29, 2015 18:17
This is an XML Untangle Example
#!/usr/bin/env python
"""
Usage examples for untangle
"""
import untangle
def access():
@clintmjohnson
clintmjohnson / blazeexample.py
Created May 30, 2015 04:05
Python Blaze Example List Computation
from blaze import *
accounts = Symbol('accounts', 'var * {id: int, name: string, amount: int}')
deadbeats = accounts[accounts.amount < 100].name
L = [[1, 'Alice', 100],
[2, 'Bob', -200],
[3, 'Charlie', 300],
[4, 'Denis', 400],
[5, 'Edith', -500]]
@clintmjohnson
clintmjohnson / MMDDYYY_2_SQLDate.sql
Created June 1, 2015 21:09
Convert MMDDYYYY to SQL Server Date Type Function
CREATE FUNCTION [dbo].[MMDDYYYY_TO_SQL_DATE]
(
@MMDDYYYY char(8)
)
RETURNS date
AS
BEGIN
@clintmjohnson
clintmjohnson / filterduplicates.sql
Last active August 29, 2015 14:23
This Query Filters a SQL Server Table to only Dupicate Values
select o.model_number, oc.model_number
from Product_Staging_Table o
inner join (
SELECT model_number, COUNT(*) AS dupeCount
FROM Product_Staging_Table
GROUP BY model_number
HAVING COUNT(*) > 1
) oc on o.model_number = oc.model_number
WHERE o.model_number <> '' AND o.model_number IS NOT NULL
@clintmjohnson
clintmjohnson / DeleteonebyOne.sql
Created June 14, 2015 13:57
Delete Duplicate Key Id Values on a SQL Server Table One by One
USE TableName
MERGE TOP (1) Product_Master_Table AS a
USING
(SELECT model_number,COUNT(*) AS dupeCount
FROM Product_Staging_Table
WHERE model_number <> '' AND model_number IS NOT NULL
GROUP BY model_number
HAVING COUNT(*) > 1) AS b
@clintmjohnson
clintmjohnson / Merge_From_File.txt
Last active August 29, 2015 14:23
SQL Server Merge From File Such as CSV Using OPENROWSET
SELECT *
FROM OPENROWSET(BULK 'c:\temp\Customers.txt',
FORMATFILE = 'c:\temp\CustomersFmt.xml') AS SRC;
SELECT *
FROM OPENROWSET(BULK 'c:\temp\Customers.txt',
FORMATFILE = 'c:\temp\CustomersFmt.xml') AS SRC;
Similarly, the OPENROWSET function can be used directly in the USING clause of the MERGE statement, like so:
MERGE INTO Sales.MyCustomers AS TGT
USING OPENROWSET(BULK 'c:\temp\Customers.txt',
FORMATFILE = 'c:\temp\CustomersFmt.xml') AS SRC
@clintmjohnson
clintmjohnson / CASE_SUB_QUERIES.sql
Last active August 29, 2015 14:23
SQL Server Searched CASE With Sub Queries Very Powerful
SELECT au_lname, au_fname, title, Category =
CASE
WHEN (SELECT AVG(royaltyper) FROM titleauthor ta
WHERE t.title_id = ta.title_id) > 65
THEN 'Very High'
WHEN (SELECT AVG(royaltyper) FROM titleauthor ta
WHERE t.title_id = ta.title_id)
BETWEEN 55 and 64
THEN 'High'
WHEN (SELECT AVG(royaltyper) FROM titleauthor ta
#!/usr/bin/python
import imaplib
import csv
from email import message_from_string
import time
srv = imaplib.IMAP4_SSL("imap.gmail.com")
srv.login('joe@gmail.com', 'joepw')
srv.select('[Gmail]/Drafts')