从DateTime (SQL Server 2005)提取时间

时间:2022-04-11 10:16:32

I can extract the month and day by using Day(Date()), Month(Date()). I can't extract hours, with HOUR(Date()). I get the following error.

我可以通过使用day (Date())、month (Date())提取月和日。我不能抽时间,用小时(日期)。我得到了下面的错误。

'HOUR' is not a recognized built-in function name.

How can I extract hours?

我如何提取时间?

10 个解决方案

#1


252  

SELECT DATEPART(HOUR, GETDATE());

DATEPART documentation

DATEPART文档

#2


26  

... you can use it on any granularity type i.e.:

…您可以在任何粒度类型上使用它,例如:

DATEPART(YEAR, [date])

DATEPART(MONTH, [date]) 

DATEPART(DAY, [date])    

DATEPART(HOUR, [date]) 

DATEPART(MINUTE, [date])

(note: I like the [ ] around the date reserved word though. Of course that's in case your column with timestamp is labeled "date")

(注:我喜欢日期前后的保留词。当然,如果您的带有时间戳的列被标记为“日期”,那么这是可以的)

#3


13  

Use datepart.

使用datepart。

E.g.:

例如:

datepart(HOUR, date)

#4


8  

try this one too:

试试这个:

   DATEPART(HOUR,GETDATE()) 

#5


1  

The DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

DATEPART()函数用于返回日期/时间的单个部分,例如年、月、日、小时、分钟等。

datepart    ***Abbreviation

year        ***yy, yyyy 
quarter     ***qq, q 
month       ***mm, m 
dayofyear   ***dy, y 
day         ***dd, d 
week        ***wk, ww 
weekday     ***dw, w 
hour        ***hh 
minute      ***mi, n 
second      ***ss, s 
millisecond ***ms 
microsecond ***mcs 
nanosecond  ***ns 

Example

例子

select * 
from table001
where datepart(hh,datetime) like 23

#6


0  

you must use datepart()

您必须使用datepart()

like 


datepart(hour , getdate())

#7


0  

I can't extract hours, with HOUR(Date())

我不能提取小时,用小时(日期)

There is a way to call HOUR (I would not recommend to use it though because there is DATEPART function) using ODBC Scalar Functions:

有一种方法可以使用ODBC标量函数调用HOUR(我不建议使用它,因为有DATEPART函数):

SELECT {fn HOUR(GETDATE())} AS hour

LiveDemo

LiveDemo

#8


0  

DATEPART(HOUR, [date]) returns the hour in military time ( 00 to 23 ) If you want 1AM, 3PM etc, you need to case it out:

日期部分(小时,[日期])返回军事时间(00至23)如果你想要凌晨1点,下午3点等,你需要指出:

SELECT Run_Time_Hour =
CASE DATEPART(HOUR, R.date_schedule)
    WHEN 0 THEN  '12AM'
    WHEN 1 THEN   '1AM'
    WHEN 2 THEN   '2AM'
    WHEN 3 THEN   '3AM'
    WHEN 4 THEN   '4AM'
    WHEN 5 THEN   '5AM'
    WHEN 6 THEN   '6AM'
    WHEN 7 THEN   '7AM'
    WHEN 8 THEN   '8AM'
    WHEN 9 THEN   '9AM'
    WHEN 10 THEN '10AM'
    WHEN 11 THEN '11AM'
    WHEN 12 THEN '12PM'
    ELSE CONVERT(varchar, DATEPART(HOUR, R.date_schedule)-12) + 'PM'
END
FROM
    dbo.ARCHIVE_RUN_SCHEDULE R

#9


0  

select case when [am or _pm] ='PM' and datepart(HOUR,time_received)<>12 
           then dateadd(hour,12,time_received) 
           else time_received 
       END 
from table

works

作品

#10


0  

to_char(current_timestamp,'YYYY') // to extract only DATE to_char(current_timestamp,'HH24') // to extract only HOUR

to_char(current_timestamp,'YYYY') //只提取日期to_char(current_timestamp,'HH24') //以提取时间。

#1


252  

SELECT DATEPART(HOUR, GETDATE());

DATEPART documentation

DATEPART文档

#2


26  

... you can use it on any granularity type i.e.:

…您可以在任何粒度类型上使用它,例如:

DATEPART(YEAR, [date])

DATEPART(MONTH, [date]) 

DATEPART(DAY, [date])    

DATEPART(HOUR, [date]) 

DATEPART(MINUTE, [date])

(note: I like the [ ] around the date reserved word though. Of course that's in case your column with timestamp is labeled "date")

(注:我喜欢日期前后的保留词。当然,如果您的带有时间戳的列被标记为“日期”,那么这是可以的)

#3


13  

Use datepart.

使用datepart。

E.g.:

例如:

datepart(HOUR, date)

#4


8  

try this one too:

试试这个:

   DATEPART(HOUR,GETDATE()) 

#5


1  

The DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

DATEPART()函数用于返回日期/时间的单个部分,例如年、月、日、小时、分钟等。

datepart    ***Abbreviation

year        ***yy, yyyy 
quarter     ***qq, q 
month       ***mm, m 
dayofyear   ***dy, y 
day         ***dd, d 
week        ***wk, ww 
weekday     ***dw, w 
hour        ***hh 
minute      ***mi, n 
second      ***ss, s 
millisecond ***ms 
microsecond ***mcs 
nanosecond  ***ns 

Example

例子

select * 
from table001
where datepart(hh,datetime) like 23

#6


0  

you must use datepart()

您必须使用datepart()

like 


datepart(hour , getdate())

#7


0  

I can't extract hours, with HOUR(Date())

我不能提取小时,用小时(日期)

There is a way to call HOUR (I would not recommend to use it though because there is DATEPART function) using ODBC Scalar Functions:

有一种方法可以使用ODBC标量函数调用HOUR(我不建议使用它,因为有DATEPART函数):

SELECT {fn HOUR(GETDATE())} AS hour

LiveDemo

LiveDemo

#8


0  

DATEPART(HOUR, [date]) returns the hour in military time ( 00 to 23 ) If you want 1AM, 3PM etc, you need to case it out:

日期部分(小时,[日期])返回军事时间(00至23)如果你想要凌晨1点,下午3点等,你需要指出:

SELECT Run_Time_Hour =
CASE DATEPART(HOUR, R.date_schedule)
    WHEN 0 THEN  '12AM'
    WHEN 1 THEN   '1AM'
    WHEN 2 THEN   '2AM'
    WHEN 3 THEN   '3AM'
    WHEN 4 THEN   '4AM'
    WHEN 5 THEN   '5AM'
    WHEN 6 THEN   '6AM'
    WHEN 7 THEN   '7AM'
    WHEN 8 THEN   '8AM'
    WHEN 9 THEN   '9AM'
    WHEN 10 THEN '10AM'
    WHEN 11 THEN '11AM'
    WHEN 12 THEN '12PM'
    ELSE CONVERT(varchar, DATEPART(HOUR, R.date_schedule)-12) + 'PM'
END
FROM
    dbo.ARCHIVE_RUN_SCHEDULE R

#9


0  

select case when [am or _pm] ='PM' and datepart(HOUR,time_received)<>12 
           then dateadd(hour,12,time_received) 
           else time_received 
       END 
from table

works

作品

#10


0  

to_char(current_timestamp,'YYYY') // to extract only DATE to_char(current_timestamp,'HH24') // to extract only HOUR

to_char(current_timestamp,'YYYY') //只提取日期to_char(current_timestamp,'HH24') //以提取时间。