Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to show time in SQL server 2008

Author  Topic 

tvb2727
Starting Member

35 Posts

Posted - 2012-03-11 : 18:45:06
I want to show a column in Time format. How can I show the time like this: 12:12 PM?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-11 : 19:37:02
this is a front end issue and should be handled at front end application if possible. If you really need to do it in SQL, you need to make use of CONVERT function


http://msdn.microsoft.com/en-us/library/ms187928.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dosteroid
Starting Member

29 Posts

Posted - 2012-03-12 : 05:50:59
You can replace the GetDate function with any datetime field to return the time portion of that field.

SELECT substring(convert(varchar(20), GetDate(), 9), 13, 5)
+ ' ' + substring(convert(varchar(30), GetDate(), 9), 25, 2)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-12 : 10:06:50
quote:
Originally posted by dosteroid

You can replace the GetDate function with any datetime field to return the time portion of that field.

SELECT substring(convert(varchar(20), GetDate(), 9), 13, 5)
+ ' ' + substring(convert(varchar(30), GetDate(), 9), 25, 2)


only issue is you will face problems if you're trying to do some date manipulations with these converted values thereafter.
So unless you've just using this for display, better not to do this.
For display also its very easier to do formatting at your front end

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -