Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

String Functions in SQL

String Functions in SQL

SQL provides a rich set of string functions to manipulate, search, format, and extract information from string data. These functions vary slightly by SQL dialect (e.g., SQL Server, MySQL, PostgreSQL), but many are shared across platforms.


? Common String Functions in SQL

FunctionDescriptionExample
LEN() / LENGTH()Returns the length of a stringLEN('Hello') ? 5
LEFT()Returns the left part of a stringLEFT('Hello', 2) ? 'He'
RIGHT()Returns the right part of a stringRIGHT('Hello', 3) ? 'llo'
SUBSTRING() / SUBSTR()Extracts part of a stringSUBSTRING('Hello', 2, 3) ? 'ell'
CHARINDEX() / INSTR()Finds the position of a substringCHARINDEX('e', 'Hello') ? 2
UPPER() / UCASE()Converts to uppercaseUPPER('hello') ? 'HELLO'
LOWER() / LCASE()Converts to lowercaseLOWER('HELLO') ? 'hello'
LTRIM() / TRIM(LEADING)Removes leading spacesLTRIM(' hello') ? 'hello'
RTRIM() / TRIM(TRAILING)Removes trailing spacesRTRIM('hello ') ? 'hello'
TRIM()Removes both leading and trailing spacesTRIM(' hello ') ? 'hello'
REPLACE()Replaces occurrences of a substringREPLACE('SQL Tutorial', 'SQL', 'MySQL') ? 'MySQL Tutorial'
REVERSE()Reverses a stringREVERSE('abc') ? 'cba'
CONCAT() / +Concatenates two or more stringsCONCAT('Hello', 'World') ? 'HelloWorld'
FORMAT()Formats numbers or dates as stringsFORMAT(1234.56, 'N2') ? '1,234.56'
STR() (SQL Server)Converts numeric to stringSTR(123.45, 6, 2) ? '123.45'
SPACE(n)Returns a string with n spacesSPACE(3) ? ' '
REPLICATE()Repeats a string n timesREPLICATE('A', 3) ? 'AAA'

? Examples

-- Extract middle name from full nameSELECT SUBSTRING(FullName, 6, 4) AS MiddleNameFROM Employees;-- Clean and standardize dataSELECT TRIM(LOWER(UserName)) AS CleanedUserFROM Users;-- Concatenate first and last nameSELECT FirstName + ' ' + LastName AS FullNameFROM Employees;

Note: Use + for SQL Server, || for PostgreSQL, and CONCAT() for MySQL when concatenating strings.


? Dialect Differences

FunctionSQL ServerMySQLPostgreSQL
LengthLEN()LENGTH()LENGTH()
SubstringSUBSTRING()SUBSTRING()SUBSTRING()
In-stringCHARINDEX()INSTR()POSITION()
Concatenate+CONCAT()`

Let me know which SQL database you're using, and I can tailor a string function cheat sheet specifically for that system!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql