T-SQL query with date range - sql-server

I have a fairly weird 'bug' with a simple query, and I vaguely remember reading the reason for it somewhere a long time ago but would love someone to refresh my memory.
The table is a basic ID, Datetime table.
The query is:
select ID, Datetime from Table where Datetime <= '2010-03-31 23:59:59'
The problem is that the query results include results where the Datetime is '2010-04-01 00:00:00'. The next day. Which it shouldn't.
Anyone?
Cheers
Moo

Take a look at How Are Dates Stored In SQL Server? and How Does Between Work With Dates In SQL Server?
If that is a smalldatetime it has 1 minute precision so if rounds up, for datetime it is 300 miliseconds
example
DECLARE #d DATETIME
SELECT #d = '2001-12-31 23:59:59.999'
SELECT #d
2002-01-01 00:00:00.000
DECLARE #d DATETIME
SELECT #d = '2001-12-31 23:59:59.998'
SELECT #d
2001-12-31 23:59:59.997
Always use less than next day at midnight, in your case
< '20100401'

try doing it like:
select ID, Datetime from Table where Datetime < '2010-04-01'
I always floor the datetime and increment the day and just use "<" less than.
to floor a datetime to just the day use:
SELECT DATEADD(day,DATEDIFF(day,0, GETDATE() ),0)
you can easily increment a datetime by using addition:
SELECT GETDATE()+1
by using the '23:59:59' you can miss rows, try it out:
DECLARE #YourTable table (RowID int, DateOf datetime)
INSERT INTO #YourTable VALUES (1,'2010-03-31 10:00')
INSERT INTO #YourTable VALUES (2,'2010-03-31')
INSERT INTO #YourTable VALUES (3,'2010-03-31 23:59:59')
INSERT INTO #YourTable VALUES (4,'2010-03-31 23:59:59.887')
INSERT INTO #YourTable VALUES (5,'2010-04-01')
INSERT INTO #YourTable VALUES (6,'2010-04-01 10:00')
select * from #YourTable where DateOf <= '2010-03-31 23:59:59'
OUTPUT
RowID DateOf
----------- -----------------------
1 2010-03-31 10:00:00.000
2 2010-03-31 00:00:00.000
3 2010-03-31 23:59:59.000
(3 row(s) affected
this query is wrong, because it does not find the missed rowID=4 record.
if you try to fix this with:
select * from #YourTable where DateOf <= '2010-03-31 23:59:59.999'
then RowID=5 will be included as well, which is wrong.

It's very odd that you are seeing that; I don't know why. But I will suggest that you write the query this way instead:
select ID, Datetime from Table where Datetime < '2010-04-01'

Related

SQL: How to query all data within the month from the given date?

I have a table tbl1 of ID Numbers and their corresponding Membership Date.
I want to create a stored procedure which has a #member_date parameter provided. I want the procedure to get the month and year of #member_date to get all the records covered by the month and date
ID_Number Member_Date
10001 12/1/2015
10002 12/15/2015
10003 10/9/2015
10004 12/13/2014
Something like below:
#member_date = '12/25/2015'
Select * from tbl1 where month(Member_Date)=12 and year(Member_Date)=2015
Output:
ID_Number Member_Date
10001 12/1/2015
10002 12/15/2015
Thank you for your help
Greatly appreciated
Still I am not getting you proper, but I think you want something like below,
CREATE TABLE RYANTABLE (ID_NUMBER INT, MEMBER_DATE DATE)
INSERT INTO RYANTABLE VALUES (10001,'12/1/2015')
INSERT INTO RYANTABLE VALUES (10002,'12/15/2015')
INSERT INTO RYANTABLE VALUES (10003,'10/9/2015')
INSERT INTO RYANTABLE VALUES (10004,'12/13/2014')
You need store procedure right like below ?
--EXEC RyanProcedure '12/25/2015'
CREATE PROCEDURE RyanProcedure
#member_date DATE
AS
BEGIN
DECLARE #MONTH AS INT=MONTH(#member_date)
DECLARE #Year AS INT=YEAR(#member_date)
SELECT *
FROM RyanTable
WHERE (MONTH(Member_Date) = #MONTH
AND YEAR(Member_Date) = #Year)
OR (Member_Date = #member_date)
END
you can use
DATE_FORMAT(<date>, <date_format>)
for example
SELECT * FROM TBL1 WHERE DATE_FORMAT(Member_Date, '%Y%m') = '201512'
%Y Year, numeric, four digits,
%m Month, numeric (00-12)
for date format, you can see in here
You should make your query Sargable for best performance
DECLARE #member_date date= '12/25/2015'
SELECT *
FROM
tbl1
WHERE
Member_Date >= dateadd(month, datediff(month, 0, #member_date), 0)
Member_Date < dateadd(month, datediff(month, -1, #member_date), 0)

How to compare datetime in SQL Server in where clause

I have CreatedDate as datetime column in my database table. I want to fetch the rows where CreatedDate and current time difference is more than 1 hour
Select * from TableName where (DateDiff(hh,CreatedDate,GetDate())>1
Answer by #Amit Singh works if you only care about the hour value itself, versus any 60 minute period.
The problem with using DATEDIFF(hh) that way is that times of 13:01 and 14:59 are only one "hour" apart.
Like:
select datediff(hh,'1/1/2001 13:59','1/1/2001 14:01')
I think doing this would address that issue:
declare #cd datetime='9/12/2013 03:10';
declare #t table(id int,CreatedDate datetime);
insert #t select 1,'9/12/2013 02:50';
insert #t select 2,'9/12/2013 02:05';
select * from #t where #cd>(DateAdd(hh,1,CreatedDate))
Dan Bellandi raises a valid point, but if it really matters if the dates should be 60 minutes apart, then just check if they are 60 minutes apart:
SELECT * FROM TableName WHERE DATEDIFF(MINUTE, DateColumnName, GETDATE()) >= 60
If you don't expect any rows created in the future...
where CreatedDate < dateadd(hour, -1, getdate())
CREATE TABLE trialforDate
(
id INT NULL,
NAME VARCHAR(20) NULL,
addeddate DATETIME NULL
)
INSERT INTO trialforDate VALUES (1,'xxxx',GETDATE())
INSERT INTO trialforDate VALUES (2,'yyyy',GETDATE())
INSERT INTO trialforDate VALUES (1,'zzzz','2013-09-12 11:20:40.533')
SELECT *
FROM trialforDate
WHERE GETDATE() > DATEADD(HOUR, 1, addeddate)
C# Code
DateTime param1= System.DateTime.Now;
DateTime param2= System.DateTime.Now.AddHours(1);
SQL Query:
SELECT * FROM TableName WHERE CreatedDate = param1 AND CreatedDate =param2;

date comparison in sql server

I am trying to display records which have their date (I have a column Date in table) 30 days back from today's date. And once it gets displayed I need to make a new record by adding details with Date= today's date..
I tried this:
select * from
paymenthist
where
Date = CONVERT(datetime, CONVERT(varchar, DATEADD(day, -30, GETDATE()), 101))
But all records are getting displayed..
Ok, I admit the way I suggested may be inefficient, but if one is a datetime and the other is a date then I believe this will be more efficient than the >= <= approach because SQL is often not great at utilising indexes for queries like this, and under the covers a datetime is actually a floating point, so for pure efficiency, try this:
CREATE TABLE ##PaymentHistory
(
ID INT IDENTITY,
[Date] DATETIME,
Col1 INT,
Col2 INT
)
INSERT INTO ##PaymentHistory([Date],Col1,Col2)
VALUES(FLOOR(CAST(GETDATE() -29 AS FLOAT) ) ,1,1)
, (FLOOR(CAST(GETDATE() -30 AS FLOAT) ) ,2,2)
, (FLOOR(CAST(GETDATE() -31 AS FLOAT) ) ,3,3)
SET IDENTITY_INSERT ##PaymentHistory ON
INSERT INTO ##PaymentHistory(ID, [Date], Col1, Col2)
SELECT ID, GETDATE(), Col1, Col2
FROM ##PaymentHistory
WHERE CAST(Date AS FLOAT) = FLOOR(CAST(GETDATE() -30 AS FLOAT) )
SET IDENTITY_INSERT ##PaymentHistory OFF
It depends somewhat on the datatype of the date column, but try this.
select * from paymenthist where cast(Date as date) = cast(DATEADD(day, -30, GETDATE()) as date)

Sum of datetime column in sql 05

I have a column named as "total_hours_worked" in my sql table which is of "datetime" datatype
I want to find out the total of "total hours worked in sql server".
How to do this?
I googled but didn't got a practical solution.
Something like this if I understand your data correctly.
declare #T table
(
total_hours_worked datetime
)
insert into #T values ('05:30:00')
insert into #T values ('10:00:00')
insert into #T values ('15:00:00')
select sum(datediff(minute, 0, total_hours_worked)) / 60.0 as hours_worked
from #T
Result:
hours_worked
---------------------------------------
30.500000
If you only need to store the hours you should consider an integer datatype instead of datetime. It will be more efficient and easier to deal with.
Try this.Here I considered seconds also.
declare #T table
(
total_hours_worked datetime
)
insert into #T values ('05:30:00')
insert into #T values ('10:00:00')
insert into #T values ('15:00:00')
insert into #T values ('05:25:45')
select SUM((DATEPART(hh,total_hours_worked)*60)+DATEPART(mi,total_hours_worked)+(DATEPART(ss,total_hours_worked)/(60.0)))/60.0 as TotalHours from #T

IsNumeric Time value problem

Table1
Time
10:00:00
12:00:00
Absent
14:00:00
Holiday
...,
Time column Datatype is varchar
I want to check the time column value is numeric then 'Present'
Tried Query
Select case when isnumeric(time) then 'Present' else time end as time from table1
Showing error in 'then'
How to modify my query according to my requirement
Need Query Help
Try using ISDATE
Returns 1 if the expression is a valid
date, time, or datetime value;
otherwise, 0.
Something like
DECLARE #Table TABLE(
[Time] VARCHAR(50)
)
INSERT INTO #Table SELECT '10:00:00'
INSERT INTO #Table SELECT '12:00:00'
INSERT INTO #Table SELECT 'Absent'
INSERT INTO #Table SELECT '14:00:00'
INSERT INTO #Table SELECT 'Holiday'
SELECT *,
ISDATE(Time),
case when ISDATE(time) != 0 then 'Present' else time end
FROM #Table

Resources