In this article, we will learn how to use Built-In functions in SQL Server.
What are Built-In functions?
A built-in function is a piece for programming that takes zero or more inputs and returns a value. There are many built-in functions in SQL Server. Here we are discussing about string, date, and time functions in SQL Server.
String Functions
ASCII()
ASCII (American Standard Code for Information Interchange) is a code that uses numbers to represent characters. This function returns the ASCII value for the specific character. To get the ASCII code of a character, we can call the ASCII() function. It takes a character as an argument and returns the ASCII code of the character. For example:
CHAR()
This function returns the character based on the ASCII code. It takes a numeric value as an argument and returns the character. For example:
CHARINDEX()
This function returns the position of a substring in a string. This function return 0(Zero) if the substring is not found. It takes three arguments. Syntax,
In the above syntax “substring” is the string to search for. “string” is the string to be searched. “start_position” is the position where the search will start. This is an optional parameter. If it is zero or negative value, the search starts at the beginning of the string.
SOUNDEX()& DIFFERENCE()
This function compares two SOUNDEX values, and returns an integer. The integer value indicates the match for the two SOUNDEX values from 0 to 4, where 0 indicates weak or no similarity and 4 indicates strong similarity.
The SOUNDEX() function converts a string to a four-character code. This function compares the similarity between strings in terms of their sounds when it is spoken.
For example,
FORMAT()
Using this function we can format a value with a specified format. This function can be applied to various types of values such as integers, floating-point numbers, date, or time. It takes three arguments or parameters. Syntax:
FORMAT(value, format, culture)
value: is the type of value to be formatted. It can be a number, a date or time.
format : it specifies the format you want to apply to the first argument.
culture : is optional parameter, specifies a culture.
LEFT()
This function is used to get a number of characters from a string starting from left. This function takes two arguments. The first argument specifies the original string and the second argument specifies the number of characters from the most-left. For example:
LEN()
This function returns the length of a string. This function takes one argument as the string to be considered. For example:
LOWER() & UPPER()
LOWER() converts a string to lower-case and UPPER() function converts a string to upper-case. These functions take one argument as a string.
LTRIM() & RTRIM()
LTRIM function removes all leading spaces from a string starting from left and RTRIM() function removes all leading spaces from a string from right.
PATINDEX()
The PATINDEX() function returns the first occurrence of a pattern in a string. It returns 0 if the pattern is not found. It takes two arguments. First one is the pattern to search and it must be surrounded by “%”. Here we can also use other wildcards like “%”, “_”, “[]”, “[^]”. The second one is the string to be searched.
REPLACE()
This function replaces all occurrences of a substring within a string, with a new substring. This function takes three arguments. The first is the string that will be used as reference. The second argument is a character or a sub-string to look for in the first argument. The third argument is a character or a sub-string to replace the second argument. For example: If we want to replace “SQL Server” as “MS SQL Server” in the “'Built-In Functions in SQL Server – Part One'” string.
REPLICATE()
This function repeats a string with a specified number of times. It takes two arguments. The first argument is the string to repeat and the second argument is no of time to repeat the string. For example:
REVERSE()
This function reverses a string. It takes one argument as string to reverse.
For example,
SPACE()
This function returns a string of the specified number of space characters.
STR()
This function converts a numeric value to a string value. It takes three arguments.
- The first argument is the number to convert to string.
- The second argument is the length of the returning string and default value is 10.
- The third argument is the number of decimals to display in the returning string and the default value is 0.
The second and the third arguments are optional. For example,
STUFF()
This function removes a part of a string and then inserts another part into the string, in a specified position. It takes four arguments. The first argument is the string to be processed or modified. The second argument is the position in string to start deletion and insertion of some characters. The third argument is the number of characters to delete from string. The fourth argument is the new sub-string to replace into string at the start position. For example,
SUBSTRING()
This function extracts a substring with a specified length starting from a position in a string. It takes three arguments. The first argument is the string from which we extract a character or sub-string. The second argument specifies the position where the returned substring starts. The third argument is the number of characters of the substring to be returned ant it must be a positive number. For example:
Date and Time Functions
CURRENT_TIMESTAMP
This function returns the current date and time, in a “YYYY-MM-DD hh:mm:ss.mmm” format. For example:
DATEADD()
This function adds a time/date interval to a input date and then returns the modified date. It takes three arguments. The first argument is the time/date interval to which we will add value. The second argument is the number of interval to be added to first argument. It can be positive or negative number. The third argument is the date that will be modified.
For example,
DATEDIFF()
This function returns the difference between two dates in years, months, weeks, etc. It takes three arguments.
The first argument is “datepart” which is the part of date like a year, a month, a week, a day etc.
The second and third arguments are “startdate” and “enddate” to be compared.
For example,
DATEFROMPARTS()
This function returns a date from the specified parts like year, month, and day values. For example:
DATENAME() & DATEPART()
The DATENAME() and DATEPART() function returns a specified part of a date. But the DATENAME() function returns the result as string value and DATEPART() return the result as integer value.
DAY(), MONTH() & YEAR()
The DAY() function returns the day of the month for a specified date. The value is from 1 to 31.
The MONTH() function returns the month part for a specified date. The value is from 1 to 12.
The YEAR() function returns the year part for a specified date.
GETDATE(),GETUTCDATE() & SYSDATETIME()
The GETDATE() function returns the current database date and time, in a “YYYY-MM-DD hh:mm:ss.mmm” format.
The GETUTCDATE() function returns the current database date and time in UTC, in a “YYYY-MM-DD hh:mm:ss.mmm” format.
The SYSDATETIME() function returns the date and time of the computer where the SQL Server instance is running.
ISDATE()
This function checks an expression is valid date or not. It returns 1 if it is a valid date, otherwise 0.
In the above article, we learned how to use string, date and time built-in functions in SQL Server. Hope this will help the readers. Happy Coding!!!
Note: Reference from C# Corner.