Just want to select all dates between cureent date and first day of month . I want to use fill all dates in a temp table
declare #temp table
(
ddate datetime
)
I have tried with with cte and i don't want to use while loop becuase i want to avoid while in stored procedure .
For example as today is 11-oct-2012
so temp table should have 11 rows starting from 1-oct-2012 to 11-oct-2012
Try this
DECLARE #startDate DATE=CAST(MONTH(GETDATE()) AS VARCHAR) + '/' + '01/' + + CAST(YEAR(GETDATE()) AS VARCHAR) -- mm/dd/yyyy
DECLARE #endDate DATE=GETDATE() -- mm/dd/yyyy
SELECT [Date] = DATEADD(Day,Number,#startDate)
FROM master..spt_values
WHERE Type='P'
AND DATEADD(day,Number,#startDate) <= #endDate
OR
DECLARE #startDate DATETIME=CAST(MONTH(GETDATE()) AS VARCHAR) + '/' + '01/' + + CAST(YEAR(GETDATE()) AS VARCHAR) -- mm/dd/yyyy
DECLARE #endDate DATETIME= GETDATE() -- mm/dd/yyyy
;WITH Calender AS
(
SELECT #startDate AS CalanderDate
UNION ALL
SELECT CalanderDate + 1 FROM Calender
WHERE CalanderDate + 1 <= #endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
FROM Calender
OPTION (MAXRECURSION 0)
declare #temp table (ddate datetime);
insert #temp
select DATEDIFF(d,0,GetDate()-Number)
from master..spt_values
where type='p' and number < DatePart(d,Getdate())
order by 1;
use current_date add days of every the generated series to the days, the series could from the first day of the month subtract current_day to 0, such today 02 Jun, the series could be (-1, 0).
Try the following code:
DECLARE #startDate DATETIME=CAST(MONTH(GETDATE()) AS VARCHAR) + '/' + '01/' + + CAST(YEAR(GETDATE()) AS VARCHAR) -- mm/dd/yyyy
DECLARE #endDate DATETIME= CAST(MONTH(GETDATE()) AS VARCHAR) + '/' + '31/' + + CAST(YEAR(GETDATE()) AS VARCHAR) -- mm/dd/yyyy
;WITH Calender AS
(
SELECT #startDate AS CalanderDate
UNION ALL
SELECT CalanderDate + 1 FROM Calender
WHERE CalanderDate + 1 <= #endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
FROM Calender
OPTION (MAXRECURSION 0)
Related
I am a bit stuck here, I want to find all the week numbers in a given interval of time and I can't really figure it out
For example , instead of
- > datepart(week,dateadd(day,-1,#oneSingleDate)) (monday is the first day of the week) ,
I need something like
- > datepart(week,#startDate,#endDate)
Given the interval '2019-01-04'-'2019-01-28' the output needs to be :
WeekNo : 1,2,3,4,5 .
i've used a recursive CTE to generate all the dates in the range, then I've selected the DISTINCT week numbers from them using DATEPART. Then I've concatenated then into your comma-separated string into a variable called #OUT
DECLARE #startDate as date = '20190104';
DECLARE #endDate as date = '2019-01-28';
DECLARE #OUT as nvarchar(max);
WITH CTE AS (SELECT #startDate As X
UNION ALL
SELECT DATEADD(y, 1, X) X FROM CTE where x < #endDate),
CTE2 AS (SELECT DISTINCT datepart(wk, X) wk from cte)
select #out = coalesce(#out + ', ', '') + cast(wk as nvarchar(4)) from cte2
OPTION (MAXRECURSION 0);
select #out;
You can do it as follows:
using :
- [CTE][1] common Table expression
- [Sql recursion union all][2]
- [Concatenation of multiple rows into one line][3]
declare #startDate as date ;
declare #endDate as date ;
set #startDate='2019-01-04';
set #endDate='2019-01-28' ;
DECLARE #weeks VARCHAR(8000) ;
with cte as (select #startDate as mydate,datepart(week,#startDate) w
union all select
dateadd(day,1,mydate),datepart(week,dateadd(day,1,mydate)) from cte
where mydate < #endDate) , cte2 as (select distinct(w) from cte)
select #weeks=COALESCE(#weeks + ', ', '') +cast(w as varchar(2)) from
cte2 OPTION (MAXRECURSION 360) select #weeks [Result]
best way to do this would be to have parameters which link the day dropdown column to the month one so that there are correct days in every month?
You would also need to use your Year parameter for a leap day in February.
I would use a table of dates based on your other parameters:
DECLARE #YEAR AS INT = 2016 --FOR DEV/TESTING - REFERENCE PARAMETERS
DECLARE #MONTH AS INT = 2 --FOR DEV/TESTING
DECLARE #START_DATE DATE = CAST(#YEAR AS VARCHAR(4)) + '-' + RIGHT('0' + CAST(#MONTH AS VARCHAR(2)), 2) + '-' + '01'
DECLARE #END_DATE DATE = DATEADD(DAY, -1, DATEADD(MONTH, 1, #START_DATE))
;WITH GETDATES AS
(
SELECT #START_DATE AS THEDATE
UNION ALL
SELECT DATEADD(DAY,1, THEDATE) FROM GETDATES
WHERE THEDATE < #END_DATE
)
SELECT DAY(GETDATES.THEDATE) AS DAYS FROM GETDATES
OPTION (maxrecursion 0)
I have a table with Year and Month fields. I need to select the rows between two particular dates. How do I set this in the where condition?
You are a bit unclear on what "between" means in this context. I'm pretty sure that datefromparts() can be quite helpful:
select t.*
from t
where #date_start <= datefromparts(year, month, 1) and
#date_end >= datefromparts(year, month, 1);
You might also find eomonth() helpful, if the end of the month is an important date.
You can try with help month(),year() function of date
Here is the example:
select * from t where (t.month=month('2017-01-01') and t.year=year('2017-01-01')) and (t.month=month('2017-05-01') and t.year=year('2017-05-01'));
Below is the link for month() in sql.
https://learn.microsoft.com/en-us/sql/t-sql/functions/month-transact-sql
This might help you in solving your problem.
you can also try something like:
select
d.*
from
(select
t.*,
cast(rtrim(cast([month] as char(2))) + '/1/' + cast ([year] as char(4)) as date) as dt) d
where
dt >= #dateStart and
dt <= #dateEnd
If the year and month values are integers, you can cast them to varchar, then date, and use BETWEEN. An example with declaring startdate and enddate variables:
DECLARE #startdate date = '2015-07-01'
,#enddate date = '2016-07-01'
SELECT *
FROM #myTable
WHERE CAST(CAST(Year as varchar) + '-' + CAST(Month as varchar) + '-01' as date)
BETWEEN #startdate AND #enddate
If they are chars or varchars you need only cast to date and use BETWEEN:
DECLARE #startdate date = '2015-07-01'
,#enddate date = '2016-07-01'
SELECT *
FROM #myTable
WHERE CAST(Year + '-' + Month + '-01' as date)
BETWEEN #startdate AND #enddate
Try with "Between".
Something like this:
SELECT SALES_AMOUNT FROM TABLE WHERE YEAR BETWEEN 2015 AND 2016 AND MONTH BETWEEN 6 AND 8
I have been wondering is there any operator/function in sql server to tell about weekday between given day.
Example :11-19-2016 to 11-29-2016 I want check if there is tuesday between that day?
Try This:
declare #start varchar(100)='11-19-2016'
declare #end varchar(100)='11-29-2016'
;with dateRange as
(
select date = dateadd(dd, 1, #start)
where dateadd(dd, 1, #start) < #end
union all
select dateadd(dd, 1, date)
from dateRange
where dateadd(dd, 1, date) < #end
)
select date,DATENAME(dw,CAST(DATEPART(m, GETDATE()) AS VARCHAR)
+ '/'+ CAST(DATEPART(d, date) AS VARCHAR)
+ '/'+ CAST(DATEPART(yy, getdate()) AS VARCHAR)) as 'Day'
from dateRange where (DATENAME(dw,CAST(DATEPART(m, GETDATE()) AS VARCHAR)
+ '/'+ CAST(DATEPART(d, date) AS VARCHAR)
+ '/'+ CAST(DATEPART(yy, getdate()) AS VARCHAR)))='Tuesday'
The query below uses a recursive query to unfold the date range.
Then uses DATEPART to select only the Tuesdays from it.
But one could also use DATENAME instead.
declare #StartDate DATE = '2016-11-19';
declare #EndDate DATE = '2016-11-29';
--SET DATEFIRST 7;
-- The dw for tuesday is 3 when ##datefirst = 7 (default setting)
-- Since it depends on a usersetting, lets calculate it anyway.
declare #TuesdayWeekday INT = (7-##datefirst + 2)%7+1;
;with DATES as
(
select #startdate as [Date]
union all
select dateadd(day, 1, [Date]) from DATES
where [Date] < #enddate
)
select [Date], datepart(dw,[Date]) as day_of_week, datename(dw,[Date]) as day_of_weekname
from DATES
where datepart(dw,[Date]) = #TuesdayWeekday;
My scenario is as below:
#StartDate = 13th of current month
#EndDate = 12th of next month.
I want to get all the date with the day-name for Mondays, Tuesdays, Wednesdays, Thursdays, Fridays, Saturdays and Sundays lying between the start and end date.
Try this:
declare #startDate datetime = '2016-01-13'
declare #endDate datetime = '2016-02-12'
;with dateRange as
(
select [Date] = dateadd(dd, 1, #startDate)
where dateadd(dd, 1, #startDate) < #endDate
union all
select dateadd(dd, 1, [Date])
from dateRange
where dateadd(dd, 1, [Date]) < #endDate
)
select [Date], datename(dw,[Date])
from dateRange
To count the number of each day as per your comment (this should be part of the question really), change the last part of James' answer to this:
select datename(dw,[Date]) as day_name, count([Date]) as number_days
from dateRange group by datename(dw,[Date]), datepart(DW,[Date])
order by datepart(DW,[Date]);
You can try something like this :
DECLARE #StartDate DATETIME
DECLARE #StartDateFixed DATETIME
DECLARE #EndDate DATETIME
DECLARE #NumberOfDays int
SET #StartDate = '2016/01/01'
SET #EndDate = '2016/01/02'
SET #NumberOfDays = DATEDIFF(DAY,#StartDate,#EndDate) + 1
SET #StartDateFixed = DATEADD(DD,-1,#StartDate)
SELECT WeekDay , COUNT(WeekDay)
FROM (
SELECT TOP (#NumberOfDays) WeekDay = DATENAME(DW , DATEADD(DAY,ROW_NUMBER() OVER(ORDER BY spt.name), #StartDateFixed))
FROM [master].[dbo].[spt_values] spt
) A
GROUP BY WeekDay
The output will be
WeekDay
------------------------------ -----------
Friday 1
Saturday 1
(2 row(s) affected)
In case if you need to get current and next date from date number specified such as 13 and 12
Current Month
DECLARE #cur_mont INT = (SELECT MONTH(GETDATE()))
Current Year
DECLARE #cur_year INT = (SELECT YEAR(GETDATE()))
Next Month
DECLARE #nxt_mont INT = (SELECT MONTH(DATEADD(month, 1, GETDATE())))
Next Month year (In case of December year change)
DECLARE #nxt_year INT = (SELECT YEAR(DATEADD(month, 1, GETDATE())))
Create start date
DECLARE #startDate DATETIME = (SELECT CAST(CAST(#cur_year AS varchar) + '-' + CAST(#cur_mont AS varchar) + '-' + CAST(13 AS varchar) AS DATETIME))
Create end date
DECLARE #endDate DATETIME = (SELECT CAST(CAST(#nxt_year AS varchar) + '-' + CAST(#nxt_mont AS varchar) + '-' + CAST(12 AS varchar) AS DATETIME))
Dates between start and end date
SELECT TOP (DATEDIFF(DAY, #startDate, #endDate) + 1)
DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, #startDate) AS Date,
DATENAME(DW, DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, #startDate)) AS Day
FROM sys.all_objects a CROSS JOIN sys.all_objects b;
DECLARE #dayStart int = 13, --The day of current month
#dayEnd int = 12, --The day of another month
#howManyMonth int = 1, --How many month to take
#dateStart date,
#dateEnd date
--Here we determine range of the dates
SELECT #dateStart = CONVERT (date,
CAST(DATEPART(Year,GETDATE()) as nvarchar(5))+ '-' +
CASE WHEN LEN(CAST(DATEPART(Month,GETDATE()) as nvarchar(5))) = 1
THEN '0'+CAST(DATEPART(Month,GETDATE()) as nvarchar(5))
ELSE CAST(DATEPART(Month,GETDATE()) as nvarchar(5)) END + '-' +
CAST (#dayStart as nvarchar(5))),
#dateEnd = CONVERT (date,
CAST(DATEPART(Year,DATEADD(Month,#howManyMonth,GETDATE())) as nvarchar(5))+ '-' +
CASE WHEN LEN(CAST(DATEPART(Month,DATEADD(Month,#howManyMonth,GETDATE())) as nvarchar(5))) = 1
THEN '0'+CAST(DATEPART(Month,DATEADD(Month,#howManyMonth,GETDATE())) as nvarchar(5))
ELSE CAST(DATEPART(Month,DATEADD(Month,#howManyMonth,GETDATE())) as nvarchar(5)) END + '-' +
CAST (#dayEnd as nvarchar(5)))
;WITH cte AS (
SELECT #dateStart as date_
UNION ALL
SELECT DATEADD(day,1,date_)
FROM cte
WHERE DATEADD(day,1,date_) <= #dateEnd
)
--Get results
SELECT DATENAME(WEEKDAY,date_) as [DayOfWeek],
COUNT(*) as [DaysCount]
FROM cte
GROUP BY DATEPART(WEEKDAY,date_),
DATENAME(WEEKDAY,date_)
ORDER BY DATEPART(WEEKDAY,date_)
OPTION (MAXRECURSION 0)
Output:
DayOfWeek DaysCount
----------- -----------
Sunday 4
Monday 4
Tuesday 4
Wednesday 5
Thursday 5
Friday 4
Saturday 4
(7 row(s) affected