0% found this document useful (0 votes)
216 views

MySQL Cheat Sheet String Functions

This document provides a summary of string functions in MySQL including: - Functions for measurement like LENGTH(), CHAR_LENGTH() and OCTET_LENGTH() - Functions for conversion between types like BIN(), HEX(), and CONV() - Functions for modification of strings like CONCAT(), INSERT(), LEFT(), and REPLACE()

Uploaded by

hanuka92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views

MySQL Cheat Sheet String Functions

This document provides a summary of string functions in MySQL including: - Functions for measurement like LENGTH(), CHAR_LENGTH() and OCTET_LENGTH() - Functions for conversion between types like BIN(), HEX(), and CONV() - Functions for modification of strings like CONCAT(), INSERT(), LEFT(), and REPLACE()

Uploaded by

hanuka92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

MySQLCHEATSHEET:STRINGFUNCTIONS

bymysqlbackupftp.comwith

MEASUREMENT

CONVERSION

MODIFICATION

Returnastringcontainingbinaryrepresentationofa
number

Returnnumericvalueofleftmostcharacter

Returnconcatenatedstring

BIN(12)='1100'
Returnlengthofargumentinbits

BIT_LENGTH('MySql')=40
Returnnumberofcharactersinargument

CHAR_LENGTH('MySql')=5
CHARACTER_LENGTH('MySql')=5
Returnthelengthofastringinbytes

LENGTH('')=2
LENGTH('A')=1
OCTET_LENGTH('')=2
OCTET_LENGTH('X')=1
Returnasoundexstring

SOUNDEX('MySql')='M240'
SOUNDEX('MySqlDatabase')='M24312'
Comparetwostrings

STRCMP('A','A')=0
STRCMP('A','B')=1
STRCMP('B','A')=1

SEARCH
Returntheindexofthefirstoccurrenceofsubstring

INSTR('MySql','Sql')=3
INSTR('Sql','MySql')=0
Returnthepositionofthefirstoccurrenceofsubstring

LOCATE('Sql','MySqlSql')=3
LOCATE('xSql','MySql')=0
LOCATE('Sql','MySqlSql',5)=6
POSITION('Sql'IN'MySqlSql')=3
Patternmatchingusingregularexpressions

'abc'RLIKE'[az]+'=1
'123'RLIKE'[az]+'=0
Returnasubstringfromastringbeforethespecified
numberofoccurrencesofthedelimiter

SUBSTRING_INDEX('A:B:C',':',1)='A'
SUBSTRING_INDEX('A:B:C',':',2)='A:B'
SUBSTRING_INDEX('A:B:C',':',2)='B:C'

ASCII('2')=50
ASCII(2)=50
ASCII('dx')=100

CONCAT('My','S','QL')='MySQL'
CONCAT('My',NULL,'QL')=NULL
CONCAT(14.3)='14.3'

Returnthecharacterforeachnumberpassed

Returnconcatenatewithseparator

CHAR(77.3,121,83,81,'76,81.6')='MySQL'
CHAR(45*256+45)=CHAR(45,45)=''
CHARSET(CHAR(X'65'USINGutf8))='utf8'

CONCAT_WS(',','My','Sql')='My,Sql'
CONCAT_WS(',','My',NULL,'Sql')='My,Sql'

Decodeto/fromabase64string

TO_BASE64('abc')='YWJj'
FROM_BASE64('YWJj')='abc'
Convertstringornumbertoitshexadecimalrepresentation

X'616263'='abc'
HEX('abc')=616263
HEX(255)='FF'
CONV(HEX(255),16,10)=255
Converteachpairofhexadecimaldigitstoacharacter

UNHEX('4D7953514C')='MySQL'
UNHEX('GG')=NULL
UNHEX(HEX('abc'))='abc'
Returntheargumentinlowercase

LOWER('MYSQL')='mysql'
LCASE('MYSQL')='mysql'
Loadthenamedfile

SETblob_col=LOAD_FILE('/tmp/picture')
Returnastringcontainingoctalrepresentationofanumber

OCT(12)='14'
Returncharactercodeforleftmostcharacterofthe
argument

ORD('2')=50
EscapetheargumentforuseinanSQLstatement

QUOTE('Don\'t!')='Don\'t!'
QUOTE(NULL)='NULL'
Converttouppercase

UPPER('mysql')='MYSQL'
UCASE('mysql')='MYSQL'

Returnanumberformattedtospecifiednumberofdecimal
places

FORMAT(12332.123456,4)=12,332.1235
FORMAT(12332.1,4)=12,332.1000
FORMAT(12332.2,0)=12332.2
FORMAT(12332.2,2,'de_DE')=12.332,20
Insertasubstringatthespecifiedpositionuptothe
specifiednumberofcharacters

INSERT('12345',3,2,'ABC')='12ABC5'
INSERT('12345',10,2,'ABC')='12345'
INSERT('12345',3,10,'ABC')='12ABC'
Returntheleftmostnumberofcharactersasspecified

LEFT('MySql',2)='My'
Returnthestringargument,leftpaddedwiththespecified
string

LPAD('Sql',2,':)')='Sq'
LPAD('Sql',4,':)')=':Sql'
LPAD('Sql',7,':)')=':):)Sql'
Removeleadingspaces

LTRIM('MySql')='MySql'
Repeatastringthespecifiednumberoftimes

REPEAT('MySQL',3)='MySQLMySQLMySQL'
Replaceoccurrencesofaspecifiedstring

REPLACE('NoSql','No','My')='MySql'
Reversethecharactersinastring

REVERSE('MySql')='lqSyM'
Returnthespecifiedrightmostnumberofcharacters

RIGHT('MySql',3)='Sql'
Returnsthestringargument,rightpaddedwiththe
specifiedstrin.

RPAD('Sql',2,':)')='Sq'
RPAD('Sql',4,':)')='Sql:'
RPAD('Sql',7,':)')='Sql:):)'

Removetrailingspaces

RTRIM('MySql')='MySql'
Returnastringofthespecifiednumberofspaces

SPACE('6')=''
Returnthesubstringasspecified

SUBSTRING=SUBSTR=MID('MySql',3)='Sql'
SUBSTRING=SUBSTR=MID('MySql'FROM4)='ql'
SUBSTRING=SUBSTR=MID('MySql',3,1)='S'
SUBSTRING=SUBSTR=MID('MySql',3)='Sql'
SUBSTRING=SUBSTR=MID('MySql'FROM4FOR2)
='yS'
Removeleadingandtrailingspaces

TRIM('MySql')='MySql'
TRIM(LEADING'x'FROM'xxxSqlMy')='MySql'
TRIM(BOTH'My'FROM'MySqlMy')='Sql'
TRIM(TRAILING'Sql'FROM'MySql')='My'

SETS
Returnstringatindexnumber

ELT(1,'ej','Heja','hej','foo')='ej'
ELT(4,'ej','Heja','hej','foo')='foo'
Returnastringsuchthatforeverybitsetinthevaluebits,
yougetanonstringandforeveryunsetbit,yougetanoff
string

EXPORT_SET(5,'Y','N',',',4)='Y,N,Y,N'
EXPORT_SET(6,'1','0',',',6)='0,1,1,0,0,0'
Returntheindex(position)ofthefirstargumentinthe
subsequentarguments

FIELD('ej','Hj','ej','Heja','hej','oo')=2
FIELD('fo','Hj','ej','Heja','hej','oo')=0
Returntheindexpositionofthefirstargumentwithinthe
secondargument

FIND_IN_SET('b','a,b,c,d')=2
FIND_IN_SET('z','a,b,c,d')=0
FIND_IN_SET('a,','a,b,c,d')=0
Returnasetofcommaseparatedstringsthathavethe
correspondingbitinbitsset

MAKE_SET(1,'a','b','c')='a'
MAKE_SET(1|4,'ab','cd','ef')='ab,ef'
MAKE_SET(1|4,'ab','cd',NULL,'ef')='ab'
MAKE_SET(0,'a','b','c')=''

You might also like