SQL DATENAME Function

Example

Return a specified part of a date:

SELECT DATENAME(year, '2017/08/25') AS DatePartString;

Definition and Usage

The DATENAME() function returns a specified part of a date.

This function returns the result as a string value.

Syntax

DATENAME(interval, date)

Continue reading SQL DATENAME Function

SQL DATEFROMPARTS Function

Example

Return a date from its parts:

SELECT DATEFROMPARTS(2018, 10, 31) AS DateFromParts;

Definition and Usage

The DATEFROMPARTS() function returns a date from the specified parts (year, month, and day values).

Syntax

DATEFROMPARTS(year, month, day)

Continue reading SQL DATEFROMPARTS Function

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