I know this should be easy and I do it no probelms in mySQL and Postgresql but I'm struggling with SQL Server. I want to select rows with a datetimeoffset field that's over an hour old.
select * from table where mydatetime < getdate() - 1 hour
I've tried dateadd and datediff but can't get it right.
WHERE mydatetime < DATEADD(hour, -1, SYSDATETIMEOFFSET())
For more see: DATEADD (Transact-SQL)
select * from table where mydatetime < dateadd(hh, -1, getdate())
if you dont want to use dateadd(), you can use the current timestamp - #seconds
SELECT * FROM `table` where `mydatetime` < (CURRENT_TIMESTAMP - 3600)
Related
I need to use today's date in WHERE clause of a statement as 11:59 PM for today’s date. for eg. if today is 09/04/2018 then I want '09/04/2018 23:59'. I tried the following but didn't work:
DECLARE #today SMALLDATETIME
SET #today = DATEADD(month, ((YEAR(GETDATE()) - 1900) * 12) + MONTH(GETDATE()) - 1, DAY(GETDATE()) - 1) + ' 23:59:59'
PRINT #today
Please guide
If you like, you can do this with date arithmetic:
SET #today = dateadd(minute, -1, cast(dateadd(day, 1, cast(getdate() as date)) as smalldatetime));
I am concerned about trying to get the last minute of the day. That suggests that you might be trying to use between or <= on datetimes. Usually it is better to avoid constructs and use <.
You could use DATETIMEFROMPARTS:
DECLARE #t DATE = GETDATE();
SELECT DATETIMEFROMPARTS(YEAR(#t),MONTH(#t),DAY(#t),23,59,0,0);
DBFidde Demo
I have a query -
SELECT * FROM TABLE WHERE Date >= DATEADD (day, -7, -getdate()) AND Date <= getdate();
This would return all records for each day except day 7. If I ran this query on a Sunday at 17:00 it would only produce results going back to Monday 17:00. How could I include results from Monday 08:00.
Try it like this:
SELECT *
FROM SomeWhere
WHERE [Date] > DATEADD(HOUR,8,DATEADD(DAY, -7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))) --7 days back, 8 o'clock
AND [Date] <= GETDATE(); --now
That's because you are comparing date+time, not only date.
If you want to include all days, you can trunc the time-portion from getdate(): you can accomplish that with a conversion to date:
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -convert(date, getdate())
AND Date <= convert(date, getdate());
If you want to start from 8 in the morning, the best is to add again 8 hours to getdate.
declare #t datetime = dateadd(HH, 8, convert(datetime, convert(date, getdate())))
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -#t) AND Date <= #t;
NOTE: with the conversion convert(date, getdate()) you get a datatype date and you cannot add hours directly to it; you must re-convert it to datetime.
Sounds like you want to remove the time. Correct? If so then do the following.
SELECT * FROM TABLE WHERE Date >= (DATEADD (day, -7, -getdate()) AND Date DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
How would I show something in SQL where the date is greater than the current date?
I want to pull out data that shows everything greater from today (now) for the next coming 90 days.
I was thinking =< {fn NOW()} but that doesnt seem to work in my sql view here.
How can this be done?
SELECT *
FROM MyTable
WHERE CreatedDate >= getdate()
AND CreatedDate <= dateadd(day, 90, getdate())
http://msdn.microsoft.com/en-us/library/ms186819.aspx
Assuming you have a field for DateTime, you could have your query look like this:
SELECT *
FROM TABLE
WHERE DateTime > (GetDate() + 90)
In sql server, you can do
SELECT *
FROM table t
WHERE t.date > DATEADD(dd,90,now())
For those that want a nice conditional:
DECLARE #MyDate DATETIME = 'some date in future' --example DateAdd(day,5,GetDate())
IF #MyDate < DATEADD(DAY,1,GETDATE())
BEGIN
PRINT 'Date NOT greater than today...'
END
ELSE
BEGIN
PRINT 'Date greater than today...'
END
For SQL Server
select *
from YourTable
where DateCol between getdate() and dateadd(d, 90, getdate())
Select * from table where date > 'Today's date(mm/dd/yyyy)'
You can also add time in the single quotes(00:00:00AM)
For example:
Select * from Receipts where Sales_date > '08/28/2014 11:59:59PM'
If you're using SQL Server it would be something like this:
DATEDIFF(d,GETDATE(),FUTUREDATE) BETWEEN 0 AND 90
I want to convert a value from bigint to datetime.
For example, I'm reading the HISTORY table of teamcity server. On the field build_start_time_server, I have this value on one record 1283174502729.
How can I convert it to a datetime value?
Does this work for you? It returns 30-8-2010 13:21:42 at the moment on SQL Server 2005:
select dateadd(s, convert(bigint, 1283174502729) / 1000, convert(datetime, '1-1-1970 00:00:00'))
I've divided by 1000 because the dateadd function won't work with a number that large. So you do lose a little precision, but it is much simpler to use.
Slightly different approach:
Your scenario:
SELECT dateadd(ms, 1283174502729 / 86400000, (1283174502729 / 86400000) + 25567)
FROM yourtable
Generic code:
SELECT dateadd(ms, yourfield / 86400000, (yourfield / 86400000) + 25567)
FROM yourtable
Output:
August, 30 2010 00:00:14
SQL Fiddle: http://sqlfiddle.com/#!3/c9eb5a/2/0
CAST(SWITCHOFFSET(CAST(dateadd(s, convert(bigint, [t_stamp]) / 1000, convert(datetime, '1-1-1970 00:00:00')) AS DATETIMEOFFSET), DATENAME (TZoffset, SYSDATETIMEOFFSET())) AS DATETIME)
The following takes new SQL terminology into account and will return the milliseconds (can also be modified for use in a calculated field.) [SQL Server 2012 or later]
declare #StartDate datetime2(3) = '1970-01-01 00:00:00.000'
, #milliseconds bigint = 1283174502729
, #MillisecondsPerDay int = 60 * 60 * 24 * 1000 -- = 86400000
SELECT DATEADD(MILLISECOND, TRY_CAST(#milliseconds % #millisecondsPerDay AS
INT), DATEADD(DAY, TRY_CAST(#milliseconds / #millisecondsPerDay AS INT),
#StartDate));
select Cast(Cast(19980324 as nvarchar) as Datetime)
If you want precision in milliseconds to be maintained then you could do as follows. Works on SQL server 2016
SELECT dateadd(ms, ((CONVERT(bigint, build_start_time_server)%1000)),
dateadd(ss, ((CONVERT(bigint, build_start_time_server)/1000)%60),
dateadd(mi, ((CONVERT(bigint, build_start_time_server)/1000)/60), '1970-01-01'))) FROM yourtable
The answer I got was
Monday, August 30, 2010 1:21 PM
To convert bigint to datetime/unixtime, you must divide these values by 1000000 (10e6) before casting to a timestamp.
SELECT
CAST( bigIntTime_column / 1000000 AS timestamp) example_date
FROM example_table
Simple and easy solution which won't require any added library or function to be imported
DATEADD(second,YourValue, CAST('1970-01-01 00:00:00' AS datetime))
Did you try FROM_UNIXTIME?
select from_unixtime('your_field') from 'your_table'
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_from-unixtime
Works for me.
How do you get the 30 days before today in SQL.
T-SQL
declare #thirtydaysago datetime
declare #now datetime
set #now = getdate()
set #thirtydaysago = dateadd(day,-30,#now)
select #now, #thirtydaysago
or more simply
select dateadd(day, -30, getdate())
(DATEADD on BOL/MSDN)
MYSQL
SELECT DATE_ADD(NOW(), INTERVAL -30 DAY)
( more DATE_ADD examples on ElectricToolbox.com)
In MS SQL Server, it is:
SELECT getdate() - 30;
SELECT (column name) FROM (table name) WHERE (column name) < DATEADD(Day,-30,GETDATE());
Example.
SELECT `name`, `phone`, `product` FROM `tbmMember` WHERE `dateofServicw` < (Day,-30,GETDATE());
Try adding this to your where clause:
dateadd(day, -30, getdate())