Excel to SQL · Lesson 11 of 12

Text functions: LEFT, CONCAT, UPPER, FIND

In Excel, you'd reach for
fx=LEFT(), =MID(), =LEN(), =UPPER(), =A2&" "&B2, =SUBSTITUTE()

Most Excel text formulas have a SQL twin. The names on this site follow SQLite, which is what runs in your browser:

  • substr(text, start, length) is MID (and LEFT when start is 1)
  • length(text) is LEN
  • upper() and lower() are UPPER and LOWER
  • a || ' ' || b joins text, like & in Excel
  • replace(text, 'old', 'new') is SUBSTITUTE
  • instr(text, '@') is FIND
Try itEdit the query, then Run
nameshoutingemail_domainlabel
Kavya HaddadKAVYA HADDADexample.comKavya Haddad (United States)
Ben KimBEN KIMexample.comBen Kim (India)
Kavya KimKAVYA KIMexample.comKavya Kim (India)
Nikhil TanakaNIKHIL TANAKAexample.comNikhil Tanaka (Nigeria)
Elena NguyenELENA NGUYENexample.comElena Nguyen (United Kingdom)
Leo GarciaLEO GARCIAexample.comLeo Garcia (Nigeria)
Tara AliTARA ALIexample.comTara Ali (Germany)
Quinn LopezQUINN LOPEZexample.comQuinn Lopez (United States)
Sam OkaforSAM OKAFORexample.comSam Okafor (Brazil)
Ben SmithBEN SMITHexample.comBen Smith (United States)

10 rows

Other databases spell a few of these differently (Postgres has left() and position(), SQL Server uses + to join text), but the ideas are identical.

Your turn

Show each customer's name and the first name only, as first_name (everything before the space).

This page on one card

Text functions: LEFT, CONCAT, UPPER, FIND: the Excel way (=LEFT()) and the SQL query, side by side
Text functions: LEFT, CONCAT, UPPER, FIND, on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows