How to get Time from DateTime format in SQL?

女生的网名这么多〃 提交于 2019-12-16 22:12:23

问题


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

回答1:


SQL Server 2008:

select cast(AttDate as time) [time]
from yourtable

Earlier versions:

select convert(char(5), AttDate, 108) [time]
from yourtable



回答2:


Assuming Sql server

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




回答3:


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
   ...



回答4:


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

SELECT convert(time(0),getDate())



回答5:


Try using this

  • Date to Time

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

    select cast(orig_time as time(0))
    



回答6:


Try this:

select  convert(nvarchar,CAST(getdate()as time),100)



回答7:


Try this, it will work:

CONVERT(VARCHAR(8),DATETIME,114)

For your reference.




回答8:


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




回答9:


select cast (as time(0))

would be a good clause. For example:

(select cast(start_date as time(0))) AS 'START TIME'



回答10:


I often use this script to get Time from DateTime:

SELECT CONVERT(VARCHAR(9),RIGHT(YOURCOLUMN_DATETIME,9),108) FROM YOURTABLE



回答11:


To get the time from datetime, we can use

SELECT CONVERT(VARCHAR(20), GETDATE(), 114)



回答12:


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.



回答13:


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




回答14:


select convert(char(5), tbl_CustomerBooking.CheckInTime, 108) AS [time]
from tbl_CustomerBooking



回答15:


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


来源:https://stackoverflow.com/questions/7710449/how-to-get-time-from-datetime-format-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!