SQL Server Calculate Number of date in a Year [duplicate] - sql

This question already has answers here:
how to get a year and day of the year by using sql server
(2 answers)
Closed 7 years ago.
How do I extract the number of days in a year base on a field in SQL?
For example: I have a Date field in SQL Server
Date
1/4/2015
2/1/2015
2/21/205
I want to use that date field to get me the day out of 365 so the result would look like this
Day of 365 Date
4 1/4/2015
32 2/1/2015
52 2/21/205
Thanks

very useful DATEPART function has overload for day, week, day of week, day of year, quater and others
select DATEPART(DAYOFYEAR, [DATE]) as DayOfYear, [DATE]
DayOfYear can be 366 in case of leap year (e.g. 31 December of 2000)

Have a look at DATEPART()
SELECT DATEPART(DAYOFYEAR , '1/4/2015')

Related

Custom month numbers that take last 30 days instead of Number of month (SQL Server)

I am trying to create a lag function to return current month and last month streams for an artist.
Instead of returning streams for Feb vs Jan, I wan the function to use the last 30 days as a period for current month, and the previous 30 days as the previous month.
The query that I am currently using is this:
SELECT
DATEPART(month, date) AS month,
artist,
SUM([Streams]) AS streams,
LAG(SUM([Streams])) OVER (PARTITION BY artist ORDER BY DATEPART(month, date)) AS previous_month_streams
FROM combined_artist
WHERE date > DATEADD(m, -2, DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()-7), CAST(GETDATE()-7 AS DATE)))
GROUP BY DATEPART(month, date), artist;
While this works, it is not giving me the data I need. This is returning the sum of streams for February vs the Streams for the month of January. February seems very low because we only have one week worth of data in February.
My goal is to get the last 30 days from the max date in the table using a lag function. So if the max date is Feb. 7 2023, I want the current month to include data from Jan. 7 2023 - Feb. 7 2023, and the previous month to include data from Dec. 7 2022 - Jan. 7 2023. I am thinking to create a custom month date part that will start from the max date and give a month number to the last 30 days . (2 for Jan 7 - Feb 7, 1 for Dec 7 - Jan-7...) I am not sure how to go about this. This is in SQL Server and I am looking to use the lag function for performance reasons.
I think you could probably use something like datediff(d, date_you_care_about, max_date)/30 in your group by and partition by clauses.
The basic idea is that integer division rounds down, so if the difference between the dates is < 30, dividing it by 30 is 0. If the difference is >=30 but less than 60, dividing it by 30 is 1. And so forth.
You can see a proof of concept in this Fiddle.

Calculate week no in google bigquery sql

Could anyone provide a code to calculate week no from current date in current quarter? E.g. today is Oct 6 2022, it lies in quarter 4, so week no is 1. Code should be in big query sql.
Considering 13 weeks per quarter and using ISO week number, you can try this:
SELECT EXTRACT(ISOWEEK FROM CURRENT_DATE()) - (13 * (EXTRACT(QUARTER FROM CURRENT_DATE())-1))
Output:
1

Number of Days in a month HIVE [duplicate]

This question already has answers here:
Find last day of a month in Hive
(6 answers)
Closed 1 year ago.
Can someone let me know a simple way to get # of days in a month in HIVE SQL based on current_date.
e.g. 2021-02-16 = 28 days, 2021-06-30 = 30 days etc
Use
day(last_day(current_date))
last_day(date) returns last day date, day(date) returns day part of the date, int
See manual here: Language Manual - Date Functions

How to get start & end of same week last year in DB2 using SQL?

I have a weekly report that uses these date parameters:
SELECT *
FROM TABLE
WHERE DATE_FIELD BETWEEN (CURRENT DATE - 8 DAYS) AND (CURRENT DATE - 2 DAYS)
This runs on Mondays to gather the previous week's data (Sun-Sat). What I want now is to run this for the same week of the previous year.
So for example, if the code above runs on Mon 29/06/20, it returns data from Sun 21/06/20 - Sat 27/06/20, i.e. week 26 of 2020. I want it to return data from Sun 23/06/19 - Sat 29/06/19, i.e. week 26 of 2019.
The report runs automatically so I can't just plug in the exact dates each time. I also can't just offset the date parameters to -357 and -367 days, as this gets thrown off by leap years.
I've searched for solutions but they all seem to rely on the DATEADD function, which my DB2 database doesn't recognise.
Does anyone know how I can get the result I'm looking for, please? Any advice would be appreciated! :)
The easiest way to do this is to build a calendar or dates table...(google sql calendar table)
Among the columns you'd have would be
date
year
month
quarter
dayofWeek
startOfWeek
endOfWeek
week_nbr
You can use the week() or week_iso() functions when loading the table, pay attention to the differences and pick the best fit for you.
Such a calendar table makes it easy to compare current period vs prior period.
If you assume that all years have 52 weeks, you can use date arithmetic:
SELECT *
FROM TABLE
WHERE DATE_FIELD BETWEEN (CURRENT DATE - (8 + 364) DAYS) AND (CURRENT DATE - (2 + 364) DAYS)
Because you want the week to start on a Monday, this doesn't have to take leap years into account. It is subtracting exactly 52 weeks -- and leap years do no affect weeks.
This gets more complicated if you have to deal with 52 or 53 week years.
A little bit complicated, but it should work. You may run it as is or test your own date.
SELECT
YEAR_1ST_WEEK_END + WEEKS_TO_ADD * 7 - 6 AS WEEK_START
, YEAR_1ST_WEEK_END + WEEKS_TO_ADD * 7 AS WEEK_END
FROM
(
SELECT
DATE((YEAR(D) - 1)||'-01-01')
+ (7 - DAYOFWEEK(DATE((YEAR(D) - 1)||'-01-01'))) AS YEAR_1ST_WEEK_END
, WEEK(D) - 2 AS WEEKS_TO_ADD
FROM (VALUES DATE('2020-06-29')) T(D)
);
The intermediate column YEAR_1ST_WEEK_END value is the 1-st Sat (end of week) of previous year for given date.
WEEKS_TO_ADD is a number of weeks to add to the YEAR_1ST_WEEK_END date.

difference between two dates in sql wih year and month format

i have two date like this
-4 year and 6 month
-2 year and 3 month
i want to calculate difference between this two date ?
If you have a datetime value then you can use Datediff function to calculate the difference between two datetime values in SQL Server.
SELECT DATEDIFF(day, '2004-06-01', '2008-06-01') AS "Number of Days";