SQL Server System Functions: Date Functions

Original: SQL Server System Functions: Date Functions

1, returns the current date and time


   
   
  1. select GETDATE() '当前日期-精确到33毫秒'
  2. select GETUTCDATE() 'UTC日期和时间-精确到33毫秒'
  3. select SYSDATETIME() '当前日期和时间-精确到100纳秒(高精度)'
  4. select SYSUTCDATETIME() 'UTC-精确到100纳秒(高精度)'
  5. select SYSDATETIMEOFFSET() '当前日期与UTC之间的差值(时分)'

2, conversion offset from one into another, from the UTC-05: 00 Switch UTC + 03: 00:

'2007-08-1210: 20: 35.9788989' is UTC minus '05: 00 'counted out after,
so UTC is' 2007-08-1210: 20: 35.9788989 'plus '05: 00',
that is, '2007-08-1215: 20: 35.9788989',
then '2007-08-1215: 20: 35.9788989' plus '03: 00 ',
which is' 2007-08-1218: 20: 35.9788989'

select SWITCHOFFSET('2007-08-12 10:20:35.9788989 -05:00','+03:00')
  
  

The common date and time based to date time offset, the offset value only increased after the date and time to return: 2007-08-1210: 20: 35.0000000 -05: 00

select TODATETIMEOFFSET('2007-08-12 10:20:35','-05:00')
  
  

3, part of the date display string


   
   
  1. select SYSDATETIME(),
  2. DATENAME( year,SYSDATETIME()),
  3. DATENAME( quarter,SYSDATETIME()),
  4. DATENAME( month,SYSDATETIME()),
  5. DATENAME( dayofyear,SYSDATETIME()),
  6. DATENAME( day,SYSDATETIME()),
  7. DATENAME( week,SYSDATETIME()),
  8. DATENAME( weekday,SYSDATETIME()),
  9. DATENAME( hour,SYSDATETIME()),
  10. DATENAME( minute,SYSDATETIME()),
  11. DATENAME( second,SYSDATETIME()),
  12. DATENAME(millisecond,SYSDATETIME()),
  13. DATENAME( microsecond,SYSDATETIME()),
  14. DATENAME(nanosecond,SYSDATETIME()),
  15. DATENAME(TZoffset,SYSDATETIME()),
  16. DATENAME(ISO_WEEK,SYSDATETIME())

4, part of the date display integer


   
   
  1. select YEAR( GETDATE()),
  2. MONTH( getdate()),
  3. DAY( getdate())
  4. select SYSDATETIME(),
  5. DATEPART( year,SYSDATETIME()),
  6. DATEPART( quarter,SYSDATETIME()),
  7. DATEPART( month,SYSDATETIME()),
  8. DATEPART( dayofyear,SYSDATETIME()),
  9. DATEPART( day,SYSDATETIME()),
  10. DATEPART( week,SYSDATETIME()),
  11. DATEPART( weekday,SYSDATETIME()),
  12. DATEPART( hour,SYSDATETIME()),
  13. DATEPART( minute,SYSDATETIME()),
  14. DATEPART( second,SYSDATETIME()),
  15. DATEPART(millisecond,SYSDATETIME()),
  16. DATEPART( microsecond,SYSDATETIME()),
  17. DATEPART(nanosecond,SYSDATETIME()),
  18. DATEPART(TZoffset,SYSDATETIME()),
  19. DATEPART(ISO_WEEK,SYSDATETIME())

5, increase, decrease date value


   
   
  1. SELECT DATEADD( quarter, 1, getdate()),
  2. DATEADD( quarter, -1, getdate())

6, the difference between the two dates


   
   
  1. select DATEDIFF( DAY, '2011-08-09', GETDATE()),
  2. DATEDIFF( MONTH, '2011-08-09', GETDATE())

 

Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12019915.html