Sunday, November 9, 2014

Built-in functions in SQL - Part 1

There are some important built-in functions in T-SQL which are used on a daily basis. ISNULL() & DATEDIFF() are some of such

1) ISNULL()

ISNULL ( check_expression , replacement_value )
The first parameter is the expression to be checked, secondly we have the result to give out in case the expression is NULL. This can be used to null check column values.

For an instance:
ISNULL(columnName, 0) will give out zero to indicate the value of the particular column is null, so that it can be skipped

Applications:
In a select statement within the where clause ISNULL(columnName, 0)=1 could be used so that any row having null values for this particular column would be skipped.


2) DATEDIFF()

WHERE DATEDIFF(DAY, tableName.DateTimeCloumn, GETDATE()) > 7

DATEDIFF returns the time between two dates. The first parameter is the datepart (eg: day, year, month, etc). The second and third parameters are start date and end date respectively.



No comments:

Post a Comment