I am running SQL Server 2008 and I have a datetime field, from this field I only want to extract the date and hour. I tried to parse out using datepart() but I think I did it incorrectly as when I attempted to string the two back together I got a number value. What I attempted was
Select Datepart(YEAR, '12/30/2015 10:57:00.000') +
Datepart(HOUR, '12/30/2015 10:57:00.000')
And what was returned was 2025 What I want to see returned is
12/30/2015 10:00:00.000
You could use DATEADD and DATEDIFF:
DECLARE #d DATETIME = '12/30/2015 10:57:00.000';
SELECT DATEADD(second,DATEDIFF(second,'1970-01-01',#d)/3600*3600 , '1970-01-01')
-- 2015-12-30 10:00:00
LiveDemo
How it works:
Get seconds difference from 1970-01-01
Divide by 3600 (integer division so the part after decimal point will be skipped)
Multiply by 3600 to get value back to full hours
Add calculated seconds number to 1970-01-01
With SQL Server 2012+ the neat way is to use DATETIMEFROMPARTS:
SELECT DATETIMEFROMPARTS(YEAR(#d), MONTH(#d), DAY(#d), DATEPART(HOUR, #d),0,0,0)
LiveDemo2
Related
I am trying to implement an SQL query that gets records between today's fixed timing 18:00 and yesterday's fixed timing 18:00 based on a Date time column that I have in my table.
I tried this query
DECLARE #today date = GETDATE()
SELECT *
FROM mytab
WHERE datetimecolumn Between #today-1 + TimeSerial(18,0,0)
And #today + TimeSerial(18,0,0)
But, it's throwing an error Timeserial is not a recognized built-in function name.
Any ideas please?
It would be best to use the DATEADD() function to add the time to the date you want. In this example, it subtracts 6 hours from the first number to get 18:00 yesterday, and then adds 18 hours to get 18:00 today.
DECLARE #today date = GETDATE();
SELECT * FROM mytab WHERE datetimecolumn Between DATEADD(hour,-6,#today) And DATEADD(hour,18,#today);
I am storing UTC datetime in Database
e.g.
2018-06-05 11:37:00.000 (UTC)
2018-06-05 17:07 (+5:30 India standart time)
I am having offset as :
offset as +02:00
How can I compare in sql query that now offset time matched ?
e.g.
2018-06-05 13:37:00.000
My issue is X (IST) date time converted to UTC and now I want to covert to different time zone (Y)
The following functions helped me to resolve the issue:
1. SWITCHOFFSET
2. TODATETIMEOFFSET
SELECT GETDATE()
SELECT SWITCHOFFSET(GETDATE(), '+05:30')
SELECT CONVERT(VARCHAR(19), SWITCHOFFSET(TODATETIMEOFFSET(GETDATE(), '+05:30'),'+00:00'), 120)
SELECT GETUTCDATE()
If I understand your question correctly, you can use the DATEADD function to calculate the new datetime based on the UTC datetime and the offset
For example:
2 hours = 120 minutes
DATEADD(minute, 120, '2018-06-05 11:37:00.000')
Or using hours
DATEADD(hour, 2, '2018-06-05 11:37:00.000')
You can also go the other way using negative numbers
You don't have to use a literal value, you can supply a column name to parameter 3 for use in a query and also use this expression as part of a where clause
Here's a cheat sheet for the DATEADD function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-2017
I've this:
SELECT GETDATE()
Which gives me :
2017-06-12 16:51:50.410
How can I convert this in order to get:
2017-06-12 16:00:00.000
I've some processes and I'm registering the date and hour of the job but I only want the exactly hour.
How can I do this?
Thanks
To get getdate() truncated to hour:
select dateadd(hour, datediff(hour, 0, getdate() ), 0)
This adds the number of hours since 1900-01-01 to the date 1900-01-01.
You can also swap hour for other levels of truncation: year, quarter, month, day, minute, second, et cetera.
I think the most human-readable solution is:
SELECT convert(datetime,FORMAT(GETDATE(),'yyyy-MM-dd HH:00:00.000'))
FORMAT to get the GETDATE timestamp exactly how you want it.
CONVERT to ensure the data type is a datetime.
How to get the date and hour information for a given datetime object in Transact-SQL?
E.g. 2014-12-18 21:00:00 for 2014-12-18 21:24:05.
I need to truncate off all parts after the hour - i.e. minutes, seconds, and partial seconds.
For SQL Server:
Select DATEPART(HOUR, [date]) + ":" + DATEPART(MINUTE, [date]);
Oracle:
Select TO_CHAR([date], 'HH:MI') From...
SELECT
CONVERT(TIME,GETDATE()) AS TimeOnly,
CONVERT(DATE,GETDATE(),101) AS DateOnly
GO
Replace the GETDATE() function with whatever you need.
I am trying to get the the result of in time and out time from dates
but it returns only hours using following select Query as follows
SELECT DATEDIFF(Hh,InTime,OutTime) as Diff_time from EmpLogTable
and i need result in HH:MM
Suppose my in time is 11 am and out is 5.49pm so o/p would be 6.49 but
using above select query i am getting o/p as 7 only
if any body has a solution then please let me know
Thanking you in Advance
Umesh Rakhe
The DATEDIFF function returns an INT so it will not work as you like, your best bet is to subtract InTime from OutTime or use DATEDIFF with minutes (n) instead.
you should probably do UI formatting on the client, not the database
you can use diff = datediff(mi, intime, outtime) to get the difference in minutes
then divide diff by 60 to get the hours
and take the modulus diff % 60 to get the remaining minutes
then turn into strings and you're good to go
SELECT Outime - InTime as Diff_time from EmpLogTable
DATEDIFF measures day, hour etc boundaries: you're asking for a true date/time difference. In this case, I'd simply subtract the values rather than using a complex nested DATEDIFF. Or do it in the client.
If you have a interval > 24 hours though, then you'd need a nested DATEDIFF to get 25 hours: my answer would give one day and one hour.
The hh:nn:ss format can be done via CONVERT with style 108, but again I'd do this in the client