Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redshift query between date

I'm quite new to Redshift SQL.

    select *  from myredshift_tbl 
    where local_date between \'2016-01-01\' and \'2017-02-01\'; 

But got this error:

[amazon][500310] invalid operation syntax error at or near "\". I believe Redshift use single quote and I need to escape single quote.

like image 818
newleaf Avatar asked Jul 13 '17 17:07

newleaf


People also ask

How do you find the difference between two dates in redshift?

The DATEDIFF function determines the number of date part boundaries crossed between two date/time expressions. This is perhaps unintuitive; for example, if we pass DATEDIFF(year, '2021-12-31', '2022-01-01') the function will return one year, even though there's only a single day difference.

How do you filter dates in redshift?

select extract(minute from sysdate); -- hour, day, month, year, century select date_part(minute, sysdate); -- hour, day, month, year, century -- returns 0-6 (integer), where 0 is Sunday and 6 is Saturday SELECT extract(dow from sysdate); SELECT extract(dow, sysdate); -- returns a string like monday, tuesday, etc select ...

How do you interval in redshift?

An interval is expressed as a combination of the INTERVAL keyword with a numeric quantity and a supported date part, for example INTERVAL '7 days' or INTERVAL '59 minutes' . You can connect several quantities and units to form a more precise interval, for example: INTERVAL '7 days, 3 hours, 59 minutes' .


2 Answers

If the column local_date is in date format, use:

select *  from myredshift_tbl 
    where local_date between '2016-01-01' and '2017-02-01';

If the column local_date is timestamp:

select *  from myredshift_tbl 
        where local_date between '2016-01-01 00:00:00' and '2017-02-01 23:59:59';
like image 193
Yusuf Hassan Avatar answered Sep 18 '22 17:09

Yusuf Hassan


SELECT * FROM schemaName.TableName WHERE datetime > '2017-02-09 
00:00:00' AND datetime < '2017-06-09 00:00:00';

The above query Works with Redshift to fetch all the entries in a table.

NOTE: The table I applied the query on had column/field 'datetime' of type 'timestamp'.

I tested this query on Redshift with the help of Workbench J.

like image 44
Golokesh Patra Avatar answered Sep 17 '22 17:09

Golokesh Patra