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 2005 Forums
 Transact-SQL (2005)
 Format Time

Author  Topic 

stormcandi
Starting Member

46 Posts

Posted - 2007-06-14 : 19:43:56
Hi all,

I am probably going about this the wrong way but I am trying to take the time from a datetime field and convert it to regular time. (i.e. 13:00:00 to 1:00 PM). I have gotten it from the 13:00:00 to 1:00:00 PM, but cannot figure out how to remove the ':00'.I thought doing this would work, but nothing is changed.

Replace(Right(Convert(varchar(20), TimeIn, 22), 11), Patindex('%:00 %', Right(Convert(varchar(20), TimeIn, 22), 11)), ' ') as TimeIn


I have looked everywhere and done what I know how to do; but am ready to beat my head against a wall to figure this out.

Thanks in advance!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-14 : 19:54:34
Why don't you do the formating in your front end application ?
[spoiler]Copyright Madhi Inc.[/spoiler]


KH

Go to Top of Page

melody_ph
Starting Member

11 Posts

Posted - 2007-06-15 : 01:15:24
hi stormcandi! did you already figured it out in your front end?
if not you might check out datepart function..check out this sample:

Declare @hour as varchar(2)
Declare @min as varchar(2)

SET @hour = CONVERT(varchar(2), DATEPART(HH, '13:00:00'))
SET @min = CONVERT(varchar(2), DATEPART(MI, '13:00:00'))
If @hour = '0'
set @hour = '00'
If @min = '0'
set @min = '00'


print @hour + ':' + @min

we could ask khtan if this would be helpful....

-=la lang=-
Go to Top of Page

Koji Matsumura
Posting Yak Master

141 Posts

Posted - 2007-06-15 : 01:29:02
SUBSTRING(Convert(varchar(20), TimeIn, 22), 10,11)

K. Matsumura
Go to Top of Page

stormcandi
Starting Member

46 Posts

Posted - 2007-06-15 : 11:46:33
quote:
Originally posted by khtan

Why don't you do the formating in your front end application ?
[spoiler]Copyright Madhi Inc.[/spoiler]


KH




I initially tried to do it, but the data is populating a dataset and then being bound to a gridview. I figured it might be easier to do it in the sql so the format is already done the way I want it to be. I will look at putting it in the front end and figuring out how to get the formatting the way I want it.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-15 : 12:04:32
It is so easy to do in a grid view, you just specify the format string that you need. Always format at the front end, otherwise you are only returning VARCHAR's via SQL.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -