SELECT dates within one season (between two dates, of any year) - sql

Is it possible in Oracle SQL to select rows where one column is a date, and we want this date to be between two dates irrespective of the year.
For example if the interval is summer, i'd want to be able to have any dates bewteen 20th of june and 22nd of september, where the year can be 1999, 2000, 2018, 2897 etc.

You can convert to a string of the form MM-DD and compare that:
where to_char(col, 'MM-DD') between '06-20' and '09-22'

Related

SQL filter by two date columns in the same month

In my dataset I have column A and column B, they have different dates, sometimes one column has a date and the other don't, sometimes both have values but different months. I want to filter by the date when they are in the January.
I want both columns to have date of January '2023-01-01'
I started with
SELECT *
FROM table
WHERE DATE_TRUNC(SAFE_CAST(column_a AS DATE), MONTH) = '2023-01-01'
OR DATE_TRUNC(column_b, MONTH) = '2023-01-01'
The problem is that in some cases the column_a has a date prior January due to the or. And both dates need to be in the some month.
How can I solve that?

Specific Month-day select statement query

I would like to select from a table where the date falls within a specific time each year f.e:
select * from Customer where date >= August 15th and date <= December 20th
Since this will be for a report that runs every year, I do not want to hardcore the date as I will have to change it every year. I would like to have it dynamic to pick the date from August 15th to December 20th of the current year.
I have the below query where I can retrieve the Month and Date:
SELECT DATENAME(month, date) AS Month,DATENAME(day, date) AS Day from Customer
However, I am struggling to have this selection date range.
TIA
In SQL SERVER 2017
maybe this can help you:
SELECT * FROM Customer WHERE date BETWEEN
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'08','15')) AND
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'12','20'))
This add the current year to a concatenated date you want, then convert it all into datetime type..

Retrieve specific year in the format of "2019-05-04 11:20:22.697" in SQL query

How can I retrieve specific year in the format of “2019-05-04 11:20:22.697” in SQL query? I need the date between 2019 and 2020.
select * from date where date between '2019-01-01' and '2020-01-01'
If you want a query to just target the year 2019, then use:
SELECT *
FROM yourTable
WHERE date >= '2019-01-01' AND date < '2020-01-01';
This will include the entire 2019 calendar year, up to, but not including, January 1 of 2020. Note also that the above WHERE clause, as written, is sargable, meaning that the above query could take advantage of an index on the date column for doing the search.

How to get year, month and day from seconds in PostgreSql?

I'm trying to create three columns based on date in seconds format.
My user.updated_at = 1521533490
I would like to get year, month and day separately and put these formatted values to columns for example:
year -> 2018, month -> 11, day -> 23
Does someone know how can I do that in pgSQL?
I would like to get year, month and day separately and put these formated values to columns
Don't do that.
Use a single column of type date or timestamp, depending on your application. Not every combination of your three columns will be a valid date. But every value in a single column of type date will be a valid date.
If you need the parts of a date separately, use PostgreSQL's date/time functions.
Try this approche to get differents arguments, then you can do whatever you want:
SELECT to_timestamp(1521533490); //2018-03-20T08:11:30.000Z
SELECT to_char(to_timestamp(1521533490), 'HH'); // 08 Hour
SELECT to_char(to_timestamp(1521533490), 'MI'); // 11 Minutes
SELECT to_char(to_timestamp(1521533490), 'SS'); // 30 Seconds
SELECT to_char(to_timestamp(1521533490), 'DD'); // 20 Day
SELECT to_char(to_timestamp(1521533490), 'Mon'); // MAR Month
SELECT to_char(to_timestamp(1521533490), 'YYYY'); // 2018 Year
Use the EXTRACT function.
SELECT to_timestamp(updated_at) "Date",
EXTRACT(YEAR FROM (to_timestamp(updated_at))) "Year",
EXTRACT(MONTH FROM (to_timestamp(updated_at))) "Month",
EXTRACT(DAY FROM (to_timestamp(updated_at))) "Day"
FROM users
Output
Date Year Month Day
2018-03-20T08:11:30Z 2018 3 20
SQL Fiddle: http://sqlfiddle.com/#!15/afe0e/15/0
More information on the EXTRACT function.

Oracle date column value as 0000

We need to store 00-00-00 in date datatype in Oracle column. But Oracle doesn't allow.
Are there alternate representations or ways to store it?
Oracle does allow you do insert the date 1st January 1AD:
INSERT INTO table_name ( date_column ) VALUES ( DATE '0001-01-01' );
I don't know if this is relevant, but I was once "required" to store 0th April 1996, 0th 1996, etc.. The real requirement was that these were fiscal transactions that could not be assigned to a single day or month respectively (e.g. journal vouchers).
The solution was to use real dates (1st April 1996, 1st Jan 1996), and to add another column that gave the "scope" of the record -- "D", "M", "Q", "Y".
So if all the transactions for 1st Apr 1996 were required, a date_scope = 'D' predicate could be added to my_date = date '1996-04-01' to ensure that only daily transactions were retrieved.
If all the transactions for April 1996 were required ...
my_date between date '1996-04-01' and date '1996-04-30' and
date_scope in ('D','M')