[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

extract just time from datetime field

Adam Clark

3/22/2007 3:31:00 PM

I need to run a query that looks at only the time (hh:mm) of a date time
field. Is there a way to do this? short of using Datepart?

thanks

Adam


3 Answers

Tom Cooper

3/22/2007 3:40:00 PM

0

Select Convert(char(12), <datetime>, 114) will get you hh:mm:ss:mmm
Select Convert(char(5), <datetime>, 114) will get you hh:mm

Tom

"Adam Clark"
<aclark@midsouthtech.net> wrote in message
news:e$HARdJbHHA.4872@TK2MSFTNGP03.phx.gbl...
>I need to run a query that looks at only the time (hh:mm) of a date time
>field. Is there a way to do this? short of using Datepart?
>
> thanks
>
> Adam
>


Aaron [SQL Server MVP]

3/22/2007 3:55:00 PM

0

SELECT CONVERT(CHAR(8), column_name, 108) FROM table_name;

--
Aaron Bertrand
SQL Server MVP
http://www.sq...
http://www.aspfa...



"Adam Clark" <aclark@midsouthtech.net> wrote in message
news:e$HARdJbHHA.4872@TK2MSFTNGP03.phx.gbl...
>I need to run a query that looks at only the time (hh:mm) of a date time
>field. Is there a way to do this? short of using Datepart?
>
> thanks
>
> Adam
>


Hugo Kornelis

3/22/2007 6:07:00 PM

0

On Thu, 22 Mar 2007 11:30:36 -0400, Adam Clark wrote:

>I need to run a query that looks at only the time (hh:mm) of a date time
>field. Is there a way to do this? short of using Datepart?
>
>thanks
>
>Adam
>

Hi Adam,

The queries given by Tom and Aaron give you the time in a string format.
If you need it as datetime (i.e. the time from the input, combined with
the "default" date of 1900-01-01), use:

SELECT DATEADD(day, -DATEDIFF(day, 0, CURRENT_TIMESTAMP),
CURRENT_TIMESTAMP)

Note - I used CURRENT_TIMESTAMP as input here, for testing purposes.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hug...