SlideShare a Scribd company logo
Advance SQL
String Manipulation in SQL
Eyal Trabelsi
Agenda
○ Gotha’s
○ Regex
○ String Functions
○ Sentence Splits
○ Aggregations
○ NLP
Advance sql  session - strings
String Gotchas #1
String Gotchas #1
Users
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #1
Users
Calculate and retrieve full name (Concatenation of first name and last name from T2)
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #1
Users
Calculate and retrieve full name (Concatenation of first name and last name from T2)
SELECT first_name + last_name
FROM users
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #1
Users
Calculate and retrieve full name (Concatenation of first name and last name from T2)
SELECT first_name + last_name
FROM users
Is it correct ?
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #1
Users
Calculate and retrieve full name (Concatenation of first name and last name from T2)
SELECT first_name + last_name
FROM users
No it will return null for the first row
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #1
Users
Calculate and retrieve full name (Concatenation of first name and last name from T2)
SELECT ISNULL(first_name,””) + ISNULL(last_name,””)
FROM users
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #1
Users
Calculate and retrieve full name (Concatenation of first name and last name from T2)
SELECT ISNULL(first_name,””) + ISNULL(last_name,””)
FROM users
Is it correct ?
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
Advance sql  session - strings
String Gotchas #2
Users
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #2
Users
Retrieve all the rows with last_name Trabelsi
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #2
Users
Retrieve all the rows with last_name Trabelsi
SELECT *
FROM T1
WHERE last_name =“trabelsi”
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #2
Users
Retrieve all the rows with last_name Trabelsi
SELECT *
FROM Users
WHERE last_name =“trabelsi”
Is it correct ?
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #2
Users
Retrieve all the rows with last_name Trabelsi
SELECT *
FROM T1
WHERE last_name =“trabelsi”
No SQL Strings are case sensitive
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Gotchas #2
Users
Retrieve all the rows with last_name Trabelsi
SELECT *
FROM Users
WHERE LOWER(last_name) =“trabelsi”
Is it correct ?
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
Advance sql  session - strings
Advance sql  session - strings
String Regex Example #1
Users
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Regex Example #1
Users
Retrieve all the rows with valid Id
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Regex Example #1
Users
Retrieve all the rows with valid Id
SELECT *
FROM Users
WHERE id ~ ‘^[0-9]*$’
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Regex Example #1
Users
Retrieve all the rows with valid Id
SELECT *
FROM Users
WHERE id ~ ‘^[0-9]*$’
Is it correct ?
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
Advance sql  session - strings
String Regex Example #2
Users
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Regex Example #2
Users
Retrieve all the rows with valid email
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Regex Example #2
Users
Retrieve all the rows with valid email
SELECT *
FROM Users
WHERE email ~ ^[a-z0-9._%-]+@[a-z0-9.-]+.[a-z]{2,4}$
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
String Regex Example #2
Users
Retrieve all the rows with valid email
SELECT *
FROM Users
WHERE email ~ ^[a-z0-9._%-]+@[a-z0-9.-]+.[a-z]{2,4}$
Is it correct ?
Id FirstName LastName Email
1 NULL Trabelsi eya
invalidId Eyal Trabelsi eyal@gmail.com
Advance sql  session - strings
Advance sql  session - strings
String Regex – Additional Resources
String Regex – Additional Resources
● Regex Curated List
String Regex – Additional Resources
● Regex Curated List
● Redshift additional functionality
Advance sql  session - strings
Supported String functions
Supported String functions
• String function are vendor ‘ dependent!!!!
Supported String functions
• String function are vendor ‘ dependent!!!!
• Most vendors support the all the basic window functions
like lengh, replace, reverse etc.
Supported String functions
• String function are vendor ‘ dependent!!!!
• Most vendors support the all the basic window functions
like lengh, replace, reverse etc.
• These are the vendor specific supported window
functions:
Supported String functions
• String function are vendor ‘ dependent!!!!
• Most vendors support the all the basic window functions
like length, replace, reverse etc.
• These are the vendor specific supported window
functions:
String Function Example #1
Tickets
Id Days_for_resolution Description
1 5 Bad product
2 5 Not working
3 1 The button doesn’t click on IE
4 1 My user is locked after I installed it
String Function Example #1
Tickets
Checking whether ticket description length correlate to days for resolution
Id Days_for_resolution Description
1 5 Bad product
2 5 Not working
3 1 The button doesn’t click on IE
4 1 My user is locked after I installed it
String Function Example #1
Tickets
Checking whether ticket description length correlate to days for resolution
Id Days_for_resolution Description
1 5 Bad product
2 5 Not working
3 1 The button doesn’t click on IE
4 1 My user is locked after I installed it
SELECT AVG(length(description)), days_for_resolution
FROM tickets
GROUP BY days_for_resolution
String Function Example #1
Tickets
Checking whether ticket description length correlate to days for resolution
Is it correct ?
Id Days_for_resolution Description
1 5 Bad product
2 5 Not working
3 1 The button doesn’t click on IE
4 1 My user is locked after I installed it
SELECT AVG(length(description)), days_for_resolution
FROM tickets
GROUP BY days_for_resolution
Advance sql  session - strings
Advance sql  session - strings
Sentence Splitting
Sentence Splitting
https://blog-holistics-io.cdn.ampproject.org/c/s/blog.holistics.io/splitting-
array-string-into-rows-in-amazon-redshift-or-mysql/amp/
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
Split the ticket descriptions into words related to ticket
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
Split the ticket descriptions into words related to ticket
WITH NS AS ( SELECT GENERATE_SERIES(0,10000
AS n)
SELECT SPLIT_PART(description,’ ’, NS.n)
FROM ticket
LEFT JOIN NS
ON NS.n < REGEXP_COUNT((description,’ ’)+1
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
Split the ticket descriptions into words related to ticket
WITH NS AS ( SELECT GENERATE_SERIES(0,10000
AS n)
SELECT SPLIT_PART(description,’ ’, NS.n)
FROM ticket
LEFT JOIN NS
ON NS.n < REGEXP_COUNT((description,’ ’)+1
4
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
Split the ticket descriptions into words related to ticket
WITH NS AS ( SELECT GENERATE_SERIES(0,10000
AS n)
SELECT SPLIT_PART(description,’ ’, NS.n)
FROM ticket
LEFT JOIN NS
ON NS.n < REGEXP_COUNT((description,’ ’)+1
4
NS.n =1: beer
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
Split the ticket descriptions into words related to ticket
WITH NS AS ( SELECT GENERATE_SERIES(0,10000
AS n)
SELECT SPLIT_PART(description,’ ’, NS.n)
FROM ticket
LEFT JOIN NS
ON NS.n < REGEXP_COUNT((description,’ ’)+1
4
NS.n =2: is
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
Split the ticket descriptions into words related to ticket
WITH NS AS ( SELECT GENERATE_SERIES(0,10000
AS n)
SELECT SPLIT_PART(description,’ ’, NS.n)
FROM ticket
LEFT JOIN NS
ON NS.n < REGEXP_COUNT((description,’ ’)+1
4
NS.n =2: very
String Splitting Example #1
Tickets
Id description
1 The beer is very nice
2 The trash cans have small holes
Split the ticket descriptions into words related to ticket
WITH NS AS ( SELECT GENERATE_SERIES(0,10000
AS n)
SELECT SPLIT_PART(description,’ ’, NS.n)
FROM ticket
LEFT JOIN NS
ON NS.n < REGEXP_COUNT((description,’ ’)+1
4
NS.n =4: nice
Advance sql  session - strings
Supported String aggregation functions
Supported String aggregation functions
• String aggregation function are vendor ‘ dependent!!!!
Supported String aggregation functions
• String aggregation function are vendor ‘ dependent!!!!
• Each vendor support different functions and call them in
different way
Supported String aggregation functions
• String aggregation function are vendor ‘ dependent!!!!
• Each vendor support different functions and call them in
different way
• These are the vendor specific supported window functions:
Supported String aggregation functions
• String aggregation function are vendor ‘ dependent!!!!
• Each vendor support different functions and call them in
different way
• These are the vendor specific supported window functions:
String Aggregation Example #1
Accounts
Id Name
1 microsoft
2 amazon
Id Account_id Phone
1 1 050 0000000
2 1 050 0000001
3 2 050 0000002
Contacts
String Aggregation Example #1
Accounts
Id Name
1 microsoft
2 amazon
Id Account_id Phone
1 1 050 0000000
2 1 050 0000001
3 2 050 0000002
Contacts
String Aggregation Example #1
Accounts
Getting all the phone numbers of an account
Id Name
1 microsoft
2 amazon
Id Account_id Phone
1 1 050 0000000
2 1 050 0000001
3 2 050 0000002
Contacts
String Aggregation Example #1
Accounts
Getting all the phone numbers of an account
Id Name
1 microsoft
2 amazon
SELECT LISTAGG(contacts.phone), account.id
FROM accounts
INNER JOIN contacts ON accounts.id = contacts.account_id
GROUP BY account.id
Id Account_id Phone
1 1 050 0000000
2 1 050 0000001
3 2 050 0000002
Contacts
String Aggregation Example #1
Accounts
Getting all the phone numbers of an account
Is it correct ?
Id Name
1 microsoft
2 amazon
SELECT LISTAGG(contacts.phone), account.id
FROM accounts
INNER JOIN contacts ON accounts.id = contacts.account_id
GROUP BY account.id
Id Account_id Phone
1 1 050 0000000
2 1 050 0000001
3 2 050 0000002
Contacts
Advance sql  session - strings
String Aggregation Example #2
String Aggregation Example #2
When we know exactly the number of strings we want to concatenate
String Aggregation Example #2
When we know exactly the number of strings we want to concatenate
https://www.periscopedata.com/blog/aggregating-into-strings-without-string-
agg-or-array-agg-in-amazon-redshift
Advance sql  session - strings
NLP- NPS Calculation
NLP- NPS Calculation
http://www.silota.com/docs/recipes/sql-nps-customer-loyalty-survey-analysis.html
Advance sql  session - strings

More Related Content

What's hot (20)

PPTX
Python Workshop
Assem CHELLI
 
PPTX
Datastructures in python
hydpy
 
PDF
F# for C# Programmers
Scott Wlaschin
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PDF
AmI 2015 - Python basics
Luigi De Russis
 
PDF
AmI 2016 - Python basics
Luigi De Russis
 
PDF
Exploring data models for heterogenous dialect data: the case of e​xplore.bre...
Jack Bowers
 
PPTX
V35 keys-c
Dhirendra Chauhan
 
DOCX
Doc 20191022-wa0041
university of Gujrat, pakistan
 
PDF
Python (regular expression)
Chirag Shetty
 
PDF
Strings in python
Prabhakaran V M
 
PPT
Php String And Regular Expressions
mussawir20
 
PDF
Python - Lecture 7
Ravi Kiran Khareedi
 
PDF
AmI 2017 - Python basics
Luigi De Russis
 
DOCX
Python - Regular Expressions
Mukesh Tekwani
 
PPTX
String in python use of split method
vikram mahendra
 
ODP
Mysql Performance Optimization Indexing Algorithms and Data Structures
Abhijit Mondal
 
PDF
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
PPTX
Finaal application on regular expression
Gagan019
 
PPTX
Dbms chapter v
Bala Ganesh
 
Python Workshop
Assem CHELLI
 
Datastructures in python
hydpy
 
F# for C# Programmers
Scott Wlaschin
 
Regular expressions in Python
Sujith Kumar
 
AmI 2015 - Python basics
Luigi De Russis
 
AmI 2016 - Python basics
Luigi De Russis
 
Exploring data models for heterogenous dialect data: the case of e​xplore.bre...
Jack Bowers
 
V35 keys-c
Dhirendra Chauhan
 
Doc 20191022-wa0041
university of Gujrat, pakistan
 
Python (regular expression)
Chirag Shetty
 
Strings in python
Prabhakaran V M
 
Php String And Regular Expressions
mussawir20
 
Python - Lecture 7
Ravi Kiran Khareedi
 
AmI 2017 - Python basics
Luigi De Russis
 
Python - Regular Expressions
Mukesh Tekwani
 
String in python use of split method
vikram mahendra
 
Mysql Performance Optimization Indexing Algorithms and Data Structures
Abhijit Mondal
 
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Finaal application on regular expression
Gagan019
 
Dbms chapter v
Bala Ganesh
 

Similar to Advance sql session - strings (20)

PDF
DEE 431 Introduction to MySql Slide 6
YOGESH SINGH
 
PPT
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
PPTX
String functions
Kumar Krishnan
 
PPTX
MySQL 5.7 String Functions
Francesco Marino
 
PPTX
String function in my sql
knowledgemart
 
PDF
DP080_Lecture_2 SQL related document.pdf
MinhTran394436
 
PPTX
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
PPTX
SQL Course - QA
pingkapil
 
PPT
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
DOC
75864 sql
bansalaman80
 
PDF
SunshinePHP 2017 - Making the most out of MySQL
Gabriela Ferrara
 
PDF
Database solution by m.moses wills
Moses Mwebaze
 
PDF
Database solution by m.moses wills
Moses Mwebaze
 
PDF
SQL Notes
mitmak
 
PPSX
Regular expressions in oracle
Logan Palanisamy
 
PDF
6-Final MySQL Library Functions.pdf
Surendrasingh211500
 
PDF
Sql wksht-5
Mukesh Tekwani
 
PDF
DPC18 - Making the most out of MySQL
Gabriela Ferrara
 
PPT
SQL select statement and functions
Vikas Gupta
 
PPT
Intro to tsql unit 10
Syed Asrarali
 
DEE 431 Introduction to MySql Slide 6
YOGESH SINGH
 
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
String functions
Kumar Krishnan
 
MySQL 5.7 String Functions
Francesco Marino
 
String function in my sql
knowledgemart
 
DP080_Lecture_2 SQL related document.pdf
MinhTran394436
 
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
SQL Course - QA
pingkapil
 
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
75864 sql
bansalaman80
 
SunshinePHP 2017 - Making the most out of MySQL
Gabriela Ferrara
 
Database solution by m.moses wills
Moses Mwebaze
 
Database solution by m.moses wills
Moses Mwebaze
 
SQL Notes
mitmak
 
Regular expressions in oracle
Logan Palanisamy
 
6-Final MySQL Library Functions.pdf
Surendrasingh211500
 
Sql wksht-5
Mukesh Tekwani
 
DPC18 - Making the most out of MySQL
Gabriela Ferrara
 
SQL select statement and functions
Vikas Gupta
 
Intro to tsql unit 10
Syed Asrarali
 
Ad

More from Eyal Trabelsi (6)

PPTX
Structuring and packaging your python project
Eyal Trabelsi
 
PPTX
Getting to know any dataset
Eyal Trabelsi
 
PPTX
Make Terminal Fun Again
Eyal Trabelsi
 
PPTX
Bring sanity back to sql (advance sql)
Eyal Trabelsi
 
PPTX
Advance sql - window functions patterns and tricks
Eyal Trabelsi
 
PDF
Seminar - Similarity Joins in SQL (performance and semantic joins)
Eyal Trabelsi
 
Structuring and packaging your python project
Eyal Trabelsi
 
Getting to know any dataset
Eyal Trabelsi
 
Make Terminal Fun Again
Eyal Trabelsi
 
Bring sanity back to sql (advance sql)
Eyal Trabelsi
 
Advance sql - window functions patterns and tricks
Eyal Trabelsi
 
Seminar - Similarity Joins in SQL (performance and semantic joins)
Eyal Trabelsi
 
Ad

Recently uploaded (20)

PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Python basic programing language for automation
DanialHabibi2
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 

Advance sql session - strings

  • 1. Advance SQL String Manipulation in SQL Eyal Trabelsi
  • 2. Agenda ○ Gotha’s ○ Regex ○ String Functions ○ Sentence Splits ○ Aggregations ○ NLP
  • 5. String Gotchas #1 Users Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 6. String Gotchas #1 Users Calculate and retrieve full name (Concatenation of first name and last name from T2) Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 7. String Gotchas #1 Users Calculate and retrieve full name (Concatenation of first name and last name from T2) SELECT first_name + last_name FROM users Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 8. String Gotchas #1 Users Calculate and retrieve full name (Concatenation of first name and last name from T2) SELECT first_name + last_name FROM users Is it correct ? Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 9. String Gotchas #1 Users Calculate and retrieve full name (Concatenation of first name and last name from T2) SELECT first_name + last_name FROM users No it will return null for the first row Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 10. String Gotchas #1 Users Calculate and retrieve full name (Concatenation of first name and last name from T2) SELECT ISNULL(first_name,””) + ISNULL(last_name,””) FROM users Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 11. String Gotchas #1 Users Calculate and retrieve full name (Concatenation of first name and last name from T2) SELECT ISNULL(first_name,””) + ISNULL(last_name,””) FROM users Is it correct ? Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 13. String Gotchas #2 Users Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 14. String Gotchas #2 Users Retrieve all the rows with last_name Trabelsi Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 15. String Gotchas #2 Users Retrieve all the rows with last_name Trabelsi SELECT * FROM T1 WHERE last_name =“trabelsi” Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 16. String Gotchas #2 Users Retrieve all the rows with last_name Trabelsi SELECT * FROM Users WHERE last_name =“trabelsi” Is it correct ? Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 17. String Gotchas #2 Users Retrieve all the rows with last_name Trabelsi SELECT * FROM T1 WHERE last_name =“trabelsi” No SQL Strings are case sensitive Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 18. String Gotchas #2 Users Retrieve all the rows with last_name Trabelsi SELECT * FROM Users WHERE LOWER(last_name) =“trabelsi” Is it correct ? Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 21. String Regex Example #1 Users Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 22. String Regex Example #1 Users Retrieve all the rows with valid Id Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 23. String Regex Example #1 Users Retrieve all the rows with valid Id SELECT * FROM Users WHERE id ~ ‘^[0-9]*$’ Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 24. String Regex Example #1 Users Retrieve all the rows with valid Id SELECT * FROM Users WHERE id ~ ‘^[0-9]*$’ Is it correct ? Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 26. String Regex Example #2 Users Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 27. String Regex Example #2 Users Retrieve all the rows with valid email Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 28. String Regex Example #2 Users Retrieve all the rows with valid email SELECT * FROM Users WHERE email ~ ^[a-z0-9._%-]+@[a-z0-9.-]+.[a-z]{2,4}$ Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 29. String Regex Example #2 Users Retrieve all the rows with valid email SELECT * FROM Users WHERE email ~ ^[a-z0-9._%-]+@[a-z0-9.-]+.[a-z]{2,4}$ Is it correct ? Id FirstName LastName Email 1 NULL Trabelsi eya invalidId Eyal Trabelsi [email protected]
  • 32. String Regex – Additional Resources
  • 33. String Regex – Additional Resources ● Regex Curated List
  • 34. String Regex – Additional Resources ● Regex Curated List ● Redshift additional functionality
  • 37. Supported String functions • String function are vendor ‘ dependent!!!!
  • 38. Supported String functions • String function are vendor ‘ dependent!!!! • Most vendors support the all the basic window functions like lengh, replace, reverse etc.
  • 39. Supported String functions • String function are vendor ‘ dependent!!!! • Most vendors support the all the basic window functions like lengh, replace, reverse etc. • These are the vendor specific supported window functions:
  • 40. Supported String functions • String function are vendor ‘ dependent!!!! • Most vendors support the all the basic window functions like length, replace, reverse etc. • These are the vendor specific supported window functions:
  • 41. String Function Example #1 Tickets Id Days_for_resolution Description 1 5 Bad product 2 5 Not working 3 1 The button doesn’t click on IE 4 1 My user is locked after I installed it
  • 42. String Function Example #1 Tickets Checking whether ticket description length correlate to days for resolution Id Days_for_resolution Description 1 5 Bad product 2 5 Not working 3 1 The button doesn’t click on IE 4 1 My user is locked after I installed it
  • 43. String Function Example #1 Tickets Checking whether ticket description length correlate to days for resolution Id Days_for_resolution Description 1 5 Bad product 2 5 Not working 3 1 The button doesn’t click on IE 4 1 My user is locked after I installed it SELECT AVG(length(description)), days_for_resolution FROM tickets GROUP BY days_for_resolution
  • 44. String Function Example #1 Tickets Checking whether ticket description length correlate to days for resolution Is it correct ? Id Days_for_resolution Description 1 5 Bad product 2 5 Not working 3 1 The button doesn’t click on IE 4 1 My user is locked after I installed it SELECT AVG(length(description)), days_for_resolution FROM tickets GROUP BY days_for_resolution
  • 49. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes
  • 50. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes Split the ticket descriptions into words related to ticket
  • 51. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes Split the ticket descriptions into words related to ticket WITH NS AS ( SELECT GENERATE_SERIES(0,10000 AS n) SELECT SPLIT_PART(description,’ ’, NS.n) FROM ticket LEFT JOIN NS ON NS.n < REGEXP_COUNT((description,’ ’)+1
  • 52. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes Split the ticket descriptions into words related to ticket WITH NS AS ( SELECT GENERATE_SERIES(0,10000 AS n) SELECT SPLIT_PART(description,’ ’, NS.n) FROM ticket LEFT JOIN NS ON NS.n < REGEXP_COUNT((description,’ ’)+1 4
  • 53. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes Split the ticket descriptions into words related to ticket WITH NS AS ( SELECT GENERATE_SERIES(0,10000 AS n) SELECT SPLIT_PART(description,’ ’, NS.n) FROM ticket LEFT JOIN NS ON NS.n < REGEXP_COUNT((description,’ ’)+1 4 NS.n =1: beer
  • 54. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes Split the ticket descriptions into words related to ticket WITH NS AS ( SELECT GENERATE_SERIES(0,10000 AS n) SELECT SPLIT_PART(description,’ ’, NS.n) FROM ticket LEFT JOIN NS ON NS.n < REGEXP_COUNT((description,’ ’)+1 4 NS.n =2: is
  • 55. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes Split the ticket descriptions into words related to ticket WITH NS AS ( SELECT GENERATE_SERIES(0,10000 AS n) SELECT SPLIT_PART(description,’ ’, NS.n) FROM ticket LEFT JOIN NS ON NS.n < REGEXP_COUNT((description,’ ’)+1 4 NS.n =2: very
  • 56. String Splitting Example #1 Tickets Id description 1 The beer is very nice 2 The trash cans have small holes Split the ticket descriptions into words related to ticket WITH NS AS ( SELECT GENERATE_SERIES(0,10000 AS n) SELECT SPLIT_PART(description,’ ’, NS.n) FROM ticket LEFT JOIN NS ON NS.n < REGEXP_COUNT((description,’ ’)+1 4 NS.n =4: nice
  • 59. Supported String aggregation functions • String aggregation function are vendor ‘ dependent!!!!
  • 60. Supported String aggregation functions • String aggregation function are vendor ‘ dependent!!!! • Each vendor support different functions and call them in different way
  • 61. Supported String aggregation functions • String aggregation function are vendor ‘ dependent!!!! • Each vendor support different functions and call them in different way • These are the vendor specific supported window functions:
  • 62. Supported String aggregation functions • String aggregation function are vendor ‘ dependent!!!! • Each vendor support different functions and call them in different way • These are the vendor specific supported window functions:
  • 63. String Aggregation Example #1 Accounts Id Name 1 microsoft 2 amazon Id Account_id Phone 1 1 050 0000000 2 1 050 0000001 3 2 050 0000002 Contacts
  • 64. String Aggregation Example #1 Accounts Id Name 1 microsoft 2 amazon Id Account_id Phone 1 1 050 0000000 2 1 050 0000001 3 2 050 0000002 Contacts
  • 65. String Aggregation Example #1 Accounts Getting all the phone numbers of an account Id Name 1 microsoft 2 amazon Id Account_id Phone 1 1 050 0000000 2 1 050 0000001 3 2 050 0000002 Contacts
  • 66. String Aggregation Example #1 Accounts Getting all the phone numbers of an account Id Name 1 microsoft 2 amazon SELECT LISTAGG(contacts.phone), account.id FROM accounts INNER JOIN contacts ON accounts.id = contacts.account_id GROUP BY account.id Id Account_id Phone 1 1 050 0000000 2 1 050 0000001 3 2 050 0000002 Contacts
  • 67. String Aggregation Example #1 Accounts Getting all the phone numbers of an account Is it correct ? Id Name 1 microsoft 2 amazon SELECT LISTAGG(contacts.phone), account.id FROM accounts INNER JOIN contacts ON accounts.id = contacts.account_id GROUP BY account.id Id Account_id Phone 1 1 050 0000000 2 1 050 0000001 3 2 050 0000002 Contacts
  • 70. String Aggregation Example #2 When we know exactly the number of strings we want to concatenate
  • 71. String Aggregation Example #2 When we know exactly the number of strings we want to concatenate https://www.periscopedata.com/blog/aggregating-into-strings-without-string- agg-or-array-agg-in-amazon-redshift

Editor's Notes

  • #59: Redshift limitations: There is limitation on the aggregation and we can only aggregate until max varchar
  • #60: Redshift limitations: There is limitation on the aggregation and we can only aggregate until max varchar
  • #61: Redshift limitations: There is limitation on the aggregation and we can only aggregate until max varchar
  • #62: Redshift limitations: There is limitation on the aggregation and we can only aggregate until max varchar
  • #63: Redshift limitations: There is limitation on the aggregation and we can only aggregate until max varchar
  • #64: Note: we can use it also to Compose less queries Using Dynamic Queries
  • #65: Note: we can use it also to Compose less queries Using Dynamic Queries
  • #66: Note: we can use it also to Compose less queries Using Dynamic Queries
  • #67: Note: we can use it also to Compose less queries Using Dynamic Queries
  • #68: Note: we can use it also to Compose less queries Using Dynamic Queries
  • #74: Most count base text algorithms can be done using SQL pretty easily: Bigram: https://www.periscopedata.com/blog/bigram-frequencies-in-pure-sql
  • #75: Most count base text algorithms can be done using SQL pretty easily: Bigram: https://www.periscopedata.com/blog/bigram-frequencies-in-pure-sql