SQL query with operator between and date in a strange format - sql

I have a column in my table that stores a date in format (DD-MM-YY HH:MM:SS). For e.g.:
05-06-15 01:02:03
I need to output for instance all the records that have a date between the 4th and the 5th of June, so i tried:
SELECT * from table where date BETWEEN '04-06-15 00:00:00' AND '05-06-15 23:59:59'
But it also output results with a different month, as:
05-07-15 14:52:34
Is there a way to use a single query for solving this issue or I have to change all my database date format?

SELECT *
from table
where
STR_TO_DATE(date,'%d-%m-%Y %T') between
'2015-06-05 00:00:00' and '2015-06-5 23:59:59';

Related

how to select all entries having date 25-11-20 in oracle 11g?

sql table
here in the table above named carpooling contains a column name start_on which has date time as timestamp i have to write a query to select all the rows having date as 25-11-20 using to_char and to_date.
You write a timestamp literal like this:
timestamp '2020-11-25 00:00:00'
so the full filtering condition will be
where start_on >= timestamp '2020-11-25 00:00:00'
and start_on < timestamp '2020-11-26 00:00:00'
Note that dates and timestamps are different in Oracle, and dates include times down to the second (this is for historical reasons - originally there was only the date type, and timestamp was added much later).
Use the TRUNC function, along with date and interval literals:
SELECT *
FROM CARPOOLING
WHERE START_ON BETWEEN DATE '2020-11-25'
AND (DATE '2020-11-26' - INTERVAL '0.000001' SECOND)
You can simply use to_date, but it's recommended to remove the time when comparing the dates. Otherwise, rows having the same date, but a different time will not be selected. Removing the time can be done using TRUNC.
So you can do something like this:
SELECT * FROM carpooling
WHERE TRUNC(start_on) = TO_DATE('2020-11-25','yyyy.mm.dd');
If you don't want to check the 25th of November 2020, but another data, change the date to match your goal.

Query that will select a timeframe from specific date to today

I'm having issues in my WHERE clause selecting data from a specific day to today's date. The day/time format in my date column is '7/2/2020 3:12:08 PM'.
I've tested a couple options but keep getting this error - 'literal does not match format string'.
Any idea's of how I can select all data from March 1, 2020 to current date?
Thanks!
In Oracle date columns are not strings, they are exactly in date datatype, so you don't need to convert/cast it. Just use simple date literals:
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-8F4B3F82-8821-4071-84D6-FBBA21C05AC1
select * from table where your_date_columg >= date'2015-12-31'
or with to_date function for your string:
select * from table
where
your_date_columg >= to_date('2019-11-25 13:57:52',
'yyyy-mm-dd hh24:mi:ss')

AWS Athena - Format and filter datetime

I have a table which is fed two different date formats:
d/m/Y & m/d/Y. The date format wanted is d/m/Y
I am able to select the date column and do a check and format if the date is in the wrong format.
This is my current SQL query:
SELECT COALESCE(TRY(date_format(date_parse(tbl.date, %d/%m/%Y), %d/%m/%Y)),
TRY(date_format(date_parse(tbl.date, %m/%d/%Y), %d/%m/%Y))) as date
FROM xxx
That fixes the mismatched dates...however I also need to query a date range e.g. the last 7 days.
If I add a WHERE statement it does not execute as I have already queried the date earlier.
How can I format my dates AND filter based on a given range (last 7 days)?
In ANSI SQL -- implemented by Presto, which Athena is based on -- the WHERE clause cannot reference the SELECT projections, so you need a aubquery:
SELECT *
FROM (
SELECT COALESCE(TRY(date_parse ....... AS date
FROM xxx
)
WHERE date > current_date - INTERVAL '7' DAY

SELECT query with LIKE fails

I'm using a SQL SELECT query to bring back all rows from a specific date.
The column I'm using is called TimeStamp (datetime)
(An example of data from this column = 01/02/2018 07:55:55)
What I would like is to return all rows from a specific date eg 24/06/2019
I have tried
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02/01/2018 07:55:55'
which returns one row (which is correct as there is only one row containing this data)
If I then try
SELECT top 20 TimeStamp
from Report
where TimeStamp LIKE '02/01/2018%'
I get no results, I have also tried escaping the forward slashes
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02\/01\/2018%'
Most databases support a string function called left(). If I assume that your "timestamp" is a string, then:
where left(timestamp, 10) = '01/02/2018'
However, it should be stored as a date or date/time. If so, then you can do:
where timestamp >= '2018-02-01' and
timestamp < '2018-02-02'
Note the use of standard formatted dates (YYYY-MM-DD). That is the way most databases implement date literals.
In SQL Server, you can also use:
where convert(date, timestamp) = '2018-02-01'
Both this and the previous version will use an index on timestamp, so both are reasonable solutions.
this should work
SELECT TimeStamp FROM report where convert(Date, TimeStamp) = '2019-06-24'
or select timestamp from report where timestamp between '2019-06-24' and '2019-06-25'. This will get you everything between 2019-06-24 00:00:00 and 2019-06-25 00:00:00 thus all records with date 2019-06-24
Convert timestamp value to date.
SELECT TimeStamp
FROM report
WHERE CAST(TimeStamp AS DATE) = '2019-06-24'

SQL Select Where date record created

Is there a way in SQL to filter a query based on the date a specific record was added if I don't have a field that has the date it was created on.
Something like this:
Select * from Table where Table.recorddate = '2013-01-01'
I think that what you are trying to achieve is not possible. If it's really necessary, just add the field. It'd be easier for you that way.
Use UTC format:
SELECT * FROM Table WHERE recorddate = '2014-01-01'
Note that UTC is YYYY-MM-DD. So Today's date would be:
SELECT * FROM Table WHERE recorddate = '2014-05-19'
If your date field also includes a time stamp you can use a between:
SELECT * FROM Table
WHERE recorddate BETWEEN '2014-05-19 00:00:00' AND '2014-05-19 23:59:59'