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)
 Getting time part of date

Author  Topic 

Rupa
Posting Yak Master

123 Posts

Posted - 2007-11-08 : 09:17:44
Hey all

I have a date in the following format:

08/11/2007 13:57:26

I would like to get 13:57

I'm using RIGHT(PrintDate, 7) which gives me: 1:56PM

How can I get 13:57 on its own??

Any response will be highly appreciated.

Many thanks,

Rupa

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-11-08 : 09:31:35
so you're casting it as a string? if you just want to seperate the time portion you'd be better to keep it as a datetime...

select getdate()-dateadd(dd,datediff(dd,0,getdate()),0) as time_only

that way you can still 'use' it for queries etc.

Em
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-11-08 : 09:36:46
or if you do really really want a character string. maybe...

select cast(datepart(hh,getdate())as char(2))+ ':' + cast(datepart(ss,getdate()) as char(2))

Em
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-11-08 : 09:39:42
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65358


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Rupa
Posting Yak Master

123 Posts

Posted - 2007-11-08 : 09:47:23
Thx Em...This worked for me:

CONVERT(VARCHAR(5),PrintDate, 108)

Thx KH

Rupa
Go to Top of Page
   

- Advertisement -