SQL Query taking between specific sysdate? - sql

I am using Oracle DB 10g, I am trying to take data between 2 dates, in the database format of data is :
10/4/2013 9:04:38 AM
I try some sql queries but it gives error...
select * from test_table where test_execution_date between '9/2/2012' and '7/2/2013'
select * from test_table where test_execution_date between '9/2/2012 9:04:38 AM' and '7/2/2013 9:04:38 AM'
It gives always same error : ORA-01843: not a valid month

Try using
TO_DATE(thedatevalue,'MM/DD/YYYY')

You need to convert string to date
select * from test_table where test_execution_date between
TO_DATE('9/2/2012', 'DD/MM/YYYY') and TO_DATE('7/2/2013', 'DD/MM/YYYY')

Related

Laravel, query where created_at format

In PostgreSQL and Laravel 5.1
I have this output query from Query Builder (raw):
select *
from mytable
// some joins and stuff
where to_char(mytable.created_at, 'DD-MM-YYYY') = '20-06-2019'
I'm getting 0 values because that last line where to_char(mytable.created_at, 'DD-MM-YYYY') = '2019-06-20' is wrong.
created_at has this format: '2019-06-20 21:00:12'
How can I query with that time '20-06-2019' in my table?
You can convert your char date into DATE and truncate date which you are getting from table can use below query
select *
from mytable
// some joins and stuff
where trunc(mytable.created_at) = to_date('20-06-2019','dd-mm-yyyy')

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.
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';
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.

Google BigQuery Date(String format) failed to convert/cast as Date

I am trying to convert/cast the date in the big query into date format. My query is like this:
SELECT CAST(t.date AS date)
FROM `table` t;
But I got an Error code of Invalid date:'20151108'. it gave me different error date when I run the query.
Any thoughts?
try
SELECT PARSE_DATE('%Y%m%d', t.date) FROM table t

How to convert Timestamp to Date Data Type in Google Bigquery

I am trying to convert Timestamp data type columns to Date datatype using:
bq query -q --destination_table=NEW_DATE_TABLE --replace "SELECT DATE(CURR_DT) AS CURR_DT from TEST.DATE_TABLE"
The new table shows the column as STRING rather than date. Is there a way to convert timestamp to date data type.
Requested Screenshot
If you use Standard SQL, you can do the following:
SELECT * REPLACE(EXTRACT(DATE FROM curr_dt)) AS curr_dt FROM test.date_table
If curr_dt is repeated field, then the solution will look the following:
SELECT * REPLACE(
ARRAY(
SELECT EXTRACT(DATE FROM curr_dt) FROM t.curr_dt
) AS curr_dt)
FROM test.date_table t
Consider below!
Works in both Legacy and Standard SQL
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT FROM TEST.DATE_TABLE
Added to address comment
Try below - as I mentioned above - it works for both Legacy and Standard
SELECT CAST(DATE(CURR_DT) AS DATE) AS CURR_DT
FROM (SELECT CURRENT_TIMESTAMP() AS CURR_DT)
if you are interested in making your case working with Legacy SQL - provide more details about CURR_DT field
Try this
SELECT TIMESTAMP_SECONDS(CAST(CURR_DT AS INT64)) AS CURR_DT FROM TEST.DATE_TABLE

How to query oracle dates properly?

select * from mytable where datecolumn == to_date('01-DEC-13','DD-MMM-YY')
I am getting the "ORA-01821: date format not recognized"
Where I do a query for a select * from mytable, I see that the values in datecolumn show up like "01-DEC-13" What am I doing wrong?
I'd recommend using the full century for dates. I've dealt with plenty of databases where somebody messed up the centuries because they didn't understand Oracle or the front-end language. Consider this for your 01-DEC-13 date:
CREATE TABLE LousyDates (DateVal DATE);
INSERT INTO LousyDates VALUES (DATE '2013-12-01');
INSERT INTO LousyDates VALUES (DATE '1713-12-01');
INSERT INTO LousyDates VALUES (TO_DATE('12/1/13 BC', 'MM/DD/YYYY BC'));
SELECT
TO_CHAR(DateVal, 'DD-MON-YY') AS NoCentury,
TO_CHAR(DateVal, 'MM/DD/YYYY BC') AS WithCentury
FROM LousyDates;
NOCENTURY WITHCENTURY
------------------ -------------
01-DEC-13 12/01/2013 AD
01-DEC-13 12/01/1713 AD
01-DEC-13 12/01/0013 BC
OK, maybe I'm overstating the point with the 13 BC date, but you get the idea :)
To use the century...
SELECT * FROM MyTable WHERE datecolumn = TO_DATE('01-DEC-2013', 'DD-MON-YYYY')
or
SELECT * FROM MyTable WHERE datecolumn = DATE '2013-12-01'
Unless you're 100% sure you will never ever use different language settings, I'd recommend using MM instead of MON:
select * from mytable where datecolumn = to_date('2013-12-01', 'YYYY-MM-DD')
This will work with any NLS settings, whereas comparing the output of to_date(...MON...) with 'DEC' will fail utterly on different locales.
Try this:
SELECT * from MYTABLE WHERE TO_CHAR(datecolumn,'DD-MON-YY') == '01-DEC-13'
instead of converting your wildcard, try to convert your datetime.