Getting Date Range - sql

I want to get all the information between two given dates, How can I get a range of Date in SQL server with the datatype of "smalldatetime"
I'm expecting to get all the information start from start date to end date

I think you are looking for this :
SELECT *
from your_table
where date BETWEEN start_date AND end_date

Related

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

Get Records By Todays Date And Before Current Time

I am trying to query some records and am looking to return only records that have a date of today, but also are before the current time.
I have the first part sorted out by using the following clause to return only records for today.
WHERE TRUNC(my_date_time) = TRUNC(sysdate)
How would I modify this to only get records before the current system time as well?
How about inequalities?
WHERE my_date_time >= TRUNC(sysdate) AND
my_date_time < sysdate
Try this.
Select * from table Where
TRUNC(my_date_time) =
TRUNC(sysdate) And
TO_CHAR(my_date_time,
'HH24:MI:SS' ) <=
TO_CHAR(sysdate,
' HH24:MI:SS' )
If column "my_date_time" is of type DATE, and need compare using SYSDATE function then you only need:
WHERE my_date_time<=sysdate
If column "my_date_time" is of type DATE, and need compare with other variable or column of type DATE for example named "other_date"
where my_date_time>=trunc(other_date)
and my_date_time<trunc(other_date)+1

Date range in select

I have this table
CREATE TABLE Table1 (
MY_ID INTEGER NOT NULL,
SCAN_TIME TIMESTAMP NOT NULL,
WORKCELL_ID SMALLINT
);
My problem when I use select SQL to get records between two dates I get empty result if the user select the same date for the two dates
For ex:
Select * from Table1
where SCAN_TIME between '20.9.2014' and '20.9.2014'
Although there are records in that date '20.9.2014' but I get empty result and I get results only if I increase the second date by one day to be '21.9.2014' !! why is that and how I should fix ?
The problem is that you used timestamp as the type, and things messed up due to the time portion. Try to use Date.
When you said between 2014-09-20 and 2014-09-20, you are actually saying between 2014-09-20 midnight and 2014-09-20 midnight, apparently nothing falls in between other than the exact moment of midnight.
If you cannot control the table structure, do what PM 77-1 suggested in his comment below - use cast to cast timestamp into date:
select 1 from table1
where cast(scan_time as date) between 'someday1' and 'someday2';
A TIMESTAMP contains both a date and a time. Since you're not specifying the time in your WHERE clause, it's using the beginning of the day for both times. So you're actually writing the equivalent of:
WHERE SCAN_TIME between '20.9.2014 00:00:00' and '20.9.2014 00:00:00'
Unless the time part of the timestamp is actually 00:00:00, it won't satisfy this criterion. You should convert the timestamp to a date, or specify the times in your range, e.g.
WHERE SCAN_TIME between '20.9.2014 00:00:00' and '20.9.2014 23:59:59'

Retrieve previous month from date column that contains MONYYYY

I need to change a date value that is coming from a table in the form of May2013 (MonYYYY). The column itself is a VARCHAR
In my select statement, I am looking to retrieve the previous month (Apr2013). I've done some research and found the following, if I were using SYSDATE:
select to_date(add_months(sysdate, 'MONYYY')-1) from dual
How do I make it work for the date structure I have above? I've tried:
select to_date(add_months(date.datetable, MONYYY)-1) from datetable
ADD_MONTHS function needs a date variable as input. So, first you need to convert your varchar column to date type and then apply the add_months function.
SELECT ADD_MONTHS (TO_DATE ('May2013', 'monyyyy'), -1) FROM DUAL;
The return type will be date. In this case, it returns 0th hour of first day of April.