PROGRESS ODBC SQL Covert string into time - sql

i need to convert string into time. I have 2 columns, one of them have date in good format but time looks like this:
Time
_____
061923
060239
134803
135011
first 2 characters are hours, second 2 are minutes and last 2 are seconds. How to convert it into format like "HH:MM:SS" ? Im using progress odbc.

You can insert ':' into the string and convert to a time:
select cast( (left(col, 2) + ':' + substring(col, 3, 2) + ':' + right(col, 2)) as time)

Related

Beginner Question (Oracle SQL) - Error with Varchar and SUM

I'm currently using Oracle SQL Developer to learn PL/SQL at university. I'm facing a problem where I had to import a .csv file to a table and have to sum up all elements of a column called TIME (composed of minutes, seconds and milliseconds).
However, the column is consisted of VARCHAR and the format is as of below:
TIME
01:00.250
02:37.408
01:29.803
...
I keep getting an error, mostly because there are non-numeric characters (":" and ".") on the column and hence the sum cannot be done. I checked on other topics and saw people saying to use TO_CHAR, TO_DATE, but none solutions seems to work.
Is there a better/easy approach for this problem?
Any help would be appreciated. :)
Oracle doesn't have a time data type. So, if you want to sum these, a simplish method is to convert the values to seconds:
select sum( to_number(substr(time, 1, 2)) * 60 +
to_number(substr(time, 4))
) as seconds
To convert the value back to a string representation of a number:
select floor(seconds / 60) || ':' || (case when seconds < 10 then '0' || mod(seconds, 60) else '' || mod(seconds, 60) end) as mmss
from (select (sum( to_number(substr(time, 1, 2)) * 60 +
to_number(substr(time, 4))
) as seconds
. . .
) s

Convert seconds to '00:00:00' format - SQL Server

I have a report in which i have to calculate overall worked hours by any employee in a week. The report reflects hours more than 24 hours(or 86400 seconds). I want to convert all the seconds into "00:00:00" format keeping the format of the columns as datetime or time so that I can do further analysis using the time field.
Below is what I have tried so far:
Declare #WorkTimeInSeconds int
Set #WorkTimeInSeconds = 144000 **--This value is an example. Mostly values are above 86400 seconds**
Select ActualHoursWorked = convert(varchar,(#WorkTimeInSeconds)/3600)
+ ':' + right( '0' + convert(varchar,((#WorkTimeInSeconds) %3600)/60),2)
+ ':' + right( '0' + convert(varchar,(#WorkTimeInSeconds) %60),2)
Above code gives me what I need however the above converts the seconds in INT to varchar which is not what I need for the report. I need the output to be in datetime/time format which can give me results like '40:00:00' for the above example.
I hope I am able I have provided all the information. Please help as I am struggling with this from many days.
Expected Output: 40:00:00
Maybe try CONVERT style 108.
SELECT CONVERT(varchar(10),convert(varchar,(#WorkTimeInSeconds)/3600)
+ ':' + right( '0' + convert(varchar,((#WorkTimeInSeconds) %3600)/60),2)
+ ':' + right( '0' + convert(varchar,(#WorkTimeInSeconds) %60),2),108)

Sum of dateTime values whose are in String format SSRS 2008

I have a Time variable returning from SQL query in dd:hh:mm format (day:hour:minute). Setting of this variable is:
...
Time = cast(SUM(b.Time) / 1440 as varchar)
+ ':' +
RIGHT('0' + cast(SUM(b.Time)/ 60 % 24 as varchar),2)
+ ':' +
RIGHT('0' + cast(SUM(b.Time) % 60 as varchar),2),
...
So Every Data has its own Time and I can show it in ssrs under Time column by just saying [Time]. My problem is that I would like to add the times of each data and show a TotalTime value. An example:
A ---- 00:02:20
B ---- 01:00:08
Total ---- 01:02:28
However, Sum[Time] does not work here because Time values are string. How can I make the sum progress in this case, I couldnt find a way to do it. I appreciate if someone helps!
THanks
You can write a formula to add the time. First convert your time (hh:mm:ss) to seconds then add the all the seconds and then again convert the total time in (hh:mm:ss) format.
Use this expression in SSRS to show formated dd:HH:mm minutes:
=FORMAT(FLOOR(Fields!Time.Value/1440),"00") & ":" &
FORMAT(FLOOR(Fields!Time.Value / 60 Mod 24),"00") & ":" &
FORMAT(FLOOR(Fields!Time.Value Mod 60),"00")
To get the total use this expression:
=FORMAT(FLOOR(Sum(Fields!Time.Value)/1440),"00") & ":" &
FORMAT(FLOOR(Sum(Fields!Time.Value) / 60 Mod 24),"00") & ":" &
FORMAT(FLOOR(Sum(Fields!Time.Value) Mod 60),"00")
Using this data arrangment in a tablix:
You will get this:
This works supposing you have a field of int data type that contains minutes.
Let me know if this helps.

Turn string with date and time to hh:mm

I have a string field in my table with date and time. Like this
20131001094049
Now i have to get the hour and Minute out of this like 09:40.
How can i easily get that?
I am going to assume, based on your string that the format is YYYYMMDDHHMMSS (Year,Month,Day,Hour,Minute,Second). This solution is for SQL Server.
Considering that you could go for the poor man fix of string manipulation.
SELECT
LEFT(RIGHT(20131001094049, 6 ) , 2) + ':' +
RIGHT(LEFT(RIGHT(20131001094049, 6 ) , 4),2)
Which returns 09:40
What happens is it takes the original date, 20131001094049, takes the right six characters 094049 and then the left two 09. Then it concatenates that to a : and then concatenates the resulting string to 04 of the assumed MM(Minutes) section.
Because your date and time information is not stored in a standard DateTime format for direct conversion, you will need to rely on string manipulation i.e:
SELECT SUBSTRING('20131001094049', 9, 2) + ':' +
SUBSTRING('20131001094049', 11, 2)
To return a full DateTime type you could manipulate the whole string and use the FORMAT function (available in SQL Server 2012 or greater):
DECLARE #dt nvarchar(50), #newdt nvarchar(50)
SET #dt = '20131001094049'
SET #newdt = SUBSTRING(#dt, 1, 4) + '-' + SUBSTRING(#dt, 5, 2) + '-' + SUBSTRING(#dt, 7, 2) + ' ' + SUBSTRING(#dt, 9, 2) + ':' + SUBSTRING(#dt, 11, 2) + ':' + SUBSTRING(#dt, 13, 2)
SELECT FORMAT(CONVERT(datetime, #newdt), 'HH:mm')
Both will return 09:40

converting varchar to date format

I have 2 fields date_issued and time_issued as below
date_issued time_issued
1031 225225 (22 hrs 52 mins 25 secs)
I need to convert that varchar value to datetime as dd-mm-yy h:m:s
I tried following
select CONVERT(datetime, date_issued+time_issued ,120) as ttt from tbl
but is showing following error
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting datetime from character string.
Any ideas??
Assuming year is 2012:
select CONVERT(datetime, '2012' + LEFT(date_issued, 2) + RIGHT(date_issued, 2) + ' ' + LEFT(time_issued, 2) + ':' + SUBSTRING(time_issued, 3, 2) + ':' + right(time_issued, 2))
Well, first of all.
U have an obvious error. If those columns are numbers (not strings) you are adding them up and that is plainly wrong.
Second, if those are strings, you are missing a year in date area and you are missing : in time area.
Third, you need to add a ' ' between those 2.
in my example
SELECT convert(datetime,'20091231 12:12:12') FROM DUAL
i get
2009-12-31 12:12:12.000
P.S. Yes, i have DUAL in SQL server, this wasn't ran in Oracle.
Here is one way, you can remove REPLICATE if the data includes leading zeros, else it is needed for padding to discriminate the date parts.
select
cast (
stuff(replicate('0', 4 - len(date_issued)) + date_issued, 3, 0, '/') + '/2012'
+ ' ' +
stuff(stuff(replicate('0', 6 - len(time_issued)) + time_issued, 3, 0, ':'), 6 ,0 ,':')
as datetime)