Strings

Introduction to SQL Server

John MacKintosh

Instructor

SELECT 
  description, 
  LEN(description) AS description_length 
FROM grid;
+----------------+-----------------------------------+
| description                   | description_length | 
|-------------------------------+--------------------|
| Severe Weather  Thunderstorms | 29                 |
| Severe Weather  Thunderstorms | 29                 |
| Severe Weather  Thunderstorms | 29                 |
| Fuel Supply Emergency  Coal   | 27                 |
| Physical Attack  Vandalism    | 26                 |
+----------------+-----------------------------------+
Introduction to SQL Server
SELECT 
  description, 
  LEFT(description, 20) AS first_20_left 
FROM grid;
+-------------------------------+-----------------------+
| description                   | first_20_left         | 
|-------------------------------+-----------------------|
| Severe Weather  Thunderstorms | Severe Weather  Thun  |
| Severe Weather  Thunderstorms | Severe Weather  Thun  |
| Severe Weather  Thunderstorms | Severe Weather  Thun  |
| Fuel Supply Emergency  Coal   | Fuel Supply Emergenc  |
| Physical Attack  Vandalism    | Physical Attack  Van  |
+-------------------------------+-----------------------+
Introduction to SQL Server
SELECT 
  description, 
  RIGHT(description, 20) AS last_20 
FROM grid;
+-------------------------------+----------------------+
| description                   | last_20              | 
|-------------------------------+----------------------|
| Severe Weather  Thunderstorms | ather  Thunderstorms |
| Severe Weather  Thunderstorms | ather  Thunderstorms |
| Severe Weather  Thunderstorms | ather  Thunderstorms |
| Fuel Supply Emergency  Coal   | pply Emergency  Coal |
| Physical Attack  Vandalism    | al Attack  Vandalism |
+-------------------------------+----------------------+
Introduction to SQL Server
SELECT 
  CHARINDEX ('_', url) AS char_location, 
  url 
FROM courses;
+---------------+-------------------------------------+
| char_location | url                                 | 
|---------------+-------------------------------------|
| 34            | datacamp.com/courses/introduction_  |
| 34            | datacamp.com/courses/intermediate_  |
| 29            | datacamp.com/courses/writing_       |
| 29            | datacamp.com/courses/joining_       |
| 27            | datacamp.com/courses/intro_         |
+---------------+-------------------------------------+
Introduction to SQL Server
SELECT 
  SUBSTRING(url, 12, 12) AS target_section, 
  url 
FROM courses;
+------------------+----------------------------------+
| target_section   | url                              |
+------------------+----------------------------------+
| datacamp.com     |https//www.datacamp.com/courses   |
+------------------+----------------------------------+
Introduction to SQL Server

REPLACE

SELECT 
  TOP(5) REPLACE(url, '_', '-') AS replace_with_hyphen 
FROM courses;
+-------------------------------------+
| replace_with_hyphen                 | 
+-------------------------------------|
| datacamp.com/courses/introduction-  |
| datacamp.com/courses/intermediate-  |
| datacamp.com/courses/writing-       |
| datacamp.com/courses/joining-       |
| datacamp.com/courses/intro-         |
+-------------------------------------+
Introduction to SQL Server

Let's practice!

Introduction to SQL Server

Preparing Video For Download...