We have a column that stores a value in 24 hr time format. It is a string/text that users enter on to the interface. I need to convert it into sql time format so that I can do time difference calculations. How can I convert the string to time? Example:
StringColumn
1400
1600
needs to be
TimeColumn
1400
1600
so that I can calculate the time difference to get 2 hrs. Thanks.
If your string value are always 4 characters (meaning 01-09 and not 1-9 for early hours) then this works:
convert(time, stuff(StringColumn,3,0,':'))
You can do a conversion as in #jpw's answer, especially if you can use DATEDIFF on the results to get what you need.
Alternately you could perhaps do it as integer maths like:
SELECT (60*left('1900',2) + right('1900',2)) - (60*left('1400',2) + right('1400',2))
(I have used constants here, but you can replace '1900' and '1400' with column names).
CAST(LEFT(Stringcolumn, 2) + ':' + RIGHT(LEFT(Stringcolumn, 4), 2) AS TIME)
Related
We have two columns in SQL. one is total_work_time & next is total_exeption_time & both column data type is varchar
total_work_time value is 07:15:00
total_exeption_time value is 01:15:00
So I need to subtract total_work_time - total_exeption_time and the result will be 06:00:00.
I have tried with concat(DATEDIFF(HOUR,total_exeption_time,total_work_time),':', DATEDIFF(MINUTE,total_exeption_time,total_work_time))
But the result is 6:360. from this, 360 is the problem, it taken total minutes. I need the result structure like 06:00:00. How to fix this issue using SQL Server.
You should be storing time values in a TIME datatype - using the correct datatype is not only a best practice but will reduce the problems you face in future.
You can convert your VARCHAR values to TIME and then use the following calculation which takes the difference in seconds (your lowest unit of interest one assumes) and creates a new TIME result.
DECLARE #total_work_time TIME = '07:15:00', #total_exeption_time TIME = '01:15:00';
SELECT CONVERT(TIME, DATEADD(SECOND, DATEDIFF(SECOND, #total_exeption_time, #total_work_time), '00:00'));
I do this query and the result is a datetime, i try so much variants, but nothing work it... I want the result to be displayed in the number of total hours, like (in this case): 25:01:05 because i have 2 days en this datetime, I have had results like 01:01:05 which is when it only subtracts the hours from the datetime. I would like that as well as add the hours by the number of days, do it with the months if it can be
The time datatype in SQL Server only holds up to 24 hours.
I would recommend decimal hours instead:
select datediff(second, 0, calchstrabajos) / (60.0 * 60)
Note: I switched from millisecond to second because that is usually sufficient.
If you want this in the form of HH:MM:SS, then you would need to convert to a string. I don't recommend that.
I'm requesting an Informix database with SQL. I have a column with numbers that represent a number of seconds. I want to transform this number to a time (mm:ss) format in my SQL statement. For example, the number '90' should be transformed into '01:30'. It's important that the new field shouldn't be a string field, but a (date)time field.
You can construct this as:
select floor(secs / 60) || ':' || lpad(mod(secs, 60), 2, '0')
SELECT DATETIME(0:0) MINUTE TO SECOND + colname UNITS SECOND
FROM data_table
This would convert the row containing a numeric value 90 to the value 01:30 with type DATETIME MINUTE TO SECOND. You can vary the type to deal with larger values:
SELECT DATETIME(0:0:0) HOUR TO SECOND + colname UNITS SECOND
FROM data_table
This will process non-negative values from 0 to 86399 producing answers from 00:00:99 to 23:59:59 of type DATETIME HOUR TO SECOND.
You can add up to 5 fractional digits of seconds if desired.
If the input values can be negative or 86400 or larger, then you have to define what you want — you will get an error if the value is 3600 in the first example, or 86400 or larger in the second.
Working with an Access database, using SQL commands to pull data from it. I have a column 'Duration' formatted in smalldatetime, which contains data pertaining to the duration of a phone call. The data is this column is like so
0/01/1900 12:00:26 AM
This indicates a call that is 26 seconds in duration. (all start at 12:00:00 AM and work upwards)
I have a column called 'Extension' which identifies the user of the phone.
I am wanting to run a query that will show me total durations, but grouped by extensions.
The query I have so far is
SELECT (CONVERT(VARCHAR(8),(DATEADD(ms, SUM(DATEDIFF(ms, '00:00:00.000', (CONVERT(VARCHAR(8),Duration,108)))), '00:00:00.000')),108)) as "timetest",
Extension AS Extension
FROM BrendanTest GROUP BY Extension
This shows:
Timetest Extension
00:12:00 117
00:06:00 118
which is correct.
However, I want to format the result, to show in decimal minutes, for example
00:04:56
would become
4.93
Is there a way I can do this? Thanks a lot for the help!
Assuming '00:04:56' is in the format HH:MM:SS, you can try something like the below to convert it to decimal format:
SELECT PARSENAME(REPLACE('00:04:56',':','.'),3) * 60 --Convert the hours to minutes
+ PARSENAME(REPLACE('00:04:56',':','.'),2) --Add converted hours to minutes
+ CONVERT(FLOAT, LEFT((CONVERT(INT,(PARSENAME(REPLACE('00:04:56',':','.'),1)))/0.6),2))/100 -- convert to decimal format
I am using SSIS (SQL 2008) to bring data over from an AS400. The date values are stored in the 400 as a 7 digit numeric. Here is the format: "CYYMMDD" C is a "century digit" where 0 = 1900 and 1 = 2000. I have been looking into derived columns and script components. I am very new to SSIS and all the casting required compounded with different cases is making me a dull boy. Also, I am losing leading zeros. I am not sure if that is b/c they are numeric type and I would see them correctly if I cast as string or not. Below is what I am seeing in SQL after a direct pull from the 400 using SSIS.
AS400 = Actual
101 01/01/1900 (I think these are "unknown" dates)
1231 12/31/1900 (I think these are "unknown" dates)
20702 07/02/1902
151231 12/31/1915
1000102 01/02/2000
1110201 02/01/2011
You should be able to use this expression
(DT_DBDATE) ((DT_STR) (AS400 + 19000000))
Firstly, add the leading zero's in a derived column task:
RIGHT("000000000" + (DT_STR,10,1252)AS400,7)
Pass this to another Derived column task, and use an expression to perform the conversion depending on the century digit, something like:
SUBSTRING([Derived Column 2],1,1) == "0" ? (DT_UI4)[Derived Column 2] + 19000000 : (DT_UI4)SUBSTRING([Derived Column 2],2,8) + 20000000
Which should give you something like 20110201. You can then convert this, or shred it into date parts as required.
Neither of the two answers were 100%, but both helped me to figure out the prob. Not sure whom to mark as "correct" Here is what I did. Had to do 2 derived columns.
1. ((DT_WSTR,8)(<<AS400>> + 19000000))
2. (DT_DBDATE)(SUBSTRING(DCDateString,1,4) + "-" + SUBSTRING(DCDateString,5,2) + "-" + SUBSTRING(DCDateString,7,2))
select substring(t1.datefield,4,2)|| '/' || substring(t1.datefield,6,2) || '/' || (cast(substring(t1.datefield,1,3) as integer) + 1900) as RegularDate from db.table1 t1
If I recall correctly, the century mark date didn't go back to 1900, but rather had to do with an arbitrary date. You might want to check the AS400 redbooks related to the Y2K dates. I programmed on the AS400 from 1992 - 2005. The break years are 1939 & 2039. Date before or after these respectively fail under the century mark system. This is because IBM decided that any two digit year greater than 39 referred to the 1900's, anything less than or equal to 39 referred to the 2000's. If you are dealing with future dates, this might cause a snag.