Specific Month-day select statement query - sql

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..

Related

Need help aggregating custom months into a snapshot field

I am working with a dataset that has a continues date field and I want to aggregate it at a monthly level where the month ends on the 15th day of the month. So each snapshot date would go from the 15th of the month to the 14th of the following month.
Example: Snapshot Date = 7/15/2021 would correspond with the date range of 6/15/2021 through 7/14/2021.
Is there an easy way to do this for all months in the table using SQL Server.
Just subtract 14 days and convert to a year/month format. One trick is to move everything to the last day of the month:
select eomonth(dateadd(day, -14, datecol)), count(*)
from t
group by eomonth(dateadd(day, -14, datecol));

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.

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

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'

Previous year Year To Date with partial current year Year To Date Calculation SQL Server

I have a revenue table with data for last year and current year. I need to calculate the YTD last year and YTD current year, BUT I need to only consider data from min(date) from last year PER branch for current year YTD calculation.
eg: Branch KTM has data from 2018-02-25 not from Jan 1st.
Now I want to get YTD for the current year from the same date on 2019 till today.
I am able to get whole YTD for last year and this year, and also the minimum date/weeknumber for each branch for last year, but unable to calculated partial YTD for the current year.
Here is one drive link to mydata and sql : https://1drv.ms/u/s!Ave_-9o8DQVEgRS7FaJmm48UNsWz?e=lRfOJF
A snippet from my code
I need help with the SQL query to do this.
This returns the number of days between the same day-of-year of a last year's date and today's date:
select current_date - (date'2018-02-25' + interval '1' year); -- PostgreSQL
select datediff(current_date, (date'2018-02-25' + interval '1' year)); -- MySQL
Alternative version:
select extract(doy from current_date) - extract(doy from date'2018-02-25'); -- PostgreSQL
doy stands for day of year. At the time of the answer (2019-09-24) all queries return 211.
To sum values in that date range, use BETWEEN:
SELECT sum(revenue)
FROM your_table
WHERE date BETWEEN date'2018-02-25' + interval '1' year AND current_date

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.