Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting int to hh:mm:ss format 1

Status
Not open for further replies.

juniordba

MIS
Feb 25, 2003
3
CA
Hi All,

Yup im a newbie to SQL and I've read on cast and convert before asking this question. But basically I have a column (int datatype) that is actually showing a time value. (i.e. 25755 - actually means 2:57:55 in hours:minutes:seconds).

Now what I want to do is to do a select on this column to change it to the following format -- hh:mm:ss.

Tips please? Thanks in advance.
 
Your question is ambiguos. What will be following considered as:
11111 -> 11:11:01 or 01:11:11 or 11:01:11
1111 -> 01:01:11 or 11:01:01 or 01:11:01

Please provide the basic rules to interpret the value.
 
Try this query using the REPLACE, STR, and STUFF functions.

Select TimeValue = replace(stuff(stuff(str(IntColumn,6),5,0,':'),3,0,':'),' ',':')
From YourTable
If you want to get the best answer for your question read faq183-874 and thread183-468158.
Terry L. Broadbent - DBA
SQL Server Page:
 
Terry,
Your query assumes that there are always 6 digits in the table, but from the example, which juniordba has given it seems that he can have a 5 digit number also in the table.
 
No it does not:
Following are results of the above examples:
253 -> ::::2:53
5 -> :::::::5
12345 -> :1:23:45
 
Sorry, I made an error when posting.

Select TimeValue = replace(stuff(stuff(str(IntColumn,6),5,0,':'),3,0,':'),' ','0')
From YourTable

Now it does as I indicated. Thanks for pointing out my error. If you want to get the best answer for your question read faq183-874 and thread183-468158.
Terry L. Broadbent - DBA
SQL Server Page:
 
Hi...

Or may be you can try like this...

Select TimeValue = Substring(str(IntColumn,6),1,2)+
Substring(stuff(str(IntColumn,6),1,2,':'),1,3)+
Substring(stuff(str(IntColumn,6),1,4,':'),1,3)

don't forget to use STR(IntColumn, 6) ! -> hh:mm:ss
 
This solution worked:

Select TimeValue = replace(stuff(stuff(str(IntColumn,6),5,0,':'),3,0,':'),' ','0')
From YourTable

But im also going to try the last one...will let you know how it goes..

once again, thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top