SQL Notes 3
SQL Notes 3
This function will return NULL as value if the specified argument value (in case of fields of
a table) is NULL.
Will count and return the total number of characters in the given string (within quotes)
mysql> SELECT LENGTH('RESURRECTION OF OSIRIS’);
+------------------------------------------------------------+
| LENGTH('RESURRECTION OF OSIRIS’)
+------------------------------------------------------------+
| 22 |
+------------------------------------------------------------+
1 row in set (0.00 sec)
Will also count the blank spaces in between the first and last characters of the given
string.
Write the statement to show the total number of characters of the field PaymentMode of
the table named SALES -
If the value of the PaymentMode of third record has been changed to NULL. Then what
will be the output for the same above statement ???
The output for a record with a NULL value for a Field will be always – NULL and not the
total characters in NULL (which is 4)
An independent statement with value as NULL as argument will return Error / No output.
5. LEFT(string, N) – returns the left most characters(starting from index address 1) from
the specified ‘string’ of total ‘N’ numbers / length of characters.
This function will extract first 4 characters from the first index address. (printable
character)
1 2 3 4 5 6 7 8 9 10 11 12
R E S U R R E C T I O N
In the above example in the output, leading blank spaces have been ignored and the
counting begins from the first printable character which is ‘R’.
Will return no value / error as the second argument has not been specified
In the above example the function has returned the 7 left most characters (including
blank spaces if any) of each respective value of the field named PaymentMode from the
table SALES.
6. RIGHT(string, N) - returns the right most characters(starting from the last index
address -1) from the specified ‘string’ of total ‘N’ numbers / length of characters.
This function will extract last 4 characters from the last index address.
-22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -
5 -4 -3 -2 -1
R E S U R E C T I O N \b O F \b O S I R I S
In the above example in the output, trailing blank spaces have been ignored and the
counting began from the right most first printable character which is ‘S’ and total 4
characters have been extracted.
Will return no value / error as the second argument has not been specified
1 2 3 4 5 6 7 8 9 10
A R M A G E D D O N
In the above example ‘DON’ is a substring (perfect match) in the main string
'ARMAGEDDON' .
So, the function will return the index address of the first character of the substring which
is 8.
In the above example there is no such substring ‘DONN’ (exact match) in the main string
'ARMAGEDDON' .
So, the function will return Error / No output.
1 2 3 4 5 6 7 8 9 10 11 12 13
H A K U N A M A T A T A
In the above example there are two exact matches of the substring ‘TA’ in the main
string but every time the function will return the index address of the first character of
the first matching substring and which will be 10.
8. LTRIM(string) – returns the new string without the leading blank spaces if any
between the opening quote and first printable character of the string.
In the above example the function LTRIM( ) has ignored / removed all the leading blank
spaces of the string.
In the above example the function LTRIM( ) has ignored / removed all the leading blank
spaces of the string and even the trailing spaces can’t be identified (removed or still
there)
Now, please note that the SQL string functions have in-built structure to ignore/remove
the leading or trailing blank spaces between the quotes and the first printable character
(from either of the sides)
9. RTRIM(string) - returns the new string without the trailing blank spaces if any.
In the above example the trailing spaces have been removed / ignored and then the
output has been shown.
In the above example both the trailing and trailing spaces have been removed although
the function used here is RTRIM( ) but then both the leading and trailing blank spaces
have been removed.
10. TRIM(string) - returns the new string without the leading and trailing blank spaces
if any. And, TRIM( ‘substring’[, FROM] ‘mainstring’) - returns the new string with
mainstring-substring.
SELECT TRIM( ‘substring’, FROM Column_Name) FROM Table_Name; - returns the
new string without the substring from each row of the mentioned column.
In the above example both the trailing and trailing spaces have been removed by the
function TRIM( ).
‘ \b \b \b E N D S \b W E L L \b \b \b ’
STRING
blank space
** According to your NCERT Book – The below two application of TRIM also works.
In the above example substring ‘OF ‘ is trimmed from the main string ‘RESURRECTION OF
OSIRIS’
In the above example substring ‘OF ‘ is trimmed from the main string ‘RESURRECTION OF
OSIRIS’ using the FROM keyword within the argument value of TRIM( ).
In the above example substring ‘OT’ does not exist in the main string ‘RESURRECTION OF
OSIRIS’.
So, when we try to remove / trim a non-existing substring from the main string then the
mainstring will be displayed as it is.
Consider the below given column PaymentMode from the table SALES.
| SELECT PaymentMode FROM SALES; |
+-----------------------------------------------------------+
| CREDIT CARD |
| DEBIT CARD |
| NULL |
| BANK FINANCE |
| CREDIT CARD |
| BANK FINANCE |
+-----------------------------------------------------------+
6 rows in set (0.01 sec)
MID(“MEASURES” , 3, 5) =ASURE
*************