You are on page 1of 5

Using functions

CONTAINS is used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server
full-text search on full-text indexed columns containing character-based data types.
CONTAINS can search for:  for a word, prefix of a word, a word near another word, synonym of a word, etc..
The most common use of the keyword CONTAINS requires two arguments and returns a number with either
a true or false result. It will be 1 (true) if it finds a match and 0 (false) if it doesn’t. The first argument is the
name of the table column to be searched. The second argument is the substring in need. The basic syntax is as
follows:
SELECT columnName FROM yourTable
WHERE CONTAINS ( columnName, 'Substringtobeserched' );
Note : that the column name isn’t enclosed in quotes, but the arguments for the CONTAINS SQL function
are enclosed in parentheses. The substring we are searching for is in quotes, as quotes must surround all
strings in SQL Server.
ISNULL
The ISNULL() function returns a specified value if the expression is NULL.
If the expression is NOT NULL, this function returns the expression.
Syntax
ISNULL(columnName, value)
columnName : The Column to test whether it is NULL
value : The value to return if column value is NULL
Note:
 ISNULL is used to replace a NULL value or column with another value or column. Whereas
COALESCE is used to return the first non-NULL value from a list of values or columns.
 The IS NULL operator is used to display all the rows for columns that do not have a value.
SELECT colname1,colname2,FROM tablename WHERE colname1 IS NULL
Concatenate
Concatenate strings from multiple columns into a single string
SQL Plus (+) operator is used to perform SQL Server Concatenate operation with multiple fields together.

Note :

 If any  of the string to be concatenated is NULL the result of concatenate with + operator gives
NULL in the output
 Use ISNULL function with + operator to replace NULL values with a space or any specific value.
 Use SQL CONVERT or CAST function to concatenate non string valued columns
SQL CONCAT function

CONCAT (string1, string2….stringN) : Concatenate string1, string2,…, stringN from multiple columns into
a single string
Note :

 When concatenate sting using SQL CONCAT function, it is possible to use system functions as well
example GETDATE()
  If all the string passed in SQL CONCAT function have a NULL value, the output will be also NULL.
 No need to use SQL CONVERT function to convert data type in SQL CONCAT function. CONCAT
function performs the conversion implicitly.

Note : Don’t forget to specify space character in between columns.to be concatenated for both + and
CONCAT function

String functions

LEFT() function extracts a number of characters from a string (starting from left).

LEFT(string, number_of_chars)

string (character_expression ) the string from which the characters are taken or extracted

number_of_chars (integer_expression ): Is a positive integer that specifies how many characters of the strings
will be returned. If the number exceeds the number of characters in string, it returns string

RIGHT () :function extracts a number of characters from a string (starting from right).
RIGHT(string, number_of_chars)

string (character_expression ) the string from which the characters are taken or extracted

number_of_chars (integer_expression ): Is a positive integer that specifies how many characters of the strings
will be returned. If the number exceeds the number of characters in string, it returns string

SUBSTRING() function extracts some characters from a string.


SUBSTRING(string, start, length)

string : the string from which the characters are taken or extracted

start : The start position. The first position in string is 1

length : The number of characters to extract. Must be a positive number


CHARINDEX() function searches for a substring in a string, and returns the position. If the substring is not
found, this function returns 0.

CHARINDEX(substring, string, start)

substring : the substring to search for

string : the string from which the substring is searched

start : The position where the search will start (if you do not want to start at the beginning of string). The first
position in string is 1

Lower (string) converts any uppercase letters to lowercase.


Upper (string) converts any lowercase letters to uppercase.

Date Functions

SQL Server comes with the following data types for storing a date or a date/time value in the database:

 DATE - format YYYY-MM-DD


 DATETIME - format: YYYY-MM-DD HH:MI:SS
 SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
 TIMESTAMP - format: a unique number

NOW()
Returns the current date and time
CURDATE()
Returns the current date
CURTIME()
Returns the current time
DATE()
Extracts the date part of a date or date/time expression
EXTRACT()
Returns a single part of a date/time

The EXTRACT() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

EXTRACT(unit FROM date)

DATE_ADD()
Adds a specified time interval to a date
DATE_ADD() function adds a specified time interval to a date.

DATE_ADD(date,INTERVAL expr type)

Type Value can be MICROSECOND,SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTER,YEAR,


DATE_SUB()
Subtracts a specified time interval from a date
DATEDIFF()
Returns the number of days between two dates
DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute
DATEPART(datepart,date)
datepart Abbreviation
year yy, yyyy
month mm, m
day dd, d
hour hh
minute mi, n
second ss, s

MONTH () Function Return the month part of a date:


YEAR() returns the year and MONTH() returns the month as a number.

DATENAME() function returns a specified part of a date. This function returns the result as a string value.

ROUND(number, decimals, operation)

Number: The number to be rounded

Decimals:. The number of decimal places to round number to

Operation: If 0, it rounds the result to the number of decimal. If another value than 0, it truncates the result to
the number of decimals. Default value is 0

FLOOR() function returns the largest integer value that is smaller than or equal to a number.

FLOOR(number)
The CEILING() function returns the smallest integer value that is larger than or equal to a number.
CEILING(number)
RAND() function :
This function in SQL Server is used to return a random decimal value and this value lies in the range greater
than and equal to zero (>=0) and less than 1.
to obtain a random integer R in the range i <= R < j, use the expression “FLOOR(i + RAND() * (j − i))”.

Example to return a random number >= 5 and <10:

SELECT FLOOR(RAND()*(10-5)+5);

You might also like