SQL COALESCE Function

Example

Return the first non-null value in a list:

SELECT COALESCE(NULL, NULL, NULL, 'iampsp.com', NULL, 'Example.com');

Definition and Usage

The COALESCE() function returns the first non-null value in a list.

Syntax

COALESCE(val1, val2, ...., val_n)

Continue reading SQL COALESCE Function

SQL CAST Function

Example

Convert a value to an int datatype:

SELECT CAST(25.65 AS int);

Definition and Usage

The CAST() function converts a value (of any type) into a specified datatype.

Tip: Also look at the CONVERT() function.

Syntax

CAST(expression AS datatype(length))

Continue reading SQL CAST Function

SQL YEAR Function

Example

Return the year part of a date:

SELECT YEAR('2017/08/25') AS Year;

Definition and Usage

The YEAR() function returns the year part for a specified date.

Syntax

YEAR(date)

Continue reading SQL YEAR Function

SQL SYSDATETIME Function

Example

Return the date and time of the SQL Server:

SELECT SYSDATETIME() AS SysDateTime;

Definition and Usage

The SYSDATETIME() function returns the date and time of the computer where the SQL Server is running.

Syntax

SYSDATETIME()

Continue reading SQL SYSDATETIME Function

SQL MONTH Function

Example

Return the month part of a date:

SELECT MONTH('2017/08/25') AS Month;

Definition and Usage

The MONTH() function returns the month part for a specified date (a number from 1 to 12).

Syntax

MONTH(date)

Continue reading SQL MONTH Function

SQL ISDATE Function

Example

Check if the expression is a valid date:

SELECT ISDATE('2017-08-25');

Definition and Usage

The ISDATE() function checks an expression and returns 1 if it is a valid date, otherwise 0.

Syntax

ISDATE(expression)

Continue reading SQL ISDATE Function

SQL GETDATE Function

Example

Return the current database system date and time:

SELECT GETDATE();

Definition and Usage

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

Tip: Also look at the CURRENT_TIMESTAMP function.

Syntax

GETDATE()

Continue reading SQL GETDATE Function

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