+ Reply to Thread
Results 1 to 3 of 3

Thread: SQL: String Functions

  1. #1
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    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:

    Code:
    SELECT ASCII('A')
    Output:

    65
    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.

    So:

    Code:
    SELECT CHAR(ASCII('A')+1)
    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.

    Thus we can use this method to convert chars from upper to lowercase by observing patterns in an ASCII chart.

    Code:
    SELECT CHAR(ASCII('A')+ASCII('a')-ASCII('A'))
    This code converts A to lowercase "a".

    Character Length

    The character length function returns the number of letters in the string.

    Code:
    SELECT CHARACTER_LENGTH("Hiii")
    This function returns the number of characters in the string.

    Output:

    4
    Another method that does the same thing is CHAR_LENGTH.

    CONCAT method

    This method is used to join strings together.

    Code:
    SELECT CONCAT("James","Hi","There");
    The function takes a variable number of arguments and joins them together.

    The returning value is:

    Jame****here
    Format Function

    Returns a number formatted to a certain amount of decimals. The function prototype is:

    FORMAT(num,D)
    The number num is formatted in a format similar to '#,###.##'. The number of decimals is D values.

    Example:

    Code:
    SELECT FORMAT(1234.343332,3)
    Output:

    1,234.343
    Lowercase Functions

    The function LCASE returns a string in lower case. This function does the same thing as the LOWER function.

    Example:

    Code:
    SELECT LOWER("Test");
    Output:

    test
    Trim Functions

    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:

    Code:
    SELECT LTRIM("      Test");
    SELECT RTRIM("Test       ");
    SELECT TRIM("       Test      ");
    SELECT RTRIM("         Test          ");
    Output:

    Test
    Test
    Test
    Test
    Replace

    The generic function is:

    Code:
    REPLACE(str,replace,new)
    The replace function replaces all instances of a string replace with new.

    Example:

    Code:
    SELECT REPLACE("Test  this is just a test test test","test","yo!");
    The output:

    Test this is just a yo! yo! yo!
    All instances of "this" will be replaced with "yo". This method is case-sensitive. Notice, how Test was not replaced?

    Reverse Function

    This function returns a string that is reversed.

    Example:

    Code:
    SELECT REVERSE("test");
    The output is:

    tset
    Substring Function

    There are two versions of this function:

    Code:
    SUBSTR(str,pos)
    and

    Code:
    SUBSTR(str,pos,len)
    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.

    Example:

    Code:
    SELECT SUBSTR("test",2);
    Output:

    est
    The second version specifies how many characters to copy and where to start copying from.

    Example:

    Code:
    SELECT SUBSTR("test",1,2);
    This function starts at the first character and copies two characters.

    Output:

    te
    Uppercase Function

    The UPPER function converts a string into upper case.

    Example:

    Code:
    SELECT UPPER("test");
    Output:

    TEST

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: SQL: String Functions

    Nice little tutorial. +rep!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: SQL: String Functions

    Very handy list of functions. I use them a LOT. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Assembly, Intro To Algorithms With String Functions (Win32, NASM)
    By RhetoricalRuvim in forum Assembly Tutorials
    Replies: 3
    Last Post: 08-21-2011, 04:56 PM
  2. Replies: 11
    Last Post: 03-02-2011, 07:00 PM
  3. Replies: 4
    Last Post: 02-06-2011, 01:37 PM
  4. String formatting functions?
    By FireGator in forum PHP Development
    Replies: 5
    Last Post: 09-10-2010, 11:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts