Skip to content

Instantly share code, notes, and snippets.

@dingjing
dingjing / Holiday.t.sql
Last active August 29, 2015 14:15
Calculate holidays in SQL Server.
with seq (yr, jan1, jul4) as (
select
2001 as yr,
cast('2001-01-01' as datetime) as jan1,
cast('2001-07-04' as datetime) as jul4
union all
select
yr + 1,
dateadd(year, 1, jan1),
dateadd(year, 1, jul4)
@dingjing
dingjing / Generate_Row.t.sql
Last active August 29, 2015 14:15
Generate rows and sequence data in SQL Server
declare @row_count int = 100;
with seq (s, d, c) as (
select
1 as s,
cast('2000-01-01' as datetime) as d,
char(1) as c
union all
select
s + 1,
@dingjing
dingjing / Generate_Row.oracle.sql
Created February 8, 2015 22:19
Generate rows and sequence data in Oracle
define row_count = "100"
select
level as s,
to_date((2000+level)*10000 + 0101, 'yyyymmdd') as d,
chr(level) as c
from dual
connect by level <= &row_count;
@dingjing
dingjing / Fiscal.t.sql
Last active August 29, 2015 14:15
Calculate fiscal year in SQL Server.
declare @shift int = 6
select
test_dt,
concat(year(dateadd(month, -@shift, test_dt)), '-', year(dateadd(month, 12-@shift, test_dt))) as fisc_year,
dateadd(month, @shift, datename(year, dateadd(month, -@shift, test_dt)) + '0101') as fisc_start
from (values
(getdate()),
(cast('2014-03-31' as date)),
(cast('2014-04-01' as date)),
@dingjing
dingjing / Fiscal.oracle.sql
Created February 8, 2015 18:18
Calculate fiscal year in Oracle
define shift = "6"
with v as (
select
trunc(sysdate - floor(dbms_random.value()*1000)) as test_dt
from dual
connect by level <= 10
)
select
test_dt,
@dingjing
dingjing / Observed_Holiday.sql
Last active August 29, 2015 14:14
Calculate observed holidays in SQL
with yr_tbl as (
select
2000 + level as yr,
to_date(101 + (2000+level)*10000, 'yyyymmdd') as jan1,
to_date(704 + (2000+level)*10000, 'yyyymmdd') as jul4
from dual
connect by level <= 100
)
select
yr,
@dingjing
dingjing / daylight_savings_change.sql
Created December 28, 2014 02:36
Daylight savings time change
with utc_tm as (
select to_timestamp_tz('1970-01-01 00:00:00 +00:00') + numtodsinterval(level - 1, 'hour') as utc_tm
from dual connect by level <= 1000000
), utc_lcl as (
select
utc_tm,
cast(utc_tm as timestamp with local time zone) as lcl_tm,
(cast(utc_tm as date) - cast(cast(utc_tm as timestamp with local time zone) as date))*24 as interval_h
from utc_tm
), interval_compare as (
@dingjing
dingjing / multiline_update.sql
Created August 26, 2014 03:06
Generate UPDATE SQL without using LISTAGG()
with col_info as (
select
c.owner,
c.table_name,
c.column_name,
c.column_id,
pc.column_name as PK_COL
from
all_tab_cols c join all_constraints p on
c.owner = p.owner and
@dingjing
dingjing / multiline_insert.sql
Created August 26, 2014 03:03
Generate insert SQL without using LISTAGG
with col_info as (
select
c.owner,
c.table_name,
c.column_name,
c.column_id,
pc.column_name as PK_COL,
max(c.column_id) over() as max_id
from
all_tab_cols c join all_constraints p on
@dingjing
dingjing / branch-promise.js
Last active August 29, 2015 13:56
Promise version of branch example.
function doTask1(){
// Create a new process
var defer = $.Deferred();
// Start main task in the process
setTimeout(function(){
var rslt = Math.floor(Math.random() * 10);
log("Task 1 returned " + rslt);
// Main task is finished.