In the database I have timezone and date as :
Timezone
'India Standard Time'
DateTime
'2018-07-19 15:47'
In Sql query I am comparing this with GETUTCDATE()
IF EXISTS( SELECT * FROM [dbo].[Notification] WHERE [Published] AT TIME ZONE 'India Standard Time' < GETUTCDATE())
BEGIN
PRINT 'True'
END
This give me wrong result
I am new with this function so don't know exact way to compare it
just use
dateadd(MINUTE, 330, getutcdate())
in place of
getutcdate()
for IST
SELECT * FROM [dbo].[Notification] WHERE [Published] < dateadd(MINUTE, 330, getutcdate())
There are many approaches to get this working -
My approach is to convert your GETUTCDATE to IST so that you can run your query without any timezone issues.
SELECT * FROM [dbo].[Notification]
WHERE [Published] < CONVERT(datetime,SWITCHOFFSET(CONVERT(datetimeoffset,GETUTCDATE()),’+05:30′))
Related
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 to enter manual time stamp in get date () ?
select conver(varchar(10),getdate(),120)
returns 2010-06-07
now i want to enter my own time stamp in this like
2010-06-07 10.00.00.000
i m using this in
select * from sample table where time_stamp ='2010-06-07 10.00.00.000'
since i m trying to automate this query i need the current date but i need different time stamp can it be done .
You just want to append a time to your result? Like this?
select convert(varchar(10),getdate(),120) + ' 10.00.00.000'
or if you want to get it back to a DATETIME type:
select convert(datetime,convert(varchar(10),getdate(),120) + ' 10:00')
--SQL Server 2008
DECLARE #MyTime time, #MyDate date
SELECT #MyDate = GETDATE(), #MyTime = '10:00:00'
SELECT CAST(#MyDate AS datetime) + #MyTime
--SQL Server 2005 and before
DECLARE #MyTime datetime, #MyDate datetime
SELECT
#MyDate = DATEADD(day, 0, DATEDIFF(day, 0, GETDATE())),
#MyTime = '19000101 10:00:00'
SELECT #MyDate + #MyTime
"zero" date = 01 Jan 1900 in SQL Server
SELECT DATEADD(hh, 1, FLOOR(CAST(GETDATE() AS FLOAT)))
Once you have the floor of the date, you can add time to it.
DATEADD(datepart, number, date)
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)
How do I convert hh:mm:ss to hh:mm in SQL Server?
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date between '2009-05-04 00:00:00' and '2009-05-06 14:58'
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
CONVERT(VARCHAR(5),Date, 108)-- Gets only HH:mm
In general, the set of timestamps is not well-ordered, this means you cannot get a "last" timestamp whose time part up to minutes is 2009-05-06 14:58.
In SQL Server, which keeps the time part of a datetime as a number of 1/300 second fractions after midnight, this "last" timestamp would be 2009-05-06 14:58:59.997, but this is not guaranteed to be compatible with future releases of with other TIMESTAMP storage methods.
That means you'll need to split your BETWEEN condition into two conditions, one of which being strict less than the next minute:
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date >= '2009-05-04 00:00:00'
AND Date < DATEADD(minute, 1, '2009-05-06 14:58')
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
This solution will efficiently use indexes on Date
SELECT Convert(varchar(5), GetDate(), 108)
Using varchar(5) will automatically truncate the date to remove the seconds.
I dont think there is a built in function; usually do something like this
SET #time = '07:45'
SET #date = CONVERT(DATETIME,#time)
SELECT #date
SELECT LEFT(CONVERT(VARCHAR,#date,108),5)
For this specific need you should use the between method as noted by Quassnoi's answer. However, the general problem can be solved with:
select dateadd(second, -datepart(second, #date), #date)
One way would be to use the RIGHT() function to crop the Date. Something like:
RIGHT(CONVERT(VARCHAR(8),Date, 108),5)
This will only work if number of characters is constant e.g. there is a leading zero if applicable. (Sorry havn't got SQL server here to test).
A better way is to use the T-SQL datepart function to split and then re-concatinate the date parts so:
DARTPART("hh", CONVERT(VARCHAR(8),Date, 108))+":"+DARTPART("mi", CONVERT(VARCHAR(8),Date, 108))
References:
http://msdn.microsoft.com/en-us/library/ms174420.aspx
http://msdn.microsoft.com/en-us/library/ms187928.aspx
http://msdn.microsoft.com/en-us/library/ms177532.aspx
To get hh:mm format from today's date:
select getdate()
select convert(varchar(5),getdate(),108)
To get hh:mm format from any datetime column in a table data:
select date_time_column_name from table_name where column_name = 'any column data name'
select convert(varchar(5),date_time_column_name,108) from table_name
where column_name = 'any column data name'
select creationdate from employee where Name = 'Satya'
select convert(varchar(5),creationdate,108) from employee
where Name = 'Satya'
SELECT CAST(CAST(GETDATE() AS Time(0)) AS VARCHAR(5))