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.
Related
I need to take diffrence of two dates which are columns in my table:
LastAIResponseReceivedDate,LastUploadedDate
I am Using this statement:
update Group set AITime=(CONVERT (varchar,(LastAIResponseReceivedDate-LastUploadedDate),121)) where GroupId=2 and Id=5
I am getting the time diffrence correctly but my date diffrence is wrong 1900-01-01 01:56:56.427
How to get the date diffrence correctly
Do not understand in your question, that what type of difference you want (Day, month, year).
But this may help you.
SELECT DATEDIFF(month, 'Start_date_variable', 'End_date_variable') AS DateDiff;
Syntax As:
DATEDIFF(interval, date1, date2)
Interval can be anything like day, month, year, week, hours, second etc..
Parse to datetime first, you need
Set timediff = DATEDIFF(s, LastAIResponseReceivedDate,LastUploadedDate)
Save time difference as second is more convenient for later use. If you want to save in some date format, remember not datetime, time difference is not a 'datetime'.
So,
Select CONVERT(varchar, FLOOR(timediff/(3600*24))) + 'D ' + CONVERT(varchar, DATEADD(s, timediff, 0), 114)
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
I have two columns in MSSQL 2008 R2: Workingdate and Startime.
Due to a mistake in the design of the application starttime was not defined as date+time, but only time (e.g. 1899-12-30 16:00:00.000 for 4:00 PM)
Now, I am facing a Problem that I cannot add date and time.
Why is
select convert(datetime,'2015-07-01 00:00:00.000') + convert(datetime,'1899-12-30 16:00:00.000')
2015-06-29 16:00:00.000 and not 2015-07-01 16:00:00.000
Thanks your help
Sorry: I found the solution: Date comes from VBA and the first day is 1899-12-30 and not 1900-01-01 as in SQL !!! Therefore I have to add +2 !!
Try this:
select dateadd(second,
datepart(hour,'1899-12-30 16:00:00.000') * 3600 +
datepart(minute,'1899-12-30 16:00:00.000') * 60 +
datepart(second,'1899-12-30 16:00:00.000'),
'2015-07-01 00:00:00.000')
The idea is to use DATEADD function to add the needed time in seconds to your base date (2015-07-01 00:00:00.000). In order to convert the record time to seconds we are using the DATEPART function three time to extract the hours, the minutes and the seconds from your record date.
To fix the date in your table you can use something like this:
UPDATE [dbo].[MyTable]
SET [Date] = DATEADD(SECOND, DATEPART(HOUR, [Date]) * 3600 + DATEPART(MINUTE, [Date]) * 60 + DATEPART(SECOND, [Date], '2015-07-01 00:00:00.000');
You can't easily use dateadd for this, the easy way is adding the time from one column with the datetime of the other column like this:
SELECT
cast('1899-12-30 16:00:00.000' as time)+
cast('2015-07-01 00:00:00.000' as datetime)
Result:
2015-07-01 16:00
Normally i would always use DATEADD for this type of operation.
Hope it helps
How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/
Goal:
To display data with this list:
Hour
-----
2
2,5
1
Problem:
Is it any possibiltiy to convert column StartTime and EndTime into specific datatype and then doing a calculacution of X1- X2
CREATE TABLE Data
(
StartTime VARCHAR(5),
EndTime VARCHAR(5),
)
GO
INSERT INTO Data(StartTime,EndTime)
SELECT '10:00','12:00' UNION ALL
SELECT '13:30','16:00' UNION ALL
SELECT '14:00','15:00' UNION ALL
GO
EDITED: To get mins in decimals
SELECT ROUND(CAST( Mins AS FLOAT)/60,2) AS [hour]
FROM (
SELECT DATEDIFF(minute, CAST(StartTime AS TIME),CAST(EndTime AS TIME)) AS Mins
FROM Data
) A
SELECT DATEDIFF(mi, CONVERT(DATETIME, StartTime), CONVERT(DATETIME, EndTime))
/ 60.0 AS Hour
FROM Data
It work's for your example data, but you should check if EndTime could be the next day, like:
StarTime: 22:30
EndTime: 01:20
Is that escenario possible? If it is, you must store the date for both Start and End times, not only the hour.
If you are using SQL-SERVER 2008, then why not just use the time object. You could then run the appropriate time comparison functions. If not, you can still make it work by converting to a datetime and running comparison functions. I believe a convert with just a time to a datetime will result in the date as the minimum date.