Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI All,

After googling alot, I have just landed on CP in search of some guidance to resolve my issue.

We have been saving time in our table like 11:33 AM in table as 1133.Now i want to get it back in proper time format i.e. in AM PM format to use in our application.

Can any body guide??

:)
Posted
Updated 22-Feb-18 18:32pm
v2
Comments
Thanks7872 3-Dec-13 2:46am    
So your application has no front end?
VICK 3-Dec-13 5:04am    
Offcource it does.. I am showing that data in RDLC report. and Properites>Number>Time option is not working from there.
André Kraak 3-Dec-13 2:55am    
How do you store 11:33 PM?
VICK 3-Dec-13 5:05am    
data comes in plain text file like 1133 or 2244 and get stored in DB directly thorugh Scheduled job and procedure.
Maciej Los 3-Dec-13 2:55am    
If you want to get complete answer, you should provide more details about data type (including sample data) and more details about issue.

1 solution

Please read my comment first.

Generally, to convert data from one type to another, please use: CAST and CONVERT (Transact-SQL)[^]

[EDIT]
Reply to OP comment. No, it's not enough information. There are few version of MS SQL Server. Some of them does not provide time data type.
SQL
DECLARE @t VARCHAR(30) = '1133'

SELECT LEFT(@t,2) + ':' + RIGHT(@t,2) + ' AM' AS MyTimeAsString

SELECT CONVERT(TIME, MyTimeAsString) AS MyTimeAsTime
FROM (
    SELECT LEFT(@t,2) + ':' + RIGHT(@t,2) + ' AM' AS MyTimeAsString
    ) AS T

SELECT CONVERT(DATETIME, MyTimeAsString) AS MyTimeAsDateTime
FROM (
    SELECT LEFT(@t,2) + ':' + RIGHT(@t,2) + ' AM' AS MyTimeAsString
    ) AS T


Results:
MyTimeAsString
11:33 AM

MyTimeAsTime
11:33:00.0000000

MyTimeAsDateTime
1900-01-01 11:33:00.000


Is that what you want?
[/EDIT]

[EDIT 2]
SQL
CREATE FUNCTION udf_FormatTime (@sTime VARCHAR(10))
    RETURNS VARCHAR(15)
AS
BEGIN
    DECLARE @sTimeFormat VARCHAR(15)

    IF LEN(@sTime) = 0
        SET @sTimeFormat = '00:00 AM'
    ELSE
        SET @sTimeFormat = LEFT(@sTime, 2) + ':' + RIGHT(@sTime, 2) 
    
    IF CONVERT(INT, LEFT(@sTimeFormat,2))<13 
        SET @sTimeFormat = @sTimeFormat + ' AM'
    ELSE
       SET @sTimeFormat = @sTimeFormat + ' PM'
    
RETURN @sTimeFormat
END


[/EDIT 2]
 
Share this answer
 
v7
Comments
coded007 3-Dec-13 5:48am    
Maciej Los I tried that but not work. I think we need to give some custom code to convert this
Maciej Los 3-Dec-13 5:57am    
What does not work? Have you read my comment first? Because OP does not provide enough information, my answer is about how to convert data between data types.
VICK 3-Dec-13 6:32am    
Thanks maciej Los... got cleared.. well I m using SQL Server 2008 Enterprise.
VICK 3-Dec-13 6:34am    
Although it did not solve my problem.. as its just adding : between the string and appending hardcoded AM at end.. but still it gives me some hint.
Maciej Los 3-Dec-13 7:01am    
I suggest you to change data type to time or datetime, depends on your needs ;)
Could you, please, mark this solution as an answer (green button)?

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