SQL Server DATEPART () function usage

usage:

DATEPART () function returns separate portions date / time, such as year, month, day, hour, minute and so forth. The return type is an integer . To return character can be used The DATENAME () function can be used to splice between the date and time, and the use DATEPART () similar to

grammar:

DATEPART(datepart,date/string)

date / string data type is a date and time, datepart is part of the data that you want to extract the return type is an integer such as: DATEPART (year, '2019-07-18 11: 44: 57.330') or DATEPART (yyyy '2019-07-1811: 44: 57.330')

Results: 2019

Other useful parameters are as follows:

datepart

Short form

year yy, yyyy
quarter qq, q
month mm, m
dayofyear house, the
day dd, d
week wk, ww
weekday dw
hour hh
minute me n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
TZoffset tz
ISO_WEEK isowk, isoww

 

 

 

 

 

 

 

 

 

 

 

 


 

If the date is not a type or a character string, we need to be converted to string (varchar) type, such as:

. 1  DECLARE  @shijian  decimal ( . 8 )
 2      SET  @shijian  =  20,190,718 
. 3  
. 4  the SELECT  GETDATE () the AS current time _DATE 
 . 5      , DATEPART ( YEAR , GETDATE ()) the AS current year _DATE
 . 6      , DATEPART (YY, GETDATE ()) AS current year abbreviated _DATE
 . 7      , DATEPART (YY, ' 2019-07-18 14:. 17: 25.793 ' ) AS current year abbreviated _string
 . 8     , DATEPART (YYYY, CAST ( CONVERT ( VARCHAR ( 8 ), @shijian ) AS DATE))   AS the current year shorthand _decimal

All of the above SQL execution results

 


 

[Title] get HS_ESA41M00 table under KEEP_OF_DATE field all year No. 1.1 to No. 1.15, No. 4.1 to No. 4.15, No. 7.1 to 7.15 No. No. No. 10.1 to 10.15 Data

select * from HS_ESA41M00 where DATEPART(MONTH, KEEP_OF_DATE) in(01,04,07,10) and DATEPART(DAY, KEEP_OF_DATE) between 01 and 15 

 

Guess you like

Origin www.cnblogs.com/EaTerLeader/p/11207137.html