May 19, 2010 at 9:35 am
hello all,
Can someone provide me the sql to convert 015136 to 1:51:36 (h:m:s)
Thanks
May 19, 2010 at 9:38 am
the field to convert is called audit_time. the output format is 015136. it should be 01:51:36
May 19, 2010 at 9:41 am
Hi there, try this:
select stuff( stuff(cast(015136 as varchar),2,0,':') , 5,0,':')
😎
May 19, 2010 at 9:53 am
I will go further and give you this:
1. First the sample table and sample rows set up
DECLARE @NUM_TABLE TABLE
(
NUM INT
)
INSERT INTO @NUM_TABLE
SELECT 11536 UNION
SELECT 123057 UNION
SELECT 11015 UNION
SELECT 24657 UNION
SELECT 54587
2. Now the code:
SELECT
CASE LEN(CAST(NUM AS VARCHAR))
WHEN 5 THEN '0'+STUFF( STUFF(CAST(NUM AS VARCHAR),2,0,':') , 5,0,':')
WHEN 6 THEN STUFF( STUFF(CAST(NUM AS VARCHAR),3,0,':') , 6,0,':')
END [TIME]
FROM
@NUM_TABLE
Hope this helps you! Tell us if this worked for you!
😎
May 19, 2010 at 10:08 am
I am really new at this and could not get it to work. this is what I entered and keep receiving error msg
select audit_time ( audit_time(cast(audit_time as varchar),2,0,':') , 5,0,':')
May 19, 2010 at 10:10 am
I'm working in crystal reports. the tables are already set up. Thanks.
May 19, 2010 at 12:24 pm
Slight modifications to the solution already provided by ColdCoffee, in order to show with the field name you specified. And a way to eliminate the case statement.
DECLARE @YOUR_TABLE TABLE
(
audit_time INT -- <<< LOOK! your field name is being used!
)
INSERT INTO @YOUR_TABLE
SELECT 11536 UNION ALL
SELECT 123057 UNION ALL
SELECT 11015 UNION ALL
SELECT 24657 UNION ALL
SELECT 54587 UNION ALL
SELECT 1 UNION ALL -- <<<< LOOK!!! really small numbers work also!
SELECT 100
-- Don't assume any length: always pad out to 6 with leading zeros.
-- Then insert the column at the 5th and then the 3rd positions.
-- By working from the back of the string forward, we don't have to
-- deal with the string changing lengths after each STUFF call.
SELECT STUFF(STUFF(RIGHT('000000' + CAST(audit_time AS VARCHAR),6),5,0,':'),3,0,':')
FROM @YOUR_TABLE
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 19, 2010 at 8:20 pm
WayneS (5/19/2010)
Slight modifications to the solution already provided by ColdCoffee, in order to show with the field name you specified. And a way to eliminate the case statement.
DECLARE @YOUR_TABLE TABLE
(
audit_time INT -- <<< LOOK! your field name is being used!
)
INSERT INTO @YOUR_TABLE
SELECT 11536 UNION ALL
SELECT 123057 UNION ALL
SELECT 11015 UNION ALL
SELECT 24657 UNION ALL
SELECT 54587 UNION ALL
SELECT 1 UNION ALL -- <<<< LOOK!!! really small numbers work also!
SELECT 100
-- Don't assume any length: always pad out to 6 with leading zeros.
-- Then insert the column at the 5th and then the 3rd positions.
-- By working from the back of the string forward, we don't have to
-- deal with the string changing lengths after each STUFF call.
SELECT STUFF(STUFF(RIGHT('000000' + CAST(audit_time AS VARCHAR),6),5,0,':'),3,0,':')
FROM @YOUR_TABLE
Awesome wayne, Thanks for the rectified code mate 🙂
May 20, 2010 at 7:50 am
thanks everyone! got it to work....
local stringvar audit_time;
audit_time := trim({AUDIT_TRAIL.AUDIT_TIME});
left(audit_time,2) & ":" & mid(audit_time, 3,2) & ":" & right(audit_time, 2)
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply