April 23, 2010 at 3:17 am
Hi All,
I have a DURATION column in one of my tables which is in nvarchar.
that column shows the duration alloted to a praticular task.
The data we get is in the form 10,20, 60 etc.
I want to store as 00:10 if it is 10 mins....
if it is 60 it must be 01:00.
How to do this?
April 23, 2010 at 3:30 am
-- *** Test Data ***
DECLARE @t TABLE
(
minutes varchar(3) NOT NULL
)
INSERT INTO @t
SELECT '10'
UNION ALL SELECT '20'
UNION ALL SELECT '60'
-- *** End Test Data ***
SELECT REPLACE(STR(minutes/60, 2), ' ', '0')
+ ':'
+ REPLACE(STR(minutes%60, 2), ' ', '0') AS Duration
FROM @t
April 23, 2010 at 3:44 am
The client DB has that column in INT.
We must store as NVARCHAR.
So, the INT value 10 should be stored as NVARCHAR "00:10"
April 23, 2010 at 6:37 am
malavika.ramanathan (4/23/2010)
The client DB has that column in INT.We must store as NVARCHAR.
So, the INT value 10 should be stored as NVARCHAR "00:10"
Taking Ken's fine code, changing the source to be an INT, and the result to be a NVARCHAR, we end up with:-- *** Test Data ***
DECLARE @t TABLE
(
minutes int NOT NULL
)
INSERT INTO @t
SELECT 10
UNION ALL SELECT 20
UNION ALL SELECT 60
-- *** End Test Data ***
SELECT CONVERT(NVARCHAR(5), REPLACE(STR(minutes/60, 2), ' ', '0')
+ ':'
+ REPLACE(STR(minutes%60, 2), ' ', '0')) AS Duration
FROM @t
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 23, 2010 at 7:18 pm
malavika.ramanathan (4/23/2010)
Hi All,I have a DURATION column in one of my tables which is in nvarchar.
that column shows the duration alloted to a praticular task.
The data we get is in the form 10,20, 60 etc.
I want to store as 00:10 if it is 10 mins....
if it is 60 it must be 01:00.
How to do this?
Will there be any durations >= 1440???
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply