SQL DATEDIFF Function

Example

Return the difference between two date values, in years:

SELECT DATEDIFF(year, '2017/08/25', '2011/08/25') AS DateDiff;

Definition and Usage

The DATEDIFF() function returns the difference between two dates, as an integer.

Syntax

DATEDIFF(interval, date1, date2)

Continue reading SQL DATEDIFF Function

SQL DATEADD Function

Example

Add one year to a date, then return the date:

SELECT DATEADD(year, 1, '2017/08/25') AS DateAdd;

Definition and Usage

The DATEADD() function adds a time/date interval to a date and then returns the date.

Syntax

DATEADD(interval, number, date)

Continue reading SQL DATEADD Function

SQL CURRENT_TIMESTAMP Function

Example

Return the current date and time:

SELECT CURRENT_TIMESTAMP;

Definition and Usage

The CURRENT_TIMESTAMP function returns the current date and time, in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format.

Tip: Also look at the GETDATE() function. Continue reading SQL CURRENT_TIMESTAMP Function

SQL SUM Function

Example

Return the sum of the “Quantity” field in the “OrderDetails” table:

SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;

Definition and Usage

The SUM() function calculates the sum of a set of values.

Note: NULL values are ignored.

Syntax

SUM(expression)

Continue reading SQL SUM Function

SQL SQUARE Function

Example

Return the square of a number:

SELECT SQUARE(64);

Definition and Usage

The SQUARE() function returns the square of a number.

Syntax

SQUARE(number)

Continue reading SQL SQUARE Function

SQL SQRT Function

Example

Return the square root of a number:

SELECT SQRT(64);

Definition and Usage

The SQRT() function returns the square root of a number.

Syntax

SQRT(number)

Continue reading SQL SQRT Function

SQL SIGN Function

Example

Return the sign of a number:

SELECT SIGN(255.5);

Definition and Usage

The SIGN() function returns the sign of a number.

This function will return one of the following:

  • If number > 0, it returns 1
  • If number = 0, it returns 0
  • If number < 0, it returns -1

Syntax

SIGN(number)

Continue reading SQL SIGN Function

SQL ROUND Function

Example

Round the number to 2 decimal places:

SELECT ROUND(235.415, 2) AS RoundValue;

Definition and Usage

The ROUND() function rounds a number to a specified number of decimal places.

Tip: Also look at the FLOOR() and CEILING() functions.

Syntax

ROUND(number, decimals, operation)

Continue reading SQL ROUND Function