PostgreSQL

Chapter 7 - DQL (Data Query Language)

STRING functions

In PostgreSQL, STRING functions are essential for manipulating and querying text data. These functions allow you to perform various operations on string values, such as extracting substrings, changing case, and searching for patterns. Understanding these functions is crucial for working with textual data in databases, as they help in formatting and querying text effectively.

Key PostgreSQL STRING Functions

  1. LENGTHFunction
    • Purpose: Returns the number of characters in a string.
    • Example:
    SELECT LENGTH('Account Number') AS length_of_string;
    

    This query returns the length of the string "Account Number".

  2. UPPERand LOWER Functions
    • Purpose: Converts a string to uppercase or lowercase.
    • Example:
    SELECT UPPER('john doe') AS uppercase_name;
    SELECT LOWER('JOHN DOE') AS lowercase_name;
    

    The first query converts "john doe" to "JOHN DOE", while the second converts "JOHN DOE" to "john doe".

  3. SUBSTRINGFunction
    • Purpose: Extracts a portion of a string based on specified positions.
    • Example:
    SELECT SUBSTRING('Account Number' FROM 1 FOR 8) AS substring_example;
    

    This query extracts the first 8 characters from the string "Account Number", resulting in "Account".

  4. TRIMFunction
    • Purpose: Removes whitespace from the beginning and end of a string.
    • Example:
    SELECT TRIM('   extra spaces   ') AS trimmed_string;
    

    This query removes the extra spaces around "extra spaces".

  5. REPLACEFunction
    • Purpose: Replaces all occurrences of a substring with another substring.
    • Example:
      SELECT REPLACE('Bank Account', 'Account', 'Number') AS replaced_string;
      

    This query replaces "Account" with "Number" in the string "Bank Account", resulting in "Bank Number".

  6. CONCATFunction
    • Purpose: Concatenates multiple strings into one.
    • In PostgreSQL, STRING functions are essential for manipulating and querying text data. These functions allow you to perform various operations on string values, such as extracting substrings, changing case, and searching for patterns. Understanding these functions is crucial for working with textual data in databases, as they help in formatting and querying text effectively.

      Key PostgreSQL STRING Functions

      1. LENGTHFunction
        • Purpose: Returns the number of characters in a string.
        • Example:
        SELECT LENGTH('Account Number') AS length_of_string;
        

        This query returns the length of the string "Account Number".

      2. UPPERand LOWER Functions
        • Purpose: Converts a string to uppercase or lowercase.
        • Example:
        SELECT UPPER('john doe') AS uppercase_name;
        SELECT LOWER('JOHN DOE') AS lowercase_name;
        

        The first query converts "john doe" to "JOHN DOE", while the second converts "JOHN DOE" to "john doe".

      3. SUBSTRINGFunction
        • Purpose: Extracts a portion of a string based on specified positions.
        • Example:
        SELECT SUBSTRING('Account Number' FROM 1 FOR 8) AS substring_example;
        

        This query extracts the first 8 characters from the string "Account Number", resulting in "Account".

      4. TRIMFunction
        • Purpose: Removes whitespace from the beginning and end of a string.
        • Example:
        SELECT TRIM('   extra spaces   ') AS trimmed_string;
        

        This query removes the extra spaces around "extra spaces".

      5. REPLACEFunction
        • Purpose: Replaces all occurrences of a substring with another substring.
        • Example:
          SELECT REPLACE('Bank Account', 'Account', 'Number') AS replaced_string;
          

        This query replaces "Account" with "Number" in the string "Bank Account", resulting in "Bank Number".

      6. CONCATFunction
        • Purpose: Concatenates multiple strings into one.
        • Example:
        SELECT CONCAT('Customer ID: ', '12345') AS concatenated_string;
        

        This query concatenates "Customer ID: " with "12345", resulting in "Customer ID: 12345".

        These functions are commonly used in queries to format, analyze, and manipulate text data in PostgreSQL.

      Tansy SQL Course - STRING functions - Video Thumbnail
Comments(0 comments)

Comments Not Found