First Day of month - sql

select distinct
DATEPART("yyyy", datum) AS year
, DATEPART("mm", datum) AS month
=
Can anybody tell my how i get the first day of the month with it. If I try "Datepart("dd", datum) As day", I get every day instead of only the first day

For SQL Server 2012+ try:
DATEFROMPARTS(YEAR(datum), MONTH(datum), 1)

You can use End of Month function (EOMONTH()) with the previous month, and add 1 day.
DECLARE #Date DATE = '2018-06-08'
SELECT
OriginalDate = #Date,
EndOfPreviousMonth = EOMONTH(#Date, -1),
StartOfMonth = DATEADD(DAY, 1, EOMONTH(#Date, -1))

First day of month is always 1st.
select distinct
DATEPART("yyyy", datum) AS year,
DATEPART("mm", datum) AS month,
1 AS FirstDayOfMonth
from qdatum
order by year, month
(I am clueless on what you are trying to achieve actually)

CREATE TABLE Table1
([datum] date)
;
INSERT INTO Table1
(datum)
VALUES
('04-06-2018')
;
SELECT distinct DATEPART("yyyy", datum) AS year
,DATEPART("mm", datum) AS month
,DATENAME(dw,CAST(DATEPART("mm", datum) AS VARCHAR)
+ '/'
+ CAST('01' AS VARCHAR)
+ '/'
+ CAST(DATEPART("yyyy", datum) AS VARCHAR)) AS [First day of month]
FROM table1
order by year, month
OutPut
year month First day of month
2018 4 Sunday
Live Demo
http://sqlfiddle.com/#!18/69402/4

Related

Bigquery YTD WORKDAYS calculation

I use the below calc to calculate workdays in a month.
i.e for calendar date = 28-05-2021 , i get 23 days ( monday to friday)
(select count(*) from unnest(generate_date_array(date_trunc(CALENDAR_DATE, month), last_day(CALENDAR_DATE, month ))) day
where not extract(dayofweek from day) in (1, 7)) as Workdays_Month,
I would like to calculate YTD Workdays based on the Calendar Date
i.e
if calendar date = 28-05-2021 then YTD workdays would be sum of workdays in months ( Jul 20 to May 21) financial year.
Assuming your financial year starts in July - you can use below
select count(*)
from unnest([struct(extract(year from current_date) as year, extract(month from current_date) as month)]),
unnest(generate_date_array(if(month < 7, date(year - 1, 7, 1), date(year, 7, 1)), last_day(date(year, month, 1)))) day
where not extract(dayofweek from day) in (1, 7)

I have a calendar Table and I need to display only 15th and 30th for every month

Here is the code I am using to display the last date of the month
SELECT * FROM Calendar_table
WHERE date in (SELECT MAX(date) from Calendar_table
ORDER BY YEAR,
DATE);
You can use to_char() to extract the day of the month from the date, and use it for filtering:
select *
from calendar_table
where to_char(dt, 'dd') in ('15', '30')
This assumes that the calendar date is stored in column dt.
You can also use extract():
select *
from calendar_table
where extract(day from dt) in (15, 30)
On the other hand, if you want the 15th of the month and the last day of the month, then:
select *
from calendar_table
where extract(day from dt) = 15 or last_day(dt) = dt
Few days back I have answered a question related to last day handling may be you can use it for the same:
https://stackoverflow.com/a/63134303/10908274

Aggregate by week using (Sun - Sat)

SELECT
CONCAT(CAST(WEEK(CAST(date as DATE)) as VARCHAR), ' - WEEK') as week,
COUNT(DISTINCT(field1)) as field1_count,
SUM(amount) as amount
FROM table
WHERE SUBSTR(DATE, 1, 4) = '2020'
GROUP BY 1
ORDER BY 1
Is there a way to ensure that the aggregate here is Sun - Sat? This is currently aggregating at Mon - Sun
Add one day to the date:
SELECT CONCAT(CAST(WEEK(CAST(date + INTERVAL '1' DAY as DATE)) as VARCHAR), ' - WEEK') as week,

last date of Financial year (I.e 2017-03-31)

Hi please let me know how to extract the last day of Financial year in sql server.my financial year start from 2016-04-01 to 2017-03-31
Closest you can use is End Of Month for that you need to provide one date to that month as below:
select eomonth('2017-03-01')
To get the last day of the financial year for any date, you need to find the last of march if before march, or the last of march next year if after march:
declare #yourdate datetime = getdate();
select case when month(#yourdate) < 4 then CONVERT(datetime,cast(YEAR(#yourdate) as char(4)) + '-03-31' ,120)
else CONVERT(datetime,cast(YEAR(#yourdate) + 1 as char(4)) + '-03-31' ,120)
end as financial_year_end
Edit:
If you want last date derived based on from_date, then use something like this
Rextester Demo
select
case when datepart(mm,from_date) <=3 then
cast(concat(year(from_date),'-03-31') as datetime)
else
dateadd(year,1,cast(concat(year(from_date),'-03-31') as datetime))
end as last_date_fin
from
(select '2017-04-30' as from_date union all
select '2017-01-13') t;
This way from_date between Jan - Mar will give same year's 31st march. Else it will give next year's 31st March.
Previous answer:
http://rextester.com/AXVM26769
If you want to get last day of march for same year as passed, then use
select cast(concat(given_year,'-03-31') as datetime)
from
(select '2017' as given_year) t
If you want to pass 2016 and then get 2017-03-31 then use. You can change the year in derived table and change the output based on that.
select dateadd(year,1,cast(concat(given_year,'-03-31') as datetime))
from
(select '2016' as given_year) t;
This Code will work to find the last date of Financial Year.
For Previous Year case matches and 'THEN' part will Execute and for current year 'ELSE'
part will execute.
select CASE WHEN (MONTH(GETDATE())) <= 3
THEN convert(varchar(4), YEAR(GETDATE())-1) + '-' + '03-31'
ELSE convert(varchar(4),YEAR(GETDATE()))+ '-' + '03-31'
end
> LastDayOfYearFY] =
> eomonth( dateadd(month, 5,
> dateadd(year, datepart(year, (dateadd(month, 6, [date])) ) -1900, 0)))
Idea extension taken from return-first-day-of-financial-year
You can select all the dates order them descendant and take the first one.
SELECT date
FROM table
ORDER BY date desc
LIMIT 1;

Date split-up based on Fiscal Year

Given a From Date, To Date and a Fiscal Year system, I want to get all the split-up duration within the
given From & To Date based on the Fiscal Year system. Explained below with examples:
Example 1:
Fiscal Year system: Apr to Mar
From Date: Jan-05-2008
To Date: May-15-2008
Based on Fiscal Year system, duration should be splitted into:
Jan-05-2008 to Mar-31-2008
Apr-01-2008 to May-15-2008
Example 2:
Fiscal Year system: Apr to Mar
From Date: Jan-17-2008
To Date: May-20-2009
Based on Fiscal Year system, duration should be splitted into:
Jan-17-2008 to Mar-31-2008
Apr-01-2008 to Mar-31-2009
Apr-01-2009 to May-20-2009
Am looking for approach/algorithm to solve this in PostgreSQL 8.2.
Regards,
Gnanam
I actually favor Andomar's solution (with the addition of a processes that automatically fills the Periods table), but for fun here's a solution that doesn't require it.
CREATE TABLE your_table (start_date date, end_date date);
INSERT INTO your_table VALUES ('Jan-17-2008', 'May-20-2009');
SELECT
GREATEST(start_date, ('04-01-'||series.year)::date) AS year_start,
LEAST(end_date, ('03-31-'||series.year + 1)::date) AS year_end
FROM
(SELECT
start_date,
end_date,
generate_series(
date_part('year', your_table.start_date - INTERVAL '3 months')::int,
date_part('year', your_table.end_date - INTERVAL '3 months')::int)
FROM your_table) AS series(start_date, end_date, year)
ORDER BY
start_date;
You could create a table containing the start and end of all fiscal years, f.e.
Periods (PeriodStartDt, PeriodEndDt)
Then you can join the tables together if they at least partly overlap. Use a case statement to select the end of the period or the end of the row, depending on which is later. For example (not tested):
select case when yt.StartDt < p.PeriodStartDt then p.PeriodStartDt
else yt.StartDt
end as SplitStart
, case when yt.EndDt > p.PeriodEndDt then p.PeriodEndDt
else yt.EndDt
end as SplitEnd
, yt.*
from YourTable yt
inner join Periods p
on yt.StartDt < p.PeriodEndDate
and yt.EndDt >= p.PeriodStartDate
CREATE TABLE your_table (start_date date, end_date date);
INSERT INTO your_table VALUES (CONVERT (date, GETDATE()),CONVERT (date, DATEADD(year, -1, GETDATE())) );
WITH mycte AS
(
SELECT 1 as id
UNION ALL
SELECT id + 1
FROM mycte
WHERE id + 1 < = 12
),
cte_distribution as
(
SELECT *,
DATEPART (month,DATEADD(month, mycte.id - 1, your_table.start_date)) as month_number ,
DATEPART (YEAR,DATEADD(month, mycte.id - 1, your_table.start_date)) as cal_year,
12000/12 as cash
FROM your_table
CROSS JOIN mycte
)
select
*,
(CASE WHEN month_number between 1 and 3 THEN '1st quarter' WHEN month_number between 4 and 6 THEN '2nd quarter' WHEN month_number between 7 and 9 THEN '3rd quarter' WHEN month_number between 9 and 12 THEN '4th quarter' END) as Quarter,
CASE WHEN month_number between 1 and 6 THEN CAST(CAST((cal_year - 1) as CHAR(4)) + '-' + CAST(cal_year as CHAR(4)) AS CHAR(9)) WHEN month_number between 6 and 12 THEN CAST(CAST((cal_year) as CHAR(4)) + '-' + CAST((cal_year + 1) as CHAR(4)) AS CHAR(9)) ELSE NULL END as fin_year
from cte_distribution;