SQL DAY Function

Example

Return the day of the month for a date:

SELECT DAY('2017/08/25') AS DayOfMonth;

Definition and Usage

The DAY() function returns the day of the month (from 1 to 31) for a specified date.

Syntax

DAY(date)

Continue reading SQL DAY Function

SQL DATEPART Function

Example

Return a specified part of a date:

SELECT DATEPART(year, '2017/08/25') AS DatePartInt;

Definition and Usage

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

This function returns the result as an integer value.

Syntax

DATEPART(interval, date)

Continue reading SQL DATEPART Function

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