select left(convert(varchar, online_time,109),4)+' '+ right(convert(varchar, online_time,109),2) as online_time from time
above is my query
i want output as
04:30 am 4:30 am will do(if initial 0 is removed no prob)
09:45 pm
11:15 am
my prob is that above query is giving right output for time before 10 am/pm but after 9:59 am/pm time is wrong i.e, it displays
11:3 pm instead of 11:30 pm
and give right for
4:40 pm
how i can do that
Loading
Suthish NairPosted Feb 15, 2012, 10:07 AM
SELECT
REPLICATE('0',2-LEN(DATEPART(HH,RIGHT(CONVERT(CHAR(17),CONVERT(datetime, CAST('09:00:00' AS TIME), 100), 100),5)))) +
LTRIM(RIGHT(CONVERT(CHAR(17),CONVERT(datetime, CAST('09:00:00' AS TIME), 100), 100),5))
SELECT
REPLICATE('0',2-LEN(DATEPART(HH,RIGHT(CONVERT(CHAR(17),CONVERT(datetime, CAST('23:05:00' AS TIME), 100), 100),5)))) +
LTRIM(RIGHT(CONVERT(CHAR(17),CONVERT(datetime, CAST('23:05:00' AS TIME), 100), 100),5))
SELECT
REPLICATE('0',2-LEN(DATEPART(HH,RIGHT(CONVERT(CHAR(17),CONVERT(datetime, CAST('13:30:00' AS TIME), 100), 100),5)))) +
LTRIM(RIGHT(CONVERT(CHAR(17),CONVERT(datetime, CAST('13:30:00' AS TIME), 100), 100),5))
nishant ranjanPosted Feb 16, 2012, 6:43 AM
yup i am doing with Suthish's suggestion
as per your last suggestion
select left(convert(char(16), online_time),5) + ' ' + right(convert(varchar, online_time,109),2) from time
again i hv problem of data in 24 hour format which i want to be in 12 hour format
VulpesPosted Feb 16, 2012, 6:23 AM
nishant ranjanPosted Feb 16, 2012, 6:11 AM
getdate() is working for me too...
but online_time is not working i.e., when i replace getdate with online_time its not wrking
note :- online_time is column in "time" table of datatype time(7)
nishant ranjanPosted Feb 16, 2012, 5:35 AM
thanks replicate function is working
@Vulpes & @Suthis Nair
about changing to char, if u r talking abt below code than its nt wrking
SELECT left(CONVERT(char(5),'09:00:00',109),5)
SELECT left(CONVERT(char(5),'23:05:00',109),5)
SELECT left(CONVERT(char(5),'13:30:00',109),5)
nt converting to 12 hr fmt & nt evn in 108 format
Suthish NairPosted Feb 15, 2012, 10:11 AM
VulpesPosted Feb 15, 2012, 9:37 AM
Abhimanyu K VatsaPosted Feb 15, 2012, 8:53 AM
Look at image, that's working on my system. If you are still getting error, undoubtedly there is some issue with your "online_time".
Can you check using GETDATE() instead of "online_time"?
nishant ranjanPosted Feb 15, 2012, 7:49 AM
still there is problem....
SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2) time
where time is my table name
i even tried with abc, online_time etc in place of time
if i dont give table name anywhere then from which tble it will retrieve online_time column
Abhimanyu K VatsaPosted Feb 15, 2012, 7:27 AM
SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2) TIME
GO
Look the bold and underlined word, you need to write any word there to represent.
I am already using this on my system.
nishant ranjanPosted Feb 15, 2012, 7:19 AM
yup thats exactly the problem is... its not reading initial 0
online_time time(7)
@Abhimanyu
SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2)
giving error invalid column name
Abhimanyu K VatsaPosted Feb 15, 2012, 7:07 AM
SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2) TIME
GO
'TIME' is just a name, not any column from table and it can be anything.
Please check.
VulpesPosted Feb 15, 2012, 6:57 AM
nishant ranjanPosted Feb 15, 2012, 6:51 AM
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2) from time
wrong output
00000p
Abhimanyu K VatsaPosted Feb 15, 2012, 6:28 AM
SUBSTRING(CONVERT(CHAR(26), GETDATE(), 9), 12, 6)
GO
--OUTPUT = 11:30
and if you want to add AM or PM then
SELECT
SUBSTRING(CONVERT(CHAR(26), GETDATE(), 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), GETDATE(), 9), 25, 2) COLNAME
GO
--OUTPUT = 11:30AM
at the place of '' you can add spaces or any separator.
nishant ranjanPosted Feb 15, 2012, 4:37 AM
Suthish NairPosted Feb 15, 2012, 3:40 AM
SELECT CONVERT(varchar(5),right('01/01/2012 11:30', 5),108)
SELECT CONVERT(varchar(5),GETDATE(),114) SELECT CONVERT(varchar(5),GETDATE(),114)
Edited:
SELECT CONVERT(varchar(5),left('09:00:00', 5),108)
SELECT CONVERT(varchar(5),left('23:05:00', 5),108)
SELECT CONVERT(varchar(5),left('13:30:00', 5),108)
http://www.sqlusa.com/bestpractices/datetimeconversion/
nishant ranjanPosted Feb 15, 2012, 3:24 AM
if time is 9:00:00 in datebase then output is 9:00: am
in my database time is stored in this format
09:00:00
23:05:00
13:30:00
time format is wrong for time before 10 am
VulpesPosted Feb 14, 2012, 9:25 AM
select left(convert(varchar, online_time,109),5)+' '+ right(convert(varchar, online_time,109),2) as online_time from time