Skip to content

Instantly share code, notes, and snippets.

View tamiroze's full-sized avatar

Tamir Rozenberg tamiroze

View GitHub Profile
select *
from information_schema.tables
where table_name= upper('table_name');
# Instead make sure you send the string in UPPER case. Dont use the upper function.
select *
from information_schema.tables
where table_name= 'TABLE_NAME';
@tamiroze
tamiroze / show_sf_parameters.sql
Last active February 16, 2021 03:18
show Snowflake account timezone
show parameters like '%TIMEZONE%' in account;
#change your account setup in case you want a different timezone
alter account SET TIMEZONE = 'UTC';
@tamiroze
tamiroze / sf_copy_command.py
Created June 20, 2019 21:38
sample of copy command with date issue
copy into {0}{1}
from {2}
truncatecolumns = true
file_format =
(
field_optionally_enclosed_by='"'
escape_unenclosed_field='NONE'
type = csv
TRIM_SPACE = TRUE
field_delimiter = '|'
Redshift Snowflake
json_extract_path_text (properties,'name') parse_json(properties):name
select json_extract_array_element_text('[111,112,113]', 2); select parse_json('[111,112,113]')[2];
Redshift Snowflake
DELETE customers DELETE from customers
@tamiroze
tamiroze / rs_to_sf.md
Last active February 16, 2021 03:13
sql syntax differences between Redshift and Snowflake
Redshift Snowflake
select GETDATE() select CURRENT_TIMESTAMP()
select TRUNC(CURRENT_TIMESTAMP()) select DATE_TRUNC(DAY,CURRENT_TIMESTAMP())
select SYSDATE select TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC',CURRENT_TIMESTAMP()))
select DATE(CURRENT_TIMESTAMP()) select TO_DATE(CURRENT_TIMESTAMP())
date_time_utc > GETDATE() - 7 date_time_utc > dateadd('DAY', -7, TO_TIMESTAMP_NTZ(LOCALTIMESTAMP))
select coalesce('a') select coalesce('a', 'b') --you will get error message if you specify only one argument "SQL compilation error: error line 1 at position 7 not enough arguments for function [COALESCE('a')], expected 2, got 1"
DISTSTYLE not used
DISTKEY not used
@tamiroze
tamiroze / run_data_migration.py
Last active February 16, 2021 03:12
Get Redshift tables
def run_data_migration(self, schema_name, table_name):
self.logger.info("querying redshift metadata")
pg_conn = pg.connect(cfg.REDSHIFT_DB_URL)
print "pg_conn:", pg_conn
pg_cursor = pg_conn.cursor()
if table_name != "none":
sql = """
@tamiroze
tamiroze / sql2sf.py
Last active May 13, 2024 20:06
Converts Oracle, SQL-Server, and other DDL to Snowflake DDL
#!/usr/bin/python
# $Id: $
# Converts Oracle, SQL-Server, and other DDL to Snowflake DDL
def usage():
print """\
# Usage: sql2sf.py input-file [output-file]
"""
@tamiroze
tamiroze / redshift_generate_ddl.py
Last active February 16, 2021 03:13
Generate redshift ddl
def get_table_ddl(self, table_name, schema_name):
self.logger.info("querying redshift metadata")
pg_conn = pg.connect(cfg.REDSHIFT_DB_URL)
pg_cursor = pg_conn.cursor()
sql = """
select ddl
from vw_admin_generate_table_ddl
where tablename='{0}'
and schemaname='{1}' """.format(