Saturday, February 25, 2023

SQL Server - SQL DATE FUNCTIONS

 SQL DATE FUNCTIONS

 


Date functions are used to format dates and store into tables and display.

Below are the important 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

 

Below are the important 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

 

 

SQL Date Data Types

MySQL comes with the following data types for storing 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

SQL Server comes with the following data types for storing 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

 

Example:

SELECT Now();

SELECT Date();

SELECT Time();

SELECT DATEDIFF('2023-01-13','2023-01-03') AS DateDiff;

No comments:

Post a Comment

SQL Server - Introduction to SQL

  Introduction to SQL   SQL is a standard language for accessing and manipulating databases. SQL stands for Structured Query Language. SQL...