I want to filter data from SQL Server 2008 R2 using FromDate and ToDate with another 2 user input dates. I want query for that.I want to filter data such a way that If user input 2 dates or any date in between of them lies in FromDate or 'ToDate' or in between date then it filters data.
For i.e.
FromDate ToDate
15-11-2014 20-11-2014
if user input dates are 11-11-2014 and 20-12-2014. It means in between dates of this 2 dates lies in between FromDate and ToDate so it should return this record.
Now, if user input dates are 11-11-2014 and 14-11-2014. It means in between dates of this 2 dates does not lie in between FromDate and ToDate so it should not return this record.
It must match given dates and as well as in between dates.
you Can use this :
Where
DATEADD(dd, 0, DATEDIFF(dd, 0, FromDate)) > '15-11-2014'
and
DATEADD(dd, 0, DATEDIFF(dd, 0, ToDate)) < '20-11-2014'
please check the example, you just compare the parameter with both datetime column to extract the rows.
declare #table table(id int,frdt datetime, todt datetime)
insert into #table values (1,GETDATE()-20, GETDATE()-19)
,(1,GETDATE()-9, GETDATE()-8)
,(1,GETDATE()+20, GETDATE()+18)
,(1,GETDATE(), GETDATE()-1)
,(1,GETDATE()-20, GETDATE())
,(1,GETDATE()-10, GETDATE()+10 )
select * from #table
declare #frdt datetime = null , #todt datetime = getdate()-10
select #frdt, #todt,* from #table
where
(#frdt is null or #frdt between frdt and todt)
and
(#todt is null or #todt between frdt and todt)
select #frdt = GETDATE() , #todt = GETDATE()
select #frdt, #todt,* from #table
where
(#frdt is null or #frdt between frdt and todt)
and
(#todt is null or #todt between frdt and todt)
From what I understand from your question.
select 'a' A, CONVERT(date,'11/15/2014') FromDate, CONVERT(date, '11/20/2014') ToDate into #Temp
declare #frmDate date;
declare #toDate date;
set #frmDate = Convert(date, '11/11/2014');
set #toDate = CONVERT(Date, '12/20/2014');
select * from #Temp where FromDate>=#frmDate and ToDate<=#toDate
set #frmDate = Convert(date, '11/11/2014');
set #toDate = CONVERT(Date, '11/14/2014');
select * from #Temp where FromDate>=#frmDate and ToDate<=#toDate
Hope fully it help you :)
Related
I need to populate a temp table with dates between and including 2 date parameters, let's say start date is 2014-01 and end date is present.
So far, I had managed to do it, but I need only "year-month" format.
Don't need any days involved.
declare #StartDate date = '2014-01-01';
declare #EndDate date = getdate();
;WITH cte AS (
SELECT #StartDate AS myDate
UNION ALL
SELECT DATEADD(day,1,myDate) as myDate
FROM cte
WHERE DATEADD(day,1,myDate) <= #EndDate
)
SELECT myDate
FROM cte
OPTION (MAXRECURSION 0)
Actual Results
2014-01-01
2014-01-02
Expected Result
2014-01
2014-02
2014-03
Replace day with month as in:
declare #StartDate date = '2014-01-01';
declare #EndDate date = getdate();
;WITH cte AS (
SELECT #StartDate AS myDate
UNION ALL
SELECT DATEADD(month,1,myDate) as myDate
FROM cte
WHERE DATEADD(month,1,myDate) <= #EndDate
)
and using the conversion suggested here:
SELECT FORMAT(myDate, 'yyyy-MM')
FROM cte
OPTION (MAXRECURSION 0)
I have 2 dates 01/04/2017 and 30/04/2017. I want all the dates between these 2 dates with 7 days interval.
Expected Output :
01/04/2017
08/04/2017
15/04/2017
22/04/2017
29/04/2017
Please help!!
DECLARE #StartDate DATETIME,
#EndDate DATETIME
SELECT #StartDate = '2017-04-01',
#EndDate = '2017-04-30'
SELECT DATEADD(DAY, number*7, #StartDate)
FROM master.dbo.spt_values
WHERE type='P'
AND #EndDate >= DATEADD(DAY, number*7, #StartDate)
One method would be to use a Calendar table. Then return the results from there using the modulus to get the 7th rows:
WITH Dates AS(
SELECT *,
ROW_NUMBER() OVER (ORDER BY [date]) AS RN
FROM DateTable
WHERE [Date] BETWEEN '20170401' AND '20170430')
SELECT *
FROM Dates
WHERE (RN - 1) % 7 = 0;
I've used this solution, as from your post you imply that you might supply any date range, and that the 1st day may not necessarily be a Monday (or other specific day).
Try this
DECLARE #STRT DATETIME='04/01/2017',#END DATETIME ='04/30/2017'
;WITH CTE
AS
(
SELECT
MyDate = CAST(#STRT AS DATETIME)
UNION ALL
SELECT
MyDate = CAST(MyDate AS DATETIME)+7
FROM CTE
WHERE CAST(MyDate AS DATETIME)+7 < CAST(#END AS DATETIME)
)
SELECT
*
FROM CTE
result
Declare #StartDate DATE=CONVERT(DATE,'01/04/2017',104),#EndDate DATE=CONVERT(DATE,'01/12/2017',104)
Declare #String NVARCHAR(MAX)=''
WHILE (#StartDate<=#EndDate AND DATEDIFF(wk,#StartDate,#EndDate)>=0)
BEGIN
SET #String=#String+CONVERT(NVARCHAR(100),#StartDate)+CHAR(10)+CHAR(13)
SET #StartDate=DATEADD(d,7,#StartDate)
END
PRINT #String
GO
I'm trying to create a report that gets records from a SQL Server database where the delivery date is today.
I've tried
select * from (tablename)
where delivery_date = getdate()
Although that didn't give me any errors, it didn't give me any records either.
I'm assuming it is because all dates are like:
2016-03-15 00:00:00.000
Perhaps, I need to truncate the date to remove the time-stamp and then try?
You can try a query like below
select * from (tablename)
where CAST(delivery_date as date) = CAST(getdate() as date)
Also if all delivery dates have time part like 00:00:00.000 for sure then
select * from (tablename)
where delivery_date = CAST(getdate() as date)
would work as good.
If delivery_date is always midnight (00:00:00.000), then compare it like this:
select * from (tablename)
where delivery_date = datediff(d, 0, getdate())
Using datediff like this is a quick way to truncate the time part of a datetime value.
I'd just create 2 params. One for StartTime and one for EndTime and use those in my query.
DECLARE #StartTime DATETIME,
#EndTime DATETIME
SET #StartTime = DATEDIFF(d,0,GETDATE())
SET #EndTime = DATEADD(d,1,#StartTime)
SELECT *
FROM [tablename]
WHERE delivery_date >= #StartTime
AND delivery_date < #EndTime
Try this:
DECLARE #Today DATETIME
SET #Today= CONVERT(date, getdate())
select * from (tablename)
where delivery_date = #Today
Yo need to remove the time part of the delivery_date field AND the GETDATE() value.
SELECT *
FROM (tablename)
WHERE DATEADD(dd, DATEDIFF(dd, 0, delivery_date), 0) = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
I am running a sql query that is omitting the day if the return count is 0. I want my query to return the day and a 0 count if the the count is 0. Snare I have is that if 0 were sold for the day, the day is omitted from my return results.
SELECT ISNULL([day],0) As [day], COUNT(ISNULL(Sold,0)) As [Sold]
FROM productionInfo
You're drawing information from a single table, productionInfo. If productionInfo has no rows with that date information (because there are no widgets sold on that date), how does it know what dates to use?
You might want to look at using a Numbers Table to get a row for each day of the month/year, then join that to productionInfo so you have a day value available, even if there was no production that day.
This will give you a dates table:
CREATE FUNCTION dbo.DatesTable (#startDate DATETIME, #endDate DATETIME)
RETURNS #retTable TABLE (DateValue DATETIME)
AS
BEGIN
DECLARE #currentDate DATETIME
SET #currentDate = #startDate
WHILE (DATEDIFF(dd, #currentDate, #endDate) >= 0)
BEGIN
INSERT INTO #retTable VALUES (#currentDate)
SET #currentDate = DATEADD(dd, 1, #currentDate)
END
RETURN
END
Then your query will look like:
SELECT dt.DateValue AS [day], COUNT(Sold) AS [Sold]
FROM dbo.DatesTable('2-1-2014', '2-10-2014') dt
LEFT JOIN productionInfo pi ON pi.day = dt.DateValue
GROUP BY dt.DateValue
I would like to compare datetime datatype, like "20/12/2011 00:00:00", with compound string date format (I mean that it is composed of string of date, string of month and string of year).
For example, coloumn entime is datatime datatype which is stored "20/12/2011 00:00:00" data and other three column(date,month,year respectively) are string. so I want to compare between entime column with the date,month and year composed together, How I can write SQL Command to suppurt the above requirement ?
Hope you can help me ?
The best option is to convert the datetime to string and then make the needed comparisons.
You can see here how to make the conversion.
There is also the DATEPART function as an alternative.
SELECT *
FROM DateTable
WHERE
DATEPART(YEAR, [DATECOLUMN]) = #YearString
AND DATEPART(MONTH, [DATECOLUMN]) = #MonthString
AND DATEPART(DAY, [DATECOLUMN]) = #DayString
You can go below way, would you please try it out, thanks
SET DATEFORMAT DMY
SELECT CAST(CONVERT(VARCHAR(15), GETDATE(), 105) AS DATETIME)
SELECT CAST(('20'+'-'+'12'+'-'+'2011') AS DATETIME)
As an example:
SET DATEFORMAT DMY
SELECT CAST(CONVERT(VARCHAR(15), yourDateColumn, 105) AS DATETIME) FROM TableName
SELECT CAST((dayColumn+'-'+monthColumn+'-'+yearColumn) AS DATETIME)
FROM anotherTable
Finally the comparison:
SELECT t1.* FROM tableName t1, anotherTable t2
WHERE CAST(CONVERT(VARCHAR(15), t1.DateColumnName, 105) AS DATETIME)
= CAST((t2.dayColumn+'-'+t2.monthColumn+'-'+t2.yearColumn) AS DATETIME)
Is this your requirement ?
DECLARE #tblTemp TABLE
(
DAY VARCHAR(10)
,Month VARCHAR(10)
,Year VARCHAR(10)
)
DECLARE #dtDateTime VARCHAR(10) = '20/12/2011 00:00:00'
INSERT INTO #tblTemp VALUES
('01','01','2011'),
('01','02','2011'),
('01','03','2011'),
('01','04','2012');
select * from #tblTemp where CONVERT(DATE,Year +Month+DAY ,103) < CONVERT(DATE,#dtDateTime,103)