Solved

Convert Numeric field to Time

Posted on 2011-09-26
8
298 Views
Last Modified: 2012-06-27
I have a numeric field starttime_t numeric(5,2) which stores decimal time in quarter hours. example 8.15, 8.30,8.45, 9.00 what I need to do is report this as time. example 08:15:00

8.15pm should be 20:15:00 any help will be appriciated

0
Comment
Question by:mguptill
8 Comments
 
LVL 13

Expert Comment

by:Wizilling
ID: 36601876
hi - if its a numeric field then you wont be able to store pm.
can you confirm that 8:15 pm is stored as 20.15 in the database?
0
 

Author Comment

by:mguptill
ID: 36601916
Yes it is I don't care if it says AMor PM  here is some sample data

20.00 I still want the same format so in this case I would like 20.00 to reflect 20:00:00
0
 
LVL 13

Accepted Solution

by:
Wizilling earned 400 total points
ID: 36601947
declare @t numeric(5,2) = '8.30'

select substring(convert(varchar(100), cast(replace(CAST(@t as varchar(10)), '.', ':') as datetime), 120), 12,8)
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 100 total points
ID: 36602237
CONVERT(DATETIME,RIGHT('0'+replaCE(CONVERT(VARCHAR(5),convert(SMALLMONEY,starttime_t),0),'.',':'),5)+':00.000',8)
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:mguptill
ID: 36709500
Thank you for your responses I'm sorry I didn't get back right away I had to go somewhere in a hurry. i will look at these and get back to you soon. again sorry for taking so long
0
 

Author Closing Comment

by:mguptill
ID: 36709564
Thank you both  for your solutions and they both worked but Wizilling's solution is what I needed although I will distribute the points accordingly because I believe in fairness and think Lowfatsread should get some points for his effort. Again I thank you both. Sorry lowfat i didn't need the date added.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36712028
I don't know why you don't do something simple like this:
DECLARE @starttime_t numeric(5,2)
SET @starttime_t = '8.20'
SELECT CONVERT(varchar(8), CONVERT(datetime, REPLACE(@starttime_t, '.', ':'), 8), 8)
0
 

Author Comment

by:mguptill
ID: 36718184
That works great ac thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now