{"id":4005,"date":"2025-06-07T09:35:39","date_gmt":"2025-06-07T09:35:39","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=4005"},"modified":"2025-06-07T09:35:40","modified_gmt":"2025-06-07T09:35:40","slug":"sql-string-functions","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-string-functions\/","title":{"rendered":"SQL String Functions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">String functions are essential for cleaning, transforming, and analyzing text data in SQL. Each DBMS offers slightly different syntax, but the core idea is the same.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Common String Functions with Examples<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Convert to upper\/lower case\nSELECT UPPER('sql tutorial');     -- SQL TUTORIAL\nSELECT LOWER('SQL TUTORIAL');     -- sql tutorial\n\n-- Get string length\nSELECT LENGTH('Hello');           -- MySQL\/PostgreSQL\nSELECT LEN('Hello');              -- SQL Server\n\n-- Concatenate strings\nSELECT CONCAT(first_name, ' ', last_name) FROM employees;      -- MySQL\/PostgreSQL\nSELECT first_name + ' ' + last_name FROM employees;            -- SQL Server\n\n-- Substring\nSELECT SUBSTRING('Database', 1, 4);    -- Data (All DBs)\nSELECT SUBSTR('Database', 1, 4);       -- MySQL\/PostgreSQL alternative\n\n-- Replace text\nSELECT REPLACE('Learn SQL', 'SQL', 'Python');  -- Learn Python\n\n-- Trim spaces\nSELECT TRIM('  SQL  ');               -- SQL (removes both ends)\nSELECT LTRIM('  SQL');                -- Removes leading spaces\nSELECT RTRIM('SQL  ');                -- Removes trailing spaces\n\n-- Position of substring\nSELECT POSITION('a' IN 'Database');   -- 2 (PostgreSQL)\nSELECT CHARINDEX('a', 'Database');    -- 2 (SQL Server)<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Useful Real-World Examples<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Get domain from email\nSELECT SUBSTRING(email, POSITION('@' IN email) + 1) FROM users;\n\n-- Concatenate first and last name\nSELECT CONCAT_WS(' ', first_name, last_name) FROM employees;  -- MySQL\n\n-- Clean user input\nSELECT TRIM(LOWER(username)) FROM users;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Common Functions by DBMS<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Function<\/th><th>MySQL \/ PostgreSQL<\/th><th>SQL Server<\/th><\/tr><\/thead><tbody><tr><td>Length<\/td><td><code>LENGTH()<\/code> \/ <code>CHAR_LENGTH()<\/code><\/td><td><code>LEN()<\/code><\/td><\/tr><tr><td>Substring<\/td><td><code>SUBSTRING()<\/code> \/ <code>SUBSTR()<\/code><\/td><td><code>SUBSTRING()<\/code><\/td><\/tr><tr><td>Concatenation<\/td><td><code>CONCAT()<\/code> \/ `<\/td><td><\/td><\/tr><tr><td>Replace<\/td><td><code>REPLACE()<\/code><\/td><td><code>REPLACE()<\/code><\/td><\/tr><tr><td>Position<\/td><td><code>POSITION()<\/code><\/td><td><code>CHARINDEX()<\/code><\/td><\/tr><tr><td>Trim<\/td><td><code>TRIM()<\/code>, <code>LTRIM()<\/code>, <code>RTRIM()<\/code><\/td><td>Same in SQL Server<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde0 <strong>Quick Recap<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Task<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>Convert Case<\/td><td><code>UPPER()<\/code>, <code>LOWER()<\/code><\/td><\/tr><tr><td>Find Length<\/td><td><code>LENGTH()<\/code>, <code>LEN()<\/code><\/td><\/tr><tr><td>Cut Substring<\/td><td><code>SUBSTRING('SQL', 1, 2)<\/code><\/td><\/tr><tr><td>Replace Text<\/td><td><code>REPLACE('SQL', 'S', 'P')<\/code><\/td><\/tr><tr><td>Concatenate Values<\/td><td><code>CONCAT(first, last)<\/code><\/td><\/tr><tr><td>Trim Spaces<\/td><td><code>TRIM(' SQL ')<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Combine string functions to clean and format output for reports, UI, and validation rules.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>String functions are essential for cleaning, transforming, and analyzing text data in SQL. Each DBMS offers slightly different syntax, but the core idea is the same. \ud83d\udd39 Common String Functions with Examples \ud83d\udd39 Useful Real-World Examples \ud83d\udd39 Common Functions by DBMS Function MySQL \/ PostgreSQL SQL Server Length LENGTH() \/ CHAR_LENGTH() LEN() Substring SUBSTRING() \/ [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-4005","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/4005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=4005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=4005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}