Convert numeric column to time - sql

I have a table of employee shifts which include shift start time and shift end time. The start time and end time columns are numeric with a full stop between hour and minute:
8.00
15.10
7.00
22.00
7.00
How can I convert these columns to time? I also need to calculate the difference between start time and end time in hours and minutes to work out the shift length.

In Sql Server use following code
declare #seconds numeric
set #seconds = 12366
select convert(char(8), dateadd(second, #seconds, ''), 114)

In Sql Server use following code
SELECT CONVERT(DATETIME, CONVERT(CHAR(9), CURRENT_TIMESTAMP, 112) + '22:00');
You can replace CURRENT_TIMESTAMP to your date
Ex:
SELECT CONVERT(DATETIME, CONVERT(CHAR(9), CONVERT(DATE,'2022-01-25'), 112) + '22:00');

Related

How do I add Time in SQL?

So my data is 1:30 PM. I want to have a Data when user selects a time, I want it to add 1hr 30mins to that and make the data as 1:30 PM - 3:00 PM. I've been searching for an answer and tried some workarounds but i couldn't get it. BTW the datatype of my Time is Varchar, because when I tried using the Time datatype I had some issues on inserting from C# to SQL
tested with sql-server 2008
declare #t varchar(25)
set #t = '1:30 PM'
--conert a varchar to time
select CONVERT(time, #t)
--if you want to add 90 minutes to it
select DATEADD(minute,90,CONVERT(time, #t))
--this does a few things
-- 1) converts the time stored as a varchar to the time datatype
-- 2) adds 90 minutes
-- 3) converts the time result back to the varchar datatype
select convert(varchar(10), dateadd(mi, 90, convert(time, '1:30 PM')), 100)
--this will show a final result of "3:00PM"
EDIT:
The following query will also return a varchar with the space between before the AM/PM.
select format(dateadd(mi, 90, convert(datetime, '1:30 PM')), 'h:mm tt')
The 'h:mm tt' is written with one "h" so that it will show up as "3:00 PM", instead of "03:00 PM".
Using Replace:
This would be how I would use replace to get to the goal of "3:00 PM"
select replace(replace(convert(varchar(10), dateadd(mi, 90, convert(time, '1:30 PM')), 100), 'P', ' P'), 'A', ' A')

How to assign current date with specific time to column?

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
/

how to add 2 varchar columns in sql

I have timediff field as varchar
I want to find sum of this field but it gives error as
"Syntax error converting the varchar value '02:00' to a column of data type int."
My timediff is like
02:00
03:00
04:00
i want to add and should display 9 in sql
please help me
thanks for helping me
You can't use SUM on a varchar column. Take a look at the documentation here.
You haven't given us much information about what you are storing or how you want it summed, but your best bet might be to parse out the number before your colon, convert it to an int and sum that.
Good luck!
If you are talking about MS SQL
You could do something like this
first convert your hour representation to minutes by this
select (CONVERT(int,SUBSTRING('02:00',1,2))*60) +
CONVERT(int,SUBSTRING('02:00',4,2))
Then use that minutes representation to perform addition like this
select
DATEADD(
minute,
(select (CONVERT(int,SUBSTRING('02:00',1,2))*60) +
CONVERT(int,SUBSTRING('02:00',4,2))),
getdate())
If your final goal is to get sum of all datadiff records in hh:mm then first store datediff in minutes instead of hh:mm which you asked here.
How to store DateDiff in minutes:
SELECT DATEDIFF(second,CAST(<timepart> AS DATETIME)
, CAST(<timepart> AS DATETIME))/60.0 AS MIN_DIFF;
For example
SELECT DATEDIFF(second,CAST('10:00 AM' AS DATETIME)
, CAST('11:15 AM' AS DATETIME))/60.0 AS MIN_DIFF;
How to do sum of all minutes in hh:mm
SELECT CAST((<total minutes> / 60) AS VARCHAR(8)) + ':'
+ CAST((<total minutes> % 60) AS VARCHAR(2))
For example
SELECT CAST((390 / 60) AS VARCHAR(8)) + ':'
+ CAST((390 % 60) AS VARCHAR(2))
See this fiddle for all your answers.

Convert seconds to datetime in SQL Server

How to convert seconds to datetime? I try this, but results are not correct:
CONVERT(datetime, DATEADD(ms, dateTimeInSeconds, 0))
Here is an example: 1900-01-15 21:58:16.287 it's should be something like this 2010-11-02 14:56:50.997
Given your example, try this:
select DATEADD(s, dateTimeInMilliseconds, '19700101')
When you use the value zero for date, this is converted to 1900-01-01. Use the specific date that you have selected as epoch:
convert(datetime, dateadd(ms, dateTimeInMilliseconds, '2010-01-01'))
Note that the datetime data type doesn't have millisecond precision, the resolution is 1/300 second. If you for example have four milliseconds and convert it, you get 2010-01-01 00:00:00.003 rather than 2010-01-01 00:00:00.004. If you need to preserve the millisecond resolution, you need to use the datetime2 data type:
convert(datetime2, dateadd(ms, dateTimeInMilliseconds, cast('2010-01-01' as datetime2)))
Edit:
To use seconds instead of milliseconds, use s instead of ms in the dateadd call:
convert(datetime, dateadd(ms, dateTimeInSeconds, '1970-01-01'))
Informative time span string:
declare #seconds int = 93825
convert(varchar,(#seconds/86400)) + 'd:' + format(dateadd(ss,#seconds,0),'HH\h:mm\m:ss\s')
Result: 1d:02h:03m:45s

Efficient way to convert second to minute and seconds in sql server 2005

Suppose I have 90 seconds. If I want to display the result in terms of minutes and second, I do it by using
select Time= '0' + CAST( 90/60 as varchar(2)) + ':' + CAST( 90%60 as varchar(2))
The output is
Time
01:30
I have appended 0(zero) because if you do a select getdate() the output will be
yyyy-mm-dd hh:mm:ss:ms
What is the standard way and recommended practice to do such a conversion?
Thanks
With hours:
SELECT CONVERT(CHAR(8),DATEADD(second,90,0),108)
00:01:30
Ignoring hours:
SELECT RIGHT(CONVERT(CHAR(8),DATEADD(second,90,0),108),5)
01:30
Try this:
select convert(varchar(10), dateadd(second, 15794, 0), 108)
One of the first things I do on a fresh SQL database is add a Timespan function similar to this one (although I tend to include days and milliseconds as well):
CREATE FUNCTION dbo.TimeSpan
(
#Hours int,
#Minutes int,
#Seconds int
)
RETURNS datetime
AS BEGIN
RETURN DATEADD(SS, #Hours * 3600 + #Minutes * 60 + #Seconds, 0)
END
Then you can format this however you want:
SELECT SUBSTRING(CONVERT(char(8), dbo.TimeSpan(0, 0, 90), 108), 4, 5)
It might look more complicated at first, but the ability to reuse the TimeSpan function comes in very handy over time. For me it feels like a hack to always be writing DATEADD calls against 0 or '1753-01-01'.