I have two timestamps in String format 2015-05-06T15:39:00
and 2015-04-06T15:39:00
.
What is the sql query for Oracle that I can query all the records in the table that has timestamp that falls within this range.
I have two timestamps in String format 2015-05-06T15:39:00
and 2015-04-06T15:39:00
.
What is the sql query for Oracle that I can query all the records in the table that has timestamp that falls within this range.
And with alternative way you can use between
SELECT *
FROM tab1
WHERE timestamps BETWEEN TO_DATE ('2015-05-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS') AND TO_DATE('2015-04-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS');
SELECT *
FROM yourTable
WHERE timestamps >= TO_DATE('2015-05-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS')
AND timestamps <= TO_DATE('2015-04-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS')
You need to convert the literal into DATE using TO_DATE and required format mask to compare the timestamp column with the input timestamp values.
Setup
SQL> CREATE TABLE t(A TIMESTAMP);
Table created.
SQL>
SQL> INSERT INTO t(A) VALUES(to_date('2015-04-10T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS'));
1 row created.
SQL> INSERT INTO t(A) VALUES(to_date('2015-05-01T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS'));
1 row created.
SQL> INSERT INTO t(A) VALUES(to_date('2015-03-01T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS'));
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM t;
A
----------------------------
10-APR-15 03.39.00.000000 PM
01-MAY-15 03.39.00.000000 PM
01-MAR-15 03.39.00.000000 PM
Query
SQL> SELECT *
2 FROM t
3 WHERE A BETWEEN
4 to_date('2015-04-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS')
5 AND
6 to_date('2015-05-06T15:39:00', 'YYYY-MM-DD"T"HH24:MI:SS');
A
--------------------------------------------------------------------------
10-APR-15 03.39.00.000000 PM
01-MAY-15 03.39.00.000000 PM
So, I got the required rows as my desired output.
None of the above answers worked for me.
however, First simply replace 'T' you have with a ' '(space) and use below query which worked for me
select * from tb1
where timestamps BETWEEN TO_DATE ('2015-05-06 15:39:00', 'YYYY-mm-dd HH24:MI:SS')
AND TO_DATE('2015-04-06 15:39:00', 'YYYY-mm-dd HH24:MI:SS');
The below one is for timestamp and you can change for your required time
SELECT *
FROM tbl1
WHERE timestamp BETWEEN to_date('21/11/2017 23:59:59','dd/MM/rrrr hh24:mi:ss')
AND to_date('21/12/2017 15:59:59','dd/MM/rrrr hh24:mi:ss');`