I have one Table which has two fields such as \"StartTime\" and \"EndTime\". The DataType of the Two columns are Time.
So the Values of the Table looks like as follo
In SQL 2012 you can use the Format() function.
https://technet.microsoft.com/en-us/library/hh213505%28v=sql.110%29.aspx
Skip casting if the column type is (datetime).
Example:
SELECT FORMAT(StartTime,'hh:mm tt') AS StartTime
FROM TableA
SELECT CONVERT(varchar, StartTime, 100) AS ST,
CONVERT(varchar, EndTime, 100) AS ET
FROM some_table
or
SELECT RIGHT('0'+ LTRIM(RIGHT(CONVERT(varchar, StartTime, 100),8)),8) AS ST,
RIGHT('0'+ LTRIM(RIGHT(CONVERT(varchar, EndTime, 100),8)),8) AS ET
FROM some_table
Try this:
select CONVERT(VARCHAR(5), ' 4:07PM', 108) + ' ' + RIGHT(CONVERT(VARCHAR(30), ' 4:07PM', 9),2) as ConvertedTime
select CONVERT(varchar(15),CAST('17:30:00.0000000' AS TIME),100)
almost works perfectly except for the space issue. if that were changed to:
select CONVERT(varchar(15),CAST('17:30:00.0000000' AS TIME),22)
...then you get the space. And additionally, if the column being converted is already of TIME
format, you can skip the cast if you reference it directly.
Final answer:
select CONVERT(varchar(15),StartTime,22)
select right(convert(varchar(20),getdate(),100),7)
Use following syntax to convert a time to AM PM format.
Replace the field name with the value in following query.
select CONVERT(varchar(15),CAST('17:30:00.0000000' AS TIME),100)