SQL Date Functions



SQL Date Functions

Important built-in date functions in MySQL:

  • NOW() : Returns the current date and time
  • CURDATE() : Returns the current date
  • CURTIME() : Returns the current time
  • DATE() : Extracts the date part of a date or date/time expression
  • EXTRACT() : Returns a single part of a date/time
  • DATE_ADD() : Adds a specified time interval to a date
  • DATE_SUB() : Subtracts a specified time interval from a date
  • DATEDIFF() : Returns the number of days between two dates
  • DATE_FORMAT() : Displays date/time data in different formats

Built-in date functions in SQL Server:


  •    GETDATE() : Returns the current date and time
  •    DATEPART() : Returns a single part of a date/time
  •    DATEADD() : Adds or subtracts a specified time interval from a date
  •    DATEDIFF() : Returns the time between two dates
  •    CONVERT() : Displays date/time data in different formats

Data types for storing a date or a date/time value in the database:


  •    DATE: format YYYY-MM-DD
  •    DATETIME: format: YYYY-MM-DD HH:MI:SS
  •    TIMESTAMP: format: YYYY-MM-DD HH:MI:SS
  •    YEAR: format YYYY or YY

Data types for storing a date or a date/time value in the database:


  •     DATE: format YYYY-MM-DD
  •     DATETIME: format: YYYY-MM-DD HH:MI:SS
  •     SMALLDATETIME: format: YYYY-MM-DD HH:MI:SS
  •     TIMESTAMP: format: a unique number

Why we can't execute a stored procedure from a User Defined function(UDF)

Functions cannot "touch" any database but read them only. Stored procedures can do anything and everything with databases. You ...