226

I want to get only Time from DateTime column using SQL query using SQL Server 2005 and 2008 Default output:

AttDate                   
==
2011-02-09 13:09:00    
2011-02-09 14:10:00    

I'd like this output:

AttDate                Time 
==
2011-02-09 13:09:00    13:09
2011-02-09 14:10:00    14:10
iconoclast
  • 19,365
  • 11
  • 98
  • 131
Jig12
  • 2,749
  • 4
  • 22
  • 27

18 Answers18

408

SQL Server 2008:

SELECT cast(AttDate as time) [time]
FROM yourtable

Earlier versions:

SELECT convert(char(5), AttDate, 108) [time]
FROM yourtable
t-clausen.dk
  • 42,087
  • 11
  • 52
  • 90
43

Assuming Sql server

SELECT CONVERT(VARCHAR(8),GETDATE(),108)

V4Vendetta
  • 35,718
  • 7
  • 75
  • 81
  • It works only on getdate() where as when I pass select convert(varchar(8),'2011-02-09 13:09:00',108) it is producing the first 8 characters from the date .how to fix this ? – Aparna Aug 04 '17 at 06:31
29

SQL Server 2008+ has a "time" datatype

SELECT 
    ..., CAST(MyDateTimeCol AS time)
FROM
   ...

For older versions, without varchar conversions

SELECT 
    ..., DATEADD(dd, DATEDIFF(dd, MyDateTimeCol, 0), MyDateTimeCol)
FROM
   ...
gbn
  • 408,740
  • 77
  • 567
  • 659
  • why you not use this SELECT CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond, CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly is there any performance issue ? – rahularyansharma Oct 10 '11 at 09:02
  • 2
    @rahularyansharma: I don't use varchar conversion for dates if needed – gbn Oct 10 '11 at 09:03
  • sir i want to know is there any performance decrease if we use this instead of your solution ? – rahularyansharma Oct 10 '11 at 09:04
  • 1
    @rahularyansharma: you can test yourself based on this http://stackoverflow.com/questions/133081/most-efficient-way-in-sql-server-to-get-date-from-datetime – gbn Oct 10 '11 at 09:05
  • 2
    For anyone not following, `0` represents the min date `1900-01-01`. So this gets the (negative) number of days between the column value and 0, then adds those negative days to the column value which "zeros out" the date portion to `1900-01-01` and you're left with only the time. – xr280xr Oct 13 '15 at 20:55
20

The simplest way to get the time from datetime without millisecond stack is:

SELECT convert(time(0),getDate())
BigDaddy
  • 301
  • 2
  • 2
11

Try using this

  • Date to Time

    select cast(getdate() as time(0))
    
  • Time to TinyTime

    select cast(orig_time as time(0))
    
Tunaki
  • 125,519
  • 44
  • 317
  • 399
Cantarero
  • 111
  • 1
  • 2
6

Try this, it will work:

CONVERT(VARCHAR(8),DATETIME,114)

For your reference.

Bugs
  • 4,456
  • 9
  • 32
  • 40
user8498521
  • 61
  • 1
  • 1
4

Try this:

select  convert(nvarchar,CAST(getdate()as time),100)
Raj Baral
  • 641
  • 6
  • 19
Balaji N
  • 51
  • 1
  • 1
4

Get date of server

SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7)) FROM TABLENAME WHERE ...

or

If it is stored in the table

SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), datename, 100), 7)) FROM TABLENAME WHERE ...

Result:

11:41AM

2

I often use this script to get Time from DateTime:

SELECT CONVERT(VARCHAR(9),RIGHT(YOURCOLUMN_DATETIME,9),108) FROM YOURTABLE
Cody Gray
  • 230,875
  • 49
  • 477
  • 553
ChinoNoypi
  • 21
  • 1
2
select cast (as time(0))

would be a good clause. For example:

(select cast(start_date as time(0))) AS 'START TIME'
Cody Gray
  • 230,875
  • 49
  • 477
  • 553
2

select AttDate,convert(char(5), AttDate, 108) [Time] from yourTableName

Rob
  • 5,122
  • 5
  • 38
  • 61
sagar Shah
  • 89
  • 1
  • 1
  • 3
1

If you want date something in this style: Oct 23 2013 10:30AM

Use this

SELECT CONVERT(NVARCHAR(30),getdate(), 100)

convert() method takes 3 parameters

  1. datatype
  2. Column/Value
  3. Style: Available styles are from 100 to 114. You can choose within range from. Choose one by one to change the date format.
Raj Baral
  • 641
  • 6
  • 19
Arif Ansari
  • 432
  • 1
  • 4
  • 12
1

To get the time from datetime, we can use

SELECT CONVERT(VARCHAR(20), GETDATE(), 114)
thevan
  • 9,684
  • 52
  • 134
  • 197
1

on MSSQL2012 or above

cast(dateadd(ms,datediff(ms, [StartDateTime], [StopDateTime]),0) as Time(0))

...or...

convert(time(0),dateadd(ms,datediff(ms, [StartDateTime], [StopDateTime]),0) )
1

SQL Server 2012:

Select TRY_CONVERT(TIME, myDateTimeColumn) from myTable;

Personally, I prefer TRY_CONVERT() to CONVERT(). The main difference: If cast fails, TRY_CONVERT() returns NULL while CONVERT() raises an error.

jinhr
  • 71
  • 6
1

You can use this:

SELECT CONVERT(VARCHAR(5), GETDATE(),8)  

Output:

08:24
Majid Basirati
  • 2,395
  • 3
  • 21
  • 45
0
select convert(char(5), tbl_CustomerBooking.CheckInTime, 108) AS [time]
from tbl_CustomerBooking
Code
  • 559
  • 4
  • 9
-2

select substr(to_char(colUmn_name, 'DD/MM/RRRR HH:MM:SS'),11,19) from table_name;

Output: from

05:11:26
05:11:24
05:11:24
Alex Szabo
  • 3,259
  • 2
  • 21
  • 30