Select DATEADD minutes with query SQL Server 2005 - sql

SELECT
[NAMENO],
[FIRSTNAME],
[MIDNAME],
[LASTNAME],
[SEX],
[STATUSOF],
[RELEASEDT],
CONVERT(VARCHAR(10), DOBDT, 101) AS DOBDT
FROM
database
WHERE
ReleaseDT >= dateadd(minute,datediff(minute,60,GETDATE()),0)
AND ReleaseDT < dateadd(minute,datediff(minute,0,GETDATE()),0)
I am wanting to get anyone that has a "releasedt" in the past 60 mins if that makes of sense? right now its pull the last 60 days..
Any suggestions??

It's just:
where ReleaseDT >= dateadd(minute, -60, getdate())
and ReleaseDT <= getdate()
Nesting dateadd and datediff (like you did in your code in the question) is not necessary.
In fact, datediff calculates the difference (in hours, minutes...whatever) between two datetime values, so you don't need it at all when you just want to get "now minus 60 minutes".

Related

Searching data for current date in SQL Server

Why is the first query taking less time than the second one in SQL Server?
This query takes 4 seconds to complete:
select *
from salesinvmaster
where day(salesdate) = day(getdate()) and
month(salesdate) = month(getdate()) and
year(salesdate) = year(getdate())
This query takes 10 seconds:
select *
from salesinvmaster
where salesdate between '2017-11-01 00:00:00' and '2017-11-01 23:59:59'
The two queries are different, because today is some day in December, not November 1st.
My guess is that you do not have an index on the salesdate column, and that the first query is returning fewer rows -- hence, it looks faster. For the record, I would recommend writing the logic as one of the following:
where convert(date, salesdate) = convert(date, getdate())
where salesdate >= convert(date, getdate()) and
salesdate < dateadd(day, 1, convert(date, getdate()))
Note that SQL Server does use an index for the conversion of a date/time value to a date. This is one of the rare (only?) times when a function does not prevent the use of an index.
As for the second method, it dispenses with the need to include the time component of the values.
Check the Exeuction plan for the query i think We've got implicit conversions on the date! check this

SQL query to start from hour 12:00 am

I have a date column like this 7/24/2017 and when I write the below where clause I get results from hour 7/24/2017 1:00:00.000 AM. I need to get this from 7/24/2017 12:00:00.000 AM. How should this where clause me modified. Please check the following code
Date>= DATEADD(day, -1, convert(date, GETDATE())) and
Date< DATEADD(day, +0, convert(date, GETDATE()))
There doesn't appear to be anything wrong with your code. Are you certain your data contains any times before 1am? Are you certain you appreciate how your SQLServer will format those times/represent them to you?
See this SQLFIDDLE: http://sqlfiddle.com/#!6/047cc/11
Optimisations: you don't need to dateadd a value of 0. You can also subtract 1.0 from a date to get the day earlier and it's a bit less wordy than DATEADD. It works because internally dates are represented as floating point numbers of the number of days since a point in time:
[Date] >= convert(date, GETDATE() -1.0) and
[Date] < convert(date, GETDATE())

SQL Datetime as time query

I would like the below select statement to be show as time rather than datetime. I'm aware that a cast as solution is likely however given I am trying to Cast as a "grouped by time", I am not finding a solution.
SELECT
DATEADD(hour, DATEDIFF(hour, 0,dbo.tbReceiptLine.Time), 0) AS Hourly,
DATEADD(minute, DATEDIFF(minute, 0, dbo.tbReceiptLine.Time) / 30 * 30, 0) AS [30 Mins]
I would like to show this as time only.
In SQL Server 2008 onwards you can cast to time datatype:
SELECT CAST(dbo.tbReceiptLine.Time as time)
See The ultimate guide to the datetime datatypes

SQL datediff function resulted in an overflow

I am calculating TotalHours of a employee worked in office based on Intime and Outtime resultant in the form of hh.mm like 8.30.
So, I wrote below sql query :
SELECT EMPLOYEEID, sum(DateDiff(mi,isnull(In_Time,0),isnull(Out_Time,0))/60) +
sum(round(DateDiff(mi,isnull(In_Time,0),isnull(Out_Time,0))%60,2))/100.0 +
sum(round(DateDiff(ss,isnull(In_Time,0),isnull(Out_Time,0)),2))/10000.0 as
TotalHours from HR_EMPLOYEES
The above sql server query was running correctly intially, but now it is giving following exception:
java.sql.SQLException: The datediff function resulted in an overflow. The number of
dateparts separating two date/time instances is too large. Try to use datediff with a
less precise datepart.
Could anybody please help me to get rid off of this?
Is it not easier to just discard the null values (returning datediff in seconds from a datetime of zero is what's probably overflowing your query) and also use 'hh' for datediff? Or are you also looking for the number of minutes (do you NEED seconds?? If this is for timesheets etc, seconds don't really matter do they?)
SELECT
EMPLOYEEID,
CASE
WHEN In_Time IS NOT NULL AND Out_Time IS NOT NULL THEN sum(DateDiff(hh, In_Time, Out_Time))
ELSE 0
END as TotalHours
FROM HR_EMPLOYEES
Edit: ok for hours/minutes just use:
SELECT
EMPLOYEEID,
CASE
WHEN In_Time IS NOT NULL AND Out_Time IS NOT NULL THEN sum(DateDiff(mi, In_Time, Out_Time)) / 60.0
ELSE 0
END as TotalHours
FROM HR_EMPLOYEES
This gives you hours plus a fraction of minutes (so 90 minutes = 1.5 hours)
Edit2: if you want minutes as actual minutes and not a fraction use this:
SELECT
EMPLOYEEID,
CASE
WHEN In_Time IS NOT NULL AND Out_Time IS NOT NULL THEN
sum(DateDiff(hh, In_Time, Out_Time) + -- Total hours
DateDiff(mi, In_Time, Out_Time) % 60 * .01) -- Total minutes (divided by 100 so that you can add it to the hours)
ELSE 0
END as TotalHours
FROM HR_EMPLOYEES
You may try this:
select empid,
convert(varchar(5), sum(datediff(minute, [intime], isnull([outtime], dateadd(hh, 19, DATEADD(dd, DATEDIFF(dd, 0, [intime]), 0))))) / 60)
+ ':' +
convert(varchar(5),sum(datediff(minute, [intime], isnull([outtime], dateadd(hh, 19, DATEADD(dd, DATEDIFF(dd, 0, [intime]), 0))))) % 60)
as TotalHours
from HR_EMPLOYEES group by empid
Some thoughts:
Can intime ever be null? If so how and why? I am assuming intime can never be null
I am assuming that if outtime is null then, the employee is still working, thus the use of getdate() But it may also be the case that there was a software bug that caused the null.
Another strategy to handle null in outtime could be to make it the midnight of the intime day. Then this begs the question, how the next day will be handled.
I think there may be a lot of edge cases here. You will have to be careful.
EDIT: Modified outtime to 7 PM of intime day if outtime is null as per OP's comment. Used Best approach to remove time part of datetime in SQL Server
Instead of using 0 as your default/fixed point in time, use some other constant date instead, that's closer to the values you're going to be processing - and thus less likely to produce an overflow.
0 gets implicitly converted to midnight on 01/01/1900. A better constant might be, for instance, 01/01/2000:
SELECT EMPLOYEEID, sum(DateDiff(mi,isnull(In_Time,'20000101'),isnull(Out_Time,'20000101'))/60) +
sum(round(DateDiff(mi,isnull(In_Time,'20000101'),isnull(Out_Time,'20000101'))%60,2))/100.0 +
sum(round(DateDiff(ss,isnull(In_Time,'20000101'),isnull(Out_Time,'20000101')),2))/10000.0 as
TotalHours from HR_EMPLOYEES
Although the more I look at this, the more I'm unsure that defaulting the date to any value even makes sense.

DateDiff in SQL Server asking for help

I am using SQL Server 2008. I have a table which has a datetime type column called CreateTime. I need to select all the records from this table whose CreateTime is more than 3 days and half an hour from current time (UTC time). I am using T-SQL store procedure.
BTW: The CreateTime column is some time in the past time.
I have taken quite some time to learn and search for help from MSDN for DateDiff, but cannot figure out. Could anyone show me a sample please?
thanks in advance,
George
You can select and add a WHERE clause with a DATEDIFF using minutes:
SELECT (fields)
FROM (table)
WHERE
DATEDIFF(MINUTE, CREATETIME, getutcdate()) <= (3*24*60 + 30)
And of course, if you only wants those rows which are MORE than 3 days and 30 minutes away, just use the opposite:
WHERE
DATEDIFF(MINUTE, CREATETIME, getutcdate()) > (3*24*60 + 30)
A sample:
SELECT
DATEDIFF(MINUTE, '2009-08-01 08:00:00', getutcdate()),
DATEDIFF(MINUTE, '2009-07-31 20:00:00', getutcdate()),
DATEDIFF(MINUTE, '2009-07-23 20:00:00', getutcdate())
gives as result:
96 816 12337
So the first two dates are still within your 4350 minute bracket (less than 3 days and 30 minutes ago), while the third date is further away.
Marc
You need DATEADD:
WHERE DATEADD(minute, 4350, CreateTime) <= getutcdate()
Or, as you mentioned, you can use DATEDIFF:
WHERE DATEDIFF(minute, CreateTime, getutcdate()) <= 4350
(4350 is '3 days and 30 minutes' in minutes)
One minor quibble with the given answers, though they are correct. Don't apply the function to the column: apply the function to the comparison value.
If CREATETIME is indexed then it's a scan rather than seek with the function on the column.
You don't need millions of rows for this to be a problem.
Adapting the answer of marc_s:
SELECT (fields)
FROM (table)
WHERE
CREATETIME <= DATEADD(MINUTE, - (3*24*60 + 30), getutcdate())