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
 General SQL Server Forums
 New to SQL Server Programming
 date format issue

Author  Topic 

craigwg
Posting Yak Master

154 Posts

Posted - 2011-07-25 : 16:56:34
I have a date in the database I am currently casting to get a more readable format. However I want to get two digits for the hour, minute and second. How do I do that. Also, is there a way to do it without using all the casts? What's the most efficient way to do this?

Here is my current code:


select
cast(datename(mm,enddate) as varchar)+' '+
cast(datepart(d,enddate) as varchar)+', '+
cast(datepart(yy,enddate) as varchar)+' '+
cast(datepart(hh,enddate) as varchar)+':'+
cast(datepart(minute,enddate) as varchar)+':'+
cast(datepart(second,enddate) as varchar)
from reportSchedules2


Craig Greenwood

Jason W
Starting Member

19 Posts

Posted - 2011-07-25 : 17:03:40
Use the convert function. There are a lot of different formats. You can find them here:
http://msdn.microsoft.com/en-us/library/ms187928.aspx

I think the format number you are looking for is 20. CONVERT(VARCHAR, EndDate, 20)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-26 : 13:14:54
also keep in mind that most of front end tools have ability to show it in desired format at front end so if its possible do it at front end as its a presentation issue.

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

Go to Top of Page
   

- Advertisement -