Does anyone know how can I format a select statement datetime value to only display time in SQL Server?
example:
Table cuatomer
id name datetime
1 Alvin 2010-10-15 15:12:54:00
2 Ken 2010-10-08 09:23:56:00
When I select the table I like the result will display as below
id name time
1 Alvin 3:12PM
2 Ken 9:23AM
Any way that I can do it in mssql?
You can use a combination of CONVERT, RIGHT and TRIM to get the desired result:
The
100you see in the function specifies the date formatmon dd yyyy hh:miAM (or PM), and from there we just grab the right characters.You can see more about converting datetimes here.