Deterministic scalar function to get week of year for a date - sql-server

Here is a great way of how to get day of week for a date, Deterministic scalar function to get day of week for a date.
Now, could anyone help me to create a deterministic scalar function to get week of year for a date please? Thanks.

This works deterministically, I can use it as a computed column.
datediff(week, dateadd(year, datediff(year, 0, #DateValue), 0), #DateValue) + 1
Test code:
;
with
Dates(DateValue) as
(
select cast('2000-01-01' as date)
union all
select dateadd(day, 1, DateValue) from Dates where DateValue < '2050-01-01'
)
select
year(DateValue) * 10000 + month(DateValue) * 100 + day(DateValue) as DateKey, DateValue,
datediff(day, dateadd(week, datediff(week, 0, DateValue), 0), DateValue) + 2 as DayOfWeek,
datediff(week, dateadd(month, datediff(month, 0, DateValue), 0), DateValue) + 1 as WeekOfMonth,
datediff(week, dateadd(year, datediff(year, 0, DateValue), 0), DateValue) + 1 as WeekOfYear
from Dates option (maxrecursion 0)

Related

How to get the Monday position of a month in T-SQL

For example consider the April 2023. In this year the Monday dates are 3, 10, 17, and 24. Now I want to get the date 10 position of April.
My target result is: April 10 = second or 2
SELECT (DAY('2023-04-10') - 1) / 7 + 1
Position outputs the searched number for a date
WITH CTE_DATES
AS(
SELECT CAST(DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) -1, 0) AS DATE) AS [DATE] -- start previous month
UNION ALL
SELECT CAST(DATEADD(DD, 1, [DATE]) AS DATE)
FROM [CTE_DATES]
WHERE [DATE] < CAST(DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 0, 0)) AS DATE) -- end previous month
)
SELECT
[date]
,DATENAME(WEEKDAY, [date]) AS 'Weekday'
,DATEPART(WEEKDAY, [date]) AS 'Day'
,ROW_NUMBER() OVER (ORDER BY [date] ASC) AS 'POSITION'
FROM [CTE_DATES]
WHERE DATEPART(WEEKDAY, [date]) IN (1)

How to get data of current date using sum in SQL?

I'm working on a SQL query trying to fetch sum data for the current day/date. Can anyone have a look at my query, and find me a working solution?
SELECT SUM(amount)
FROM tbl_expense_record
WHERE dateonly = CAST(GETDATE() AS Date)
But I get data when mentioning a specific date in where condition like
SELECT SUM(amount) AS total
FROM tbl_expense_record
WHERE dateonly = '2020-06-12'
I want the a code to auto pick current date. Also I would like to fetch sum of ranged dates like a whole week, and a month!
select datename(month, '2020-06-12'), datename(month, getdate());
--1week
SELECT SUM(amount) AS total
FROM tbl_expense_record
WHERE dateonly >= dateadd(week, -1, cast(getdate() as date))
and dateonly <= cast(getdate() as date)
--1month
SELECT SUM(amount) AS total
FROM tbl_expense_record
WHERE dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly <= cast(getdate() as date)
--build muscle memory (it is always safe to check for < date+1 instead of <= date)
--1month
SELECT SUM(amount) AS total
FROM tbl_expense_record
WHERE dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date));
--6months
SELECT SUM(amount) AS total
FROM tbl_expense_record
WHERE dateonly >= dateadd(month, -6, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date));
if not exists
(
select *
FROM tbl_expense_record
WHERE dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date))
)
begin
select 'no rows within the last month'
end
else
begin
select 'there are rows within the last month';
end;
Examples:
declare #tbl_expense_record table(dateonly date, amount decimal(9,2));
insert into #tbl_expense_record
values ('20200501', 10), ('20200612', 10), ('20200613', 11), ('20200614', 12),
('20200710', 5), ('20200720', 6), ('20200820', 20), ('20200825', 30),
('20201102', 1), ('20201110', 2), ('20201120', 3);
--aggregation per month, for all rows
select year(dateonly) as _year, month(dateonly) as _month, sum(amount) as sum_amount_per_month, count(*) as rows_per_month
from #tbl_expense_record
group by year(dateonly), month(dateonly);
--aggregation per iso-week
select year(dateonly) as _year, datepart(iso_week, dateonly) as _isoweek, sum(amount) as sum_amount_per_isoweek, count(*) as rows_per_isoweek
from #tbl_expense_record
group by year(dateonly), datepart(iso_week, dateonly);
--aggregation per month, for all rows with a dateonly that falls in the last month
--check the difference between aggregation per month earlier and this..
--filter rows first == where .... and then aggregate
--there are two rows with dateonly > 06 november (the row at 05 is filtered out by the where clause)
select year(dateonly) as _year, month(dateonly) as _month, sum(amount) as sum_amount_per_month, count(*) as rows_per_month
from #tbl_expense_record
where dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date))
group by year(dateonly), month(dateonly);
--aggregate per week diff from today/getdate()
select
datediff(week, getdate(), dateonly) as week_diff_from_today,
dateadd(day,
--datepart(weekday..) is used...account for ##datefirst setting / set datefirst
1-(##datefirst+datepart(weekday, dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date))))%7,
dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date)))
as startofweek,
dateadd(day, 6, --add 6 days to startofweek
dateadd(day,
--datepart(weekday..) is used...account for ##datefirst setting / set datefirst
1-(##datefirst+datepart(weekday, dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date))))%7,
dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date)))
) as endofweek,
sum(amount) as sum_amount, count(*) as rows_within_week
from #tbl_expense_record
group by datediff(week, getdate(), dateonly);

How to get whole month or week or day level data in a single query

I have a data in which I have to display a record by month or week or day according to the date selected.
If the date selected is for a whole month for like january its 01-01-2015 to 31-01-2015
then it should fetch for the record for the whole month.
If the date selected is such that for example like 01-01-2015 to 15-02-2015. Now here there are more than a month but Feburary is not in whole month so the january will record will come in one and next first week record for Feburary will be fetched in one and next week record will be fetched in another`.
Also he day left (as there will be 14 days in week two) will have a another record.`
So, in all 4 record will be fetched. Here instead of fecthing whole 46 days record I am just fecthing 4 records. Hope I gave a proper example if you can't just comment I will explain with another exapmle. I am now just worrying about how to split month, week and day. Can anyone help me with that. Thanks in advance
I am only worried about how to split the dates in month week or a day. The formula is simple if e have whole month the entry should be shown in a single record. If we date between of more than week it should shown week wise and less than week it should be on day level
Input
Here Time_type_id is a foreign key from other 3 tables. This time_type_id will help to get data day, week or month wise. The data is shown according to date passed by the user. Time_id is the primary key of the table and it points to the id of other 3 fact table.
Dimesion Table
Time_type_id Time_id metric_id
1 1 100
1 2 400
1 3 300
1 4 100
1 5 250
2 6 1000
2 7 2000
2 8 3000
2 9 2000
2 10 5000
3 11 11000
3 12 20000
3 13 40000
3 14 50000
3 15 60000
Fact table
Fact1- This shows the day basis data.
Dim_type Id day_date month year
1 1 01-09-2015 September 2015
1 2 02-09-2015 September 2015
1 3 03-09-2015 September 2015
1 4 04-09-2015 September 2015
1 5 05-09-2015 September 2015
Fact2- This shows the weekly basis data.
Dim_type Id week_number month year
2 6 week1 September 2015
2 7 week2 September 2015
2 8 week3 September 2015
2 9 week4 September 2015
2 10 week1 October 2015
Fact3- This shows the monthly basis data.
Dim_type Id month year
3 11 September 2015
3 12 September 2015
3 13 September 2015
3 14 September 2015
3 15 October 2015
Now the user just passed the dates between which he wants to see the data and according to dates passed we have to sho the ouput.
If it is month level we have to show Fact3 table where Dim_type is 3 same for other two table. I just want how to split the the date
Output-
For second example-
Sr no id data (from the date range)
1 1 data from whole January
2 2 data from first week of Feburary
3 3 data from second week of Feburary
4 4 data from 15 Febuaray
Note: Don't make that a week will always have 7 days maybe if week start from Thrusday than it would not have 7 days
you can use two parameter, so it's dynamic to choose dates like:
Declare #FromDate DATETIME,
#ToDate DATETIME
SELECT ...
FROM ...
WHERE DATE BETWEEN #FromDate AND #ToDate
There can be two solution available in your case:
Solution # 1
You need a table/temptable which contains data for all dates with columns like Date, DaySrNo, WeekSrNo, MonthSrNo and Moreover YearSrNo. Use this table in Join and get your data among three tables (which are also in JOIN)
Solution # 2
You need extra column for date range in your existing table structure:
like In Fact1 table contains day wise data, So you need Date Range Columns Like StartDate and EndDate
Same way Same Two column in other tables Fact2 and Fact3
So using this column in Join and where filter you can get your desired output.
IF OBJECT_ID('IAS_TEST_SCHEMA.DATESPLIT', 'P') IS NOT NULL
DROP PROCEDURE IAS_TEST_SCHEMA.DATESPLIT;
GO
CREATE PROCEDURE IAS_TEST_SCHEMA.DATESPLIT (#STARTDATE date, #ENDDATE date)
AS
BEGIN
DECLARE #DATEDIFF integer,
#MONTHEND integer = 0,
#MONTHSTART integer = 0,
#STARTWEEK integer = 0,
#ENDWEEK integer = 0;
IF #STARTDATE = CONVERT(date, DATEADD(M, DATEDIFF(M, 0, #STARTDATE), 0))
BEGIN
SET #MONTHSTART = 1;
END
IF MONTH(#ENDDATE) != MONTH(DATEADD(DAY, 1, #ENDDATE))
BEGIN
SET #MONTHEND = 1;
END
IF DATEPART(DW, #STARTDATE) = 2
BEGIN
SET #STARTWEEK = 1;
END
IF DATEPART(DW, #ENDDATE) = 1
BEGIN
SET #ENDWEEK = 1;
END
SET #DATEDIFF = DATEDIFF(DAY, #STARTDATE, #ENDDATE);
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
END
END AS START_DATE,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #STARTDATE), 6)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN CASE
WHEN #MONTHEND = 0 THEN DATENAME(MONTH, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1)) + ' ' + DATENAME(YEAR, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1))
ELSE DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(S, -1, DATEADD(MM, DATEDIFF(M, 0, #STARTDATE) + 1, 0))))
END
END AS END_DATE
UNION
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE DATENAME(MONTH, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, #STARTDATE), 0))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, #STARTDATE), 0)))
END
END,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
END
WHEN #DATEDIFF > 90 THEN CASE
WHEN #MONTHEND = 0 THEN DATENAME(MONTH, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1)))
ELSE DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
END
END
UNION
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHEND = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DD, -(DAY(#ENDDATE) - 1), #ENDDATE)))
END
END,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHEND = 1 THEN DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
END
END
END;
GO

Return records from this week

SELECT *
FROM TABLE TB
WHERE TB.DATE >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND TB.DATE < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
My dates look like this: 2015-01-19 00:00:00
Currently my query returns no records. I want to return records from this week. Anything created from Monday to Sunday.
I am using SQL Server 10.50.1617
Try this, it will find all data from this week from monday to sunday:
SELECT *
FROM TABLE TB
WHERE
TB.DATE >= dateadd(d, 0, datediff(d, 0, current_timestamp)/7*7)
AND TB.DATE < dateadd(d, 7, datediff(d, 0, current_timestamp)/7*7)
This will work for current week Monday - Sunday:
SELECT *
FROM TABLE TB
WHERE TB.DATE >= (DATEADD(day, DATEDIFF(day, 1, getdate()) / 7 * 7, - 0))
AND TB.DATE <= DATEADD(dd,DATEDIFF(dd,0,getdate())+ 1 , + 2)
This will get you prior week Monday - Sunday:
SELECT *
FROM TABLE TB
WHERE TB.DATE >= DATEADD(wk, DATEDIFF(wk, 6, GETDATE())-1, 7)
AND TB.DATE <= (DATEADD(day, DATEDIFF(day, 1, getdate()) / 7 * 7, - 1))

SQL Statement to Select Day Interval

How can I get the range between a day dynamically for e.g. BETWEEN 23.05.2012 00:00 AND 23.05.2012 23:59 using MSSQL ? I got the first part;
WHERE
AND s.SCHEDULE_START_DATE BETWEEN dateadd(DAY, datediff(DAY, 0, getdate()), 0) AND FILL HERE
You will have to try something like
s.SCHEDULE_START_DATE >= dateadd(DAY, datediff(DAY, 0, getdate()), 0)
AND s.SCHEDULE_START_DATE < (dateadd(DAY, datediff(DAY, 0, getdate()), 0) + 1)
Have a look at the below example
SQL Fiddle DEMO
Try this one -
Query:
DECLARE #Dates TABLE
(
SCHEDULE_START_DATE DATETIME
)
INSERT INTO #Dates
VALUES
('20130522'),
('20130523'),
('20130524'),
('20130523 18:00:00'),
('20130523 23:59:59')
DECLARE
#DateFrom DATETIME = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
, #DateTo DATETIME = DATEADD(SECOND, 86399, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))
SELECT *
FROM #Dates
WHERE SCHEDULE_START_DATE BETWEEN #DateFrom AND #DateTo
Output:
SCHEDULE_START_DATE
-----------------------
2013-05-23 00:00:00.000
2013-05-23 18:00:00.000
2013-05-23 23:59:59.000

Resources