Nice little tutorial. +rep!
Thread: SQL: String Functions |
SQL Functions - String Functions
SQL provides a large amount of functions for string manipulations.
ASCII
The ASCII function is used to return a numeric value for the leftmost character. Each character has a numeric value that represents it.
Example:
Output:Code:SELECT ASCII('A')
65 is the numeric representation. An interesting property of characters is that each letter is one integer value away. This makes it great for adding and subtracting other values. You can perform math with characters.65
So:
Returns B. This converts A to it's ascii value and adds 1. Then it converts it back to a character. This shows that the letters are exactly one integer value away.Code:SELECT CHAR(ASCII('A')+1)
Thus we can use this method to convert chars from upper to lowercase by observing patterns in an ASCII chart.
This code converts A to lowercase "a".Code:SELECT CHAR(ASCII('A')+ASCII('a')-ASCII('A'))
Character Length
The character length function returns the number of letters in the string.
This function returns the number of characters in the string.Code:SELECT CHARACTER_LENGTH("Hiii")
Output:
Another method that does the same thing is CHAR_LENGTH.4
CONCAT method
This method is used to join strings together.
The function takes a variable number of arguments and joins them together.Code:SELECT CONCAT("James","Hi","There");
The returning value is:
Format FunctionJame****here
Returns a number formatted to a certain amount of decimals. The function prototype is:
The number num is formatted in a format similar to '#,###.##'. The number of decimals is D values.FORMAT(num,D)
Example:
Output:Code:SELECT FORMAT(1234.343332,3)
Lowercase Functions1,234.343
The function LCASE returns a string in lower case. This function does the same thing as the LOWER function.
Example:
Output:Code:SELECT LOWER("Test");
Trim Functionstest
There are three functions:
- LTRIM
- RTRIM
- TRIM
that remove the space from around the string. The LTRIM function removes all leading spaces before the left side of the string. The RTRIM function removes all trailing spaces after the right side of the string. The TRIM function removes all leading and trailing spaces.
Examples:
Output:Code:SELECT LTRIM(" Test"); SELECT RTRIM("Test "); SELECT TRIM(" Test "); SELECT RTRIM(" Test ");
ReplaceTest
Test
Test
Test
The generic function is:
The replace function replaces all instances of a string replace with new.Code:REPLACE(str,replace,new)
Example:
The output:Code:SELECT REPLACE("Test this is just a test test test","test","yo!");
All instances of "this" will be replaced with "yo". This method is case-sensitive. Notice, how Test was not replaced?Test this is just a yo! yo! yo!
Reverse Function
This function returns a string that is reversed.
Example:
The output is:Code:SELECT REVERSE("test");
Substring Functiontset
There are two versions of this function:
andCode:SUBSTR(str,pos)
The first function copies all the characters from index pos to the end of the string. One thing to note, the index of the first character is one.Code:SUBSTR(str,pos,len)
Example:
Output:Code:SELECT SUBSTR("test",2);
The second version specifies how many characters to copy and where to start copying from.est
Example:
This function starts at the first character and copies two characters.Code:SELECT SUBSTR("test",1,2);
Output:
Uppercase Functionte
The UPPER function converts a string into upper case.
Example:
Output:Code:SELECT UPPER("test");
TEST
Nice little tutorial. +rep!
Very handy list of functions. I use them a LOT. +rep
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
There are currently 1 users browsing this thread. (0 members and 1 guests)