How to get condition with last day in Oracle SQL query - sql

I am trying to write the follwoing query but getting an error. Would apprecite if someone help me.
select Ldate from Dates
where Ldate >=LAST_DAY(SYSDATE)-1
The database date format is in string
select Ldate from Dates where Ldate >=LAST_DAY(SYSDATE)-1

Don't store dates as string; use a DATE then your query would work.
If you do store it as a string (please don't) then convert it to a date in the query:
select Ldate
from Dates
where TO_DATE(Ldate, 'YYYY-MM-DD') >= LAST_DAY(TRUNC(SYSDATE))
Note: update the 'YYYY-MM-DD' format model to match your date format.

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

Getting Date Range

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

In oracle SQL how to get todays date in the format of lets say 01-JAN-15?

Just want to know In oracle SQL how to get todays date in the format of lets say 01-JAN-15?
So it should return 06-DEC-15
Use to_char(). This is how you convert dates to a string with any format you like:
select to_char(sysdate, 'DD-MON-YY')
The explanation of the format is here.

Formatting dates in PostgreSQL

I have a field which its format is date with time as: YYYY-MM-DD HH-MM-SS for example: 2000-08-12 00:00:00 I want to get just the date part and change its format to DD/MMM/YYYY for example the expected result of the previous example will be: 12/Aug/2000
The field definition is: Ddate timestamp without time zone DEFAULT now()
I read the whole page of Date/Time Functions and Operators and other sources as well but I couldn't find any information that is helpful.
You can use the to_char function to format your column:
SELECT TO_CHAR(ddatte, 'dd/Mon/yyyy') FROM mytable
try with:
to_char(your_Field, 'dd/mm/yyyy')