-SQL - Get date up to present date and year - sql

i'm trying to get the date from the start up to present date and year.
For example:
Date Today Jan 31 , 2020
the result must be
jan 1 - 31 2020 ,
Another example: if the date is june 30, 2020
the result must be
jan 1 - june 30, 2020
Can you help me how to do it ? , i'm planning to use BETWEEN

DECLARE #date date = ' 2019-05-01'
SELECT EOMONTH(#date), DATEADD(yy, DATEDIFF(yy, 0, #date), 0)

Related

How to pass variable of adding 270 days to the date?

Below is my query:
select
facility_lob as FACILITY_LOB,
TO_DATE(REPLACE(posting_d_date_sk, ',', ''), 'YYYYMMDD') as PostingDate,
count(distinct encounter_num ||cast(date_of_service as varchar(20) )) as ENCOUNTER_VOLUME,
SUM(charge_amt) as Gross_Charges
from sources.Table
where 1=1
and REPLACE(posting_d_date_sk, ',', '') >= '20210101'
and REPLACE(posting_d_date_sk, ',', '') <= '20210928'
and posting_d_date_sk <> '-1'
and posting_d_date_sk is not NULL
group by facility_lob, posting_d_date_sk
order by REPLACE(posting_d_date_sk, ',', '')
For now, I have hard coded the dates from Jan 1, 2021 to Sep 28, 2021 ( which is 270 days from Jan 1, 2021).
My requirement is the query should should pull data from and greater than year 2021 (>= 2021 of the posting date), and I need to pull the data from Jan 1, 2021, to 270 days. If I run the query today, I need to get the data from Jan 1, 2021 to Sep 28 2021 ( which is 270 days from jan 1 2021). If I execute this query tomorrow, I need to get the data from Jan 2 2021 to Sep 29 2021. jan 3, 2021 to sep 30 2021 etc.. If I run the query on April 3, 2022 then the calculation of 270 days is from Feb 1, 2021, to Oct 29, 2021.
Could you please help how do I fix this?
Why not using a simple dateadd based on getdate and then format it into your format ?
Declare #to varchar(10), #from varchar(10), #mydate smalldatetime
Set #mydate = Getdate()
Set #from = convert(varchar,year(#mydate)) + right('0' + convert(varchar,month(#mydate)),2) + right('0' + convert(varchar,day(#mydate)),2)
Set #mydate = dateadd(dd,270,getdate())
Set #to = convert(varchar,year(#mydate)) + right('0' + convert(varchar,month(#mydate)),2) + right('0' + convert(varchar,day(#mydate)),2)

Subtract N from weeknum of current year

We have table WeeklyResults that looks like this:
Year WeekNum Value
2021 47 11.0
2021 48 14.0
2021 49 12.0
2021 50 17.0
2021 51 11.8
2021 52 11.3
2021 53 11.1
2022 01 11.5
2022 02 11.5
2022 03 81.5
We have a report with two parameters: Date and WeekNum. The report needs to show the last 6 weeks based on the weeknumber selected.
The issue is that, if user selects Week 2 of 2022, how can I subtract 6 weeks so that I get weeks 50, 51, 52, 53 of 2021 and weeks 1, 2 from 2022?
So, if the user selects 2022 and Week 02, it would show the last 6 weeks based on Weeknum 2 of year 2022 (wk50 to Wk02). If user selects 2021 and 52, it would show wk47-52.
You can use a little < and <= logic along side TOP and ORDER BY to achieve this:
DECLARE #Year int = 2022,
#WeekNum int = 3; --Note, if you are storing WeekNum as a (var)char,
--your leading zeros imply you are, then define the
--variable as a char(2).
SELECT TOP (6)
[Year],
WeekNum,
[Value]
FROM dbo.YourTable
WHERE ([Year] = #Year AND WeekNum <= #WeekNum)
OR [Year] < #Year
ORDER BY [Year] DESC,
WeekNum DESC;
Try this:
DECLARE #Year int = 2022
, #WeekNum varchar(02) = '02'
;
WITH FinalTable AS
(
SELECT TOP 6 *
FROM WeeklyResults
ORDER BY LTRIM(Year) + WeekNum DESC
)
SELECT *
FROM FinalTable
ORDER BY Year, WeekNum
Another option that doesn't involve an ORDER BY. Using DATEPART you can determine the final week of the prior year and subtract the number of weeks to get the records needed.
You may need to adjust what day is the first day of the week for your count. See this post for more info on that.
DECLARE
#Year INT = 2022
, #WeekNum INT = 3;
SELECT
*
FROM
WeeklyResults
WHERE
Year = #Year
AND WeekNum <= #WeekNum
OR
(
#WeekNum < 6
AND Year = #Year - 1
AND WeekNum > DATEPART (WEEK, CONCAT ('12/31/', #Year - 1)) - #WeekNum
);

SQL Server - Get calander month begining and end

I've tried searching but cannot find anything.
I am trying to get the first and last date of the calander month.
So for example the calander month for January 2020 actually starts on December 30th 2019 and ends on February 2nd 2020. (Week 1 - 5)
|---------------------|-------------------|-------------------|
| Week number | From Date | To Date |
|---------------------|-------------------|-------------------|
| Week 01 | December 30, 2019 | January 5, 2020 |
|---------------------|-------------------|-------------------|
| Week 05 | January 27, 2020 | February 2, 2020 |
|---------------------|-------------------|-------------------|
Using this website to get week numbers
Is this possible?
Many thanks.
If you are using MSSQL-2012 or onwards.
DECLARE #DATE DATETIME='29-JAN-2020'
SELECT DATEADD(DAY, 2 - CASE WHEN DATEPART(WEEKDAY, #DATE-DAY(#DATE)+1)=1 THEN 8 ELSE DATEPART(WEEKDAY, #DATE-DAY(#DATE)+1) END, CAST( #DATE-DAY(#DATE)+1 AS DATE)) [MONTH_START_DATE],
DATEADD(DAY, 8 - CASE WHEN DATEPART(WEEKDAY, EOMONTH(#DATE))=1 THEN 8 ELSE DATEPART(WEEKDAY, EOMONTH(#DATE)) END , CAST(EOMONTH(#DATE) AS DATE)) [MONTH_END_DATE];
You can try on below link:-
https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=9747ea25d0d0bc343be8dbcc90803303
You can use this logic:
select convert(date, dateadd(day, 1 - day(getdate()), getdate())) as month_first,
dateadd(day, 1, eomonth(getdate(), -1)) as alternative_month_first,
eomonth(getdate()) as month_last
Of course, you would use whatever date you wanted instead of getdate().

How to get month value using week value sql server

I want to get month value using week no.
I have week numbers stored in a table with year value.
How to query database to get month value using that week value.
I am using SQL
You can try this:
SELECT DATEPART(m,DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + CAST(t.year as VARCHAR(4))) + (t.week-1), 6))
It depends on how you're classing your week numbers, For example, if we assume that week numbers start on a Monday then we'd have to say that week 1 in 2016 actually started on Monday 28th of December 2015 and finished on Sunday 3rd January 2016. If this is how your week numbers are set up then you can use the method below
Sample Data;
CREATE TABLE #DateTable (WeekNum int, YearNum int)
INSERT INTO #DateTable (WeekNum, YearNum)
VALUES
(1,2016)
,(2,2016)
,(3,2016)
,(4,2016)
,(5,2016)
,(6,2016)
,(7,2016)
We will then cast the week and year into a date, then convert this to a month;
SELECT
WeekNum
,YearNum
,DATEADD(wk, DATEDIFF(wk, 7, '1/1/' + CONVERT(varchar(4),YearNum)) + (WeekNum-1), 7) AS WeekStart
,DATEPART(mm,DATEADD(wk, DATEDIFF(wk, 7, '1/1/' + CONVERT(varchar(4),YearNum)) + (WeekNum-1), 7)) MonthNum
(Edit: updated as source is int)
Gives these results;
WeekNum YearNum WeekStart MonthNum
1 2016 2015-12-28 00:00:00.000 12
2 2016 2016-01-04 00:00:00.000 1
3 2016 2016-01-11 00:00:00.000 1
4 2016 2016-01-18 00:00:00.000 1
5 2016 2016-01-25 00:00:00.000 1
6 2016 2016-02-01 00:00:00.000 2
7 2016 2016-02-08 00:00:00.000 2
You can't go from week number to month because weeks can occur in two different months. For example the 31st Jan 2016 and 1st Feb 2016 are both in week 6.
SELECT DATEPART(WEEK, '2016-01-31')
SELECT DATEPART(WEEK, '2016-02-01')
You can try the query below:
SELECT
[Week],
[Year],
'Output-Month' = MONTH(DATEADD(WEEK, [Week], DATEADD(WEEK, DATEDIFF(WEEK, '19050101', '01/01/' + CAST([Year] AS VARCHAR(4))), '19050101')))
FROM YourTable
1st is to get the 1st day of the year using this:
DATEADD(WEEK, DATEDIFF(WEEK, '19050101', '01/01/' + CAST([Year] AS VARCHAR(4))), '19050101')
2nd is to add your number of week using this:
DATEADD(WEEK, [Week], 'From 1st result')
Last is getting the number of Month using the MONTH function.

SqL stored procedure to find the dates,month and days

In SQL Server how to write the query to get the month in between the startdate and enddate, days(in month) for the Month , actual number of days in the month
startdate and enddate is passed as a input parameter , i need to know the number of months, actual days and the count of days in the month
e.g :
Start date : 2012-04-02
End date : 2012-08-23
i need to get the result as,
Month Days ActualDays _ inMonth
----------------------------------------------
04 29 30 - (The no of days on Apr'12 )
05 31 31
06 31 31
07 31 31
08 31 31
04 -month
29 -(Frm startdate 2-Apr to till 30-Apr'12(Apr End))
30 - (The no of days on Apr'12 )
Here's a procedure to calculate the number of days in a month. I do not know what you mean by "actual days" or how that could be different.
if object_id('spMonthList') is not null
drop procedure spMonthList
go
create procedure dbo.spMonthList(
#start date
, #end date)
as
; with Months as
(
select dateadd(month, datediff(month, 0, #start), 0) as MonthStart
union all
select dateadd(month, 1, MonthStart)
from Months
where MonthStart < #end
)
select datepart(month, MonthStart) as Month
, datediff(day, MonthStart, dateadd(month, 1, MonthStart)) as NumberOfDays
from Months;
go
exec spMonthList '2012-04-01', '2012-08-01'
-->
Month NumberOfDays
4 30
5 31
6 30
7 31
8 31
Example at SQL Fiddle.