SQL > SQL Date Functions > Datepart Function

DATEPART is a SQL Server function that extracts a specific part of the date/time value.

Syntax

The syntax for the DATEPART function is as follows:

DATEPART (part_of_day, expression)

where part_of_day can have the following:

 datepart  abbreviation 
 year  yy, yyyy 
 quarter  qq, q 
 month  mm, m 
 dayofyear  dy, y 
 day  dd, d 
 week  wk, ww 
 hour  hh 
 minute  mi, n 
 second  ss, s 
 millisecond  ms 
 microsecond  mcs 
 nanosecond  ns 
 TZoffset  tz 
 ISO_WEEK  isowk, isoww 

Examples

Example 1

The SQL statement,

SELECT DATEPART (yyyy, '2000-01-20');

produces the following result:

2000

Example 2

The SQL statement,

SELECT DATEPART (dy, '2000-02-10');

produces the following result:

41

2000-02-10 is the 41st day in the year 2000.

List of SQL Date Functions

Function NameDescription
 DATEADD  Adds an interval to a date value in SQL Server. 
 DATEDIFF  Calculates the difference between two dates in MySQL and SQL Server. 
 DATEPART  Extracts a specific part of a date/time value in SQL Server. 
 GETDATE  Retrieves database time in SQL Server. 
 SYSDATE  Retrieves database time in Oracle and MySQL. 
 EXTRACT  Retrieves a certain component of a date or timestamp value. 

Next: SQL GETDATE

This page was last updated on October 12, 2024.




Copyright © 2024   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact