I have a table which I want to query records from based on a timescale between two dates.
I have two variables,
DECLARE #StartDate datetime;
DECLARE #EndDate datetime;
What clause could I add to only show the records found within dates applied to these two variables?
This is based on a 'Date' column within the table.
SELECT myColumn
FROM myTable
WHERE Date BETWEEN #StartDate AND #EndDate
Edited: Between clause is inclusive (both dates are included in the result) so if you maybe want to exclude one of the dates in the variable columns better use:
SELECT myColumn
FROM myTable
WHERE Date >= #StartDate
AND Date <= #EndDate
SELECT * FROM [your_table] WHERE Date>=#StartDate AND Date<=#EndDate
Related
I faced a problem with a SQL query. I have a table with 10 fields.
I need to create a query, which gets date by field ProductionYear(int) between 2 variables #startDate(int) and #endDate(int). Both of these variables are unnecessary. And I need to build a SQL query with following conditions:
If(#endDate = 0)
Select Id from MyTable where ProductionYear > #startDate
else
Select Id from MyTable where ProductionYear BETWEEN #startDate and #endDate.
How can I build a query with those conditions?
You can incorporate this into a single query:
Select Id
from MyTable
where ProductionYear >= #startDate and
(ProductionYear <= #endDate or #endDate = 0);
Your two queries are inconsistent on whether #startDate is included. BETWEEN includes the comparison values, but > does not.
If you want #startDate to also be optional:
Select Id
from MyTable
where (ProductionYear >= #startDate or #startDate = 0) and
(ProductionYear <= #endDate or #endDate = 0);
Some additional comments. Calling a "year" a "date" is confusing. Your parameters should probably be called #startYear and #endYear.
These queries are going to result in full table scans of your table. This is probably not a big deal, because the granularity is by year. If the granularity were more refined, you might want to use an index. In that case, perhaps the best approach is dynamic SQL.
you can try by using case when
Select Id from MyTable
where ProductionYear BETWEEN (case when #startDate>#endDate then #endDate
else #startDate end) and
(case when #startDate>#endDate then #startDate else #endDate end)
I have some data stored in SQL Server which contains dates (date datatype). I am currently using a BETWEEN clause to filter my records in dates range, something like this ...
SELECT
*
FROM
Report
WHERE
ReportDate BETWEEN '2016-08-01' AND '2017-08-01'
Is it possible to use BETWEEN and LIKE clause at the same time or something close to that so that whenever a user doesn't specify any dates he/she would be able to pull all the reports? So far the query below
SELECT
*
FROM
Report
WHERE
ReportDate BETWEEN '' AND ''
doesn't show any records at all. Is there a way of doing this ..?
Use NULL with a parameter... if no value is given for #startDate and #endDate then the default will be NULL for these parameters, and the second WHERE condition would be met, returning all records.
create proc myProc(#startDate datetime = null, #endDate datetime = null)
as
SELECT * FROM Report
WHERE
(ReportDate BETWEEN #startDate AND #endDate)
or
(#startDate is null and #endDate is null)
Also, if your field is a DATETIME then this blog by Aaron is well worth your read.
Also, this method means the user has to enter both or neither of the parameters. If that's not what you'd want just let us know.
I think the correct logic would be:
SELECT r.*
FROM Report r
WHERE (ReportDate >= #startDate OR #startDate IS NULL) AND
(ReportDate <= #endDate OR #endDate IS NULL);
This works when only one of the values is NULL.
Note:
I would go with Aaron Bertrand's advice and really use:
SELECT r.*
FROM Report r
WHERE (ReportDate >= #startDate OR #startDate IS NULL) AND
(ReportDate < DATEADD(day, 1, #endDate) OR #endDate IS NULL);
I'm writing a stored procedure in sql!
I have to get records in the particular date.
I am using this query:
Declare #FromDate datetime
set #FromDate = '06/02/2014'
select * from Table where Date = #FromDate
Actually, in the Database there are 10 records in that date, but it is showing only two records because the #FromDate is taking like this 06/02/2014 00:00:00.000
If I write the query like this it means it works correctly!
select * from Table
where Date between '2014-08-28 00:00:00.000' and '2014-08-28 23:59:59.999'
How to solve this? I need to get all the records in that particular date.
Please help me !
If #FromDate is of data type datetime and Table.Date is also of data type datetime then:
Declare #FromDate datetime = '2014-06-02';
Select Table.Date
From Table
Where Table.Date >= #FromDate And Date < DateAdd(day, 1, Table.Date)
Above, we create an inclusive lower boundary (anything equal to or later than 2014-06-02) and an exclusive upper boundary (anything earlier than 2014-06-03), but with a variable defined just once. So, effectively the query checks 2014-06-02 <= FromDate < 2014-06-03.
If you convert DateTime to Nvarchar your issue would be solved.
Try this query:
Declare #Date datetime='2014-08-28 00:00:00.000'
select * from Table
where CONVERT(nvarchar(20),Date,105) = CONVERT(nvarchar(20),#Date,105)
Some of the dates in a column of dates were recorded wrong. I'd like to make a query which subtracts one day from each date IF the days are in a certain date range.
I know I'll have to use DATEADD and UPDATE, but I can't seem to figure it out. Thanks in advance.
This should do it:
UPDATE [SomeTable]
SET [DateColumn] = DATEADD(d, -1, [DateColumn])
WHERE [DateColumn] BETWEEN [Date1] AND [Date2]
Here's the MSDN doc's on the DATEADD function: http://msdn.microsoft.com/en-us/library/ms186819.aspx
When performing updates on data like this, it's always best to run a select statement first with the same criteria to ensure that you're updating the correct records. It also helps reduce the stress level of updating (especially if you're unfamiliar with SQL).
SELECT *, --Depending on what columns you would like to see, the wildcard could be replaced
DATEADD(d, -1, [DateColumn]) AS ProposedDate
FROM [SomeTable]
WHERE [DateColumn] BETWEEN [Date1] AND [Date2]
DECLARE #min_date datetime, #max_date datetime;
UPDATE yourtable
SET date_column = DATEADD(day, -1, date_column)
WHERE id IN (
SELECT id
FROM yourtable
WHERE date_column BETWEEN #min_date AND #max_date
);
You'll have to set appropriate values for #min_date and #max_date.
UPDATE MyTable
SET DateField = DATEADD(Day, -1, DateField)
WHERE Datefield BETWEEN '1/1/2011' AND '2/1/2011'
use UPDATE
set the data = ( do the date math )
WHERE the date is in the range you want
and the key is the row you want.
I wish to create a stored procedure which can retrieve the datetime less than or greater than current sys date.. in my table,startdate and enddate has the value as 'datetime'
How do I get details between startdate and enddate in SQL stored procedure?
thanks in advance
eg:
SELECT *
FROM MyTable
WHERE DATEDIFF ('d',mydatefield ,getdate() ) < 3
gets within 3 days
Considering this table definitionCREATE TABLE [dbo].[Dates](
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL
)
I assume that if you pass a date you want to know which rows satisfy the condition: startDate < date < EndDate. If this is the case you can use the query: select *
from Dates
where convert(datetime, '20/12/2010', 103) between StartDate and EndDate;
A stored procedure could look like: ALTER PROCEDURE [dbo].[GetDataWithinRange]
#p_Date datetime
AS
BEGIN
SELECT *
from Dates
where #p_Date between StartDate and EndDate;
END
It sounds like you're trying to filter data in a table based on a date range. If this is the case (I'm having some trouble understanding your question), you'd do something like this:
select *
from MyTable m
where m.Date between #DateFrom and #DateTo
Now, I'm assuming your filtering dates are put into the variables #DateFrom and #DateTo.
There are two things:
1> To get todays date we can write
SET #today_date = GETTDDT();
2> To get Current time we can us ethe following query:
SET #today_time = (SELECT
digits(cast(hour(current time) as decimal(2,0)))||
digits(cast(minute(current time) as decimal(2,0)))||
digits(cast(second(current time) as decimal(2,0)))
FROM sysibm/sysdummy1);