Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i want to use multiple substring in select query for single column.
so for exp. i have value as 221415525
i want to make it

22:14:155
can i use substring in select as
 SELECT ' ' + substring('221415525', 1, 2)
+ ':' + substring('143825', 3, 2)
+ ':' + substring('143825', 5, 2), name from mytable
Posted
Comments
Saral S Stalin 23-Mar-15 3:01am    
Yes, you can

SELECT ' ' + substring('221415525', 1, 2)
+ ':' + substring('221415525', 3, 2)
+ ':' + substring('221415525', 5, 2)

Should get you desired result. Let me know if you are expecting something else.
maulikshah1990 23-Mar-15 3:09am    
i have value as float 1.09796836411745 , which i convert into time by using

REPLACE(CONVERT(CHAR(20),CAST(SUM(CCAGDT.CONVERSATION_TIME) AS datetime),114), ':', ''),

and i get result as 221415525 ..
then i need to make it 22:14:15525

how to do this..
Saral S Stalin 23-Mar-15 3:18am    
What is the basis of conversion of 1.09 as 22:14? What is the unit of 1.09? Days/Hour/Seconds
maulikshah1990 23-Mar-15 3:20am    
i did not understand . can u explain more simply.
as i have used CAST(SUM(CCAGDT.CONVERSATION_TIME) AS datetime ,114 , it gives value 221415525

Saral S Stalin 23-Mar-15 3:23am    
CCAGDT.CONVERSATION_TIME what are you storing in this? Hours/Days?

SQL
DECLARE @STR AS NVARCHAR(MAX) = '221415525'

SELECT CONVERT(DATETIME, SUBSTRING(@STR, 1, 2) + ':' + SUBSTRING(@STR, 3, 2) + ':' + SUBSTRING(@STR, 5, 2) + ':' + SUBSTRING(@STR, 7, 2), 114)
 
Share this answer
 
Comments
maulikshah1990 23-Mar-15 5:36am    
thanks for reply.. but time value 2214515525 is generated from float value 1.0925141425 from database ..

the time value is generated by REPLACE(CONVERT(CHAR(20),CAST(SUM(CCAGDT.CONVERSATION_TIME) AS datetime),114), ':', ''),

CCAGDT.Conversation_time is table field which has value float 1.0952525 , and i cast it to datetime .. so if i dont use replace, it says

[Error converting data type varchar to float.]




and my query should be that it selects one column which will have value


select (substring(1)+':'+substring(2)+':'+substring(3)) , name from tablename
'



SELECT ' ' + substring('221415525', 1, 2)
+ ':' + substring('143825', 3, 2)
+ ':' + substring('143825', 5, 2), name from mytable



I hope it is clear now...
maulikshah1990 24-Mar-15 1:44am    
thanks for reply.. but time value 2214515525 is generated from float value 1.0925141425 from database ..

the time value is generated by REPLACE(CONVERT(CHAR(20),CAST(SUM(CCAGDT.CONVERSATION_TIME) AS datetime),114), ':', ''),

CCAGDT.Conversation_time is table field which has value float 1.0952525 , and i cast it to datetime .. so if i dont use replace, it says

[Error converting data type varchar to float.]




and my query should be that it selects one column which will have value


select (substring(1)+':'+substring(2)+':'+substring(3)) , name from tablename
'



SELECT ' ' + substring('221415525', 1, 2)
+ ':' + substring('143825', 3, 2)
+ ':' + substring('143825', 5, 2), name from mytable



I hope it is clear now...
I found answer
ie.

CONVERT(TIME(0),CONVERT(datetime,1.09796836411745)),

this gives output as 02:21:04
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900