SQL Text Manipulation Farrokh Alemi, Ph.D.

Slides:



Advertisements
Similar presentations
Manipulating Strings String Functions. VB provides a large number of functions that facilitate working with strings. These are found in Microsoft.VisualBasic.Strings.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
1 Pertemuan 04 Expression Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Group practice in problem design and problem solving
Chapter 5 new The Do…Loop Statement
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Manipulation Masterclass By the VB Gods. In this masterclass, we will learn how to use some of the string manipulation function such as Len, Right, Left,
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
 Agenda: 4/24/13 o External Data o Discuss data manipulation tools and functions o Discuss data import and linking in Excel o Sorting Data o Date and.
Copyright © 2008 Pearson Prentice Hall. All rights reserved Chapter 6 Data Tables and Amortization Tables Exploring Microsoft Office Excel 2007.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
String Manipulation. Strings have their own properties and methods, just like a textbox or label or form does.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
There’s a particular style to it… Rob Hatton
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
EET 2259 Unit 13 Strings and File I/O
SPSS For a Beginner CHAR By Adebisi A. Abdullateef
Microsoft Visual Basic 2008: Reloaded Third Edition
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
String Methods Programming Guides.
Chapter 6 JavaScript: Introduction to Scripting
COMPUTATIONAL CONSTRUCTS
Open Source Server Side Scripting MySQL Functions
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Built-in Functions. Usage of Wildcards
Introduction to Scripting
Chapter 5 The Do…Loop Statement
Types of Joins Farrokh Alemi, Ph.D.
SELECT & FROM Commands Farrokh Alemi, PhD
Rank Order Function Farrokh Alemi, Ph.D.
Date Functions Farrokh Alemi, Ph.D.
Creating Tables & Inserting Values Using SQL
Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi
String and Lists Dr. José M. Reyes Álamo.
Cursors Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi
Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Introduction to Computer Science
Inside Module 8 Extracting Data Page Using the Extract command 2
MapInfo SQL String Functions
EET 2259 Unit 13 Strings and File I/O
Lecture 5 SQL FUNCTIONS.
Trainer: Bach Ngoc Toan– TEDU Website:
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

SQL Text Manipulation Farrokh Alemi, Ph.D. In this section we discuss several text manipulation functions in SQL. This brief presentation was organized by Dr. Alemi.

Medical & Nursing Notes Cross Join Medical & Nursing Notes Dose in Name of Meds Text Is Common Many fields in electronic health records contain free text. For example, medical and nursing notes are typically entered as open text. For another example, name of medications are typically presented as text fields. If we want to report the dose of the medication, one may need to write a code to analyze the name and extract the dose from within the name. Analysis and manipulation of free text is important part of SQL.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join There are many functions available.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join The charindex and the pathindex functions return the location of substring or a pattern in a string

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join Concat or the simple use of plus sign attach two or more strings together. Concat is one of the functions that we will describe at some length through this set of slides.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join Left and right functions extract a substring, starting from left or right.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join LEN and Data length functions return the length of the specified string. Data length returns it in bytes.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join Lower and upper functions change a string to lower or upper case so it is easier to read.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join L and R trim functions remove leading or trailing spaces from a string. Space adds it in.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join Replace function replaces a sequence of charact4ers in a stroing with another set of characters. Substring function extracts a substring from a string.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join Stuff function replaces a sequence of characters with another, starting at a specified position.

Cross Join Function Description CHARINDEX or PATINDEX Returns the location of a substring or a pattern in a string CONCAT or + Concatenates two or more strings together LEFT, RIGHT Extracts a substring from a string, starting from left or right LEN or DATALENGTH Returns the length of the specified string or expression LOWER or UPPER Converts a string to lower- or upper-case LTRIM or RTRIM Removes leading or trailing spaces from a string REPLACE Replaces a sequence of characters in a string with another set of characters SPACE Returns a string with a specified number of spaces STUFF Replaces a sequence of characters with another, starting at a specified position SUBSTRING Extracts a substring from a string Cross Join In these slides we will focus on concat and stuff functions. All the remaining functions’ syntax can be found on the web.

Start CONCAT Finish Cross Join We begin with concat function. This function joins one or more strings so that end of one is the beginning of another. Think of it as a relay run with each run segment being a string. You may also think of it as a way of adding text to each other.

CONCAT CONCAT(string1, string2, ...., string_n) Cross Join The syntax of the concat() function starts with the reserve word concat.

CONCAT CONCAT(string1, string2, ...., string_n) Cross Join The parameters of the concat() function are specified in parentheses as columns of string separated by commas.

CONCAT CONCAT(string1, string2, ...., string_n) Cross Join CONCAT(string1, string2, ...., string_n) string1 + string2 + string_n CONCAT Alternatively, one could simply write the strings and put a plus sign between them. Note that all fields must be text, numbers must be converted to text.

CONCAT Sex Unable to Eat Unable to Sit M 1 F Cross Join , CASE WHEN Gender='M' THEN '1' ELSE '0' END + [Unable to Eat] + [Unable to Sit] AS All Cross Join Sex Unable to Eat Unable to Sit M 1 F CONCAT Here is an example. In this code we want to attach three fields together. Each field is a binary text variable containing 1 or 0. The first field contains “m” for male and “f” for female.

CONCAT Sex Unable to Eat Unable to Sit M 1 F Cross Join , CASE WHEN Gender='M' THEN '1' ELSE '0' END + [Unable to Eat] + [Unable to Sit] AS All Cross Join Sex Unable to Eat Unable to Sit M 1 F CONCAT We use the case function to replace these values with text of 1 or 0. The second field is inability to eat. It contains a text binary field. The third field is inability to sit, this too contains a text binary field.

CONCAT Sex Unable to Eat Unable to Sit M 1 F Cross Join , CASE WHEN Gender='M' THEN '1' ELSE '0' END + [Unable to Eat] + [Unable to Sit] AS All Cross Join Sex Unable to Eat Unable to Sit M 1 F CONCAT The plus sign instructs the computer to attach the two fields separated by the plus sign to each other.

CONCAT Sex Unable to Eat Unable to Sit All M 1 111 110 101 100 F 011 , CASE WHEN Gender='M' THEN '1' ELSE '0' END + [Unable to Eat] + [Unable to Sit] AS All Cross Join Sex Unable to Eat Unable to Sit All M 1 111 110 101 100 F 011 010 001 000 CONCAT Now we have a new column of data with three 0 or 1 text-entry, each indicating if the patient is male, if the patient is unable to eat, or the patient is unable to sit. This concatenated new field contains the information in all the three variables and thus may be easier to process. For example, using GROUP BY all variable will have the same result as grouping on all three variables separately. Concat function has joined the values of three fields into one field, each starting where the other one left off.

Delete Insert Stuff Function Cross Join Stuff function is also useful and has a more complicated syntax. The STUFF() function first deletes a sequence of characters of certain length and then inserts another sequence of characters into the string, starting at the start of deletion. The deleted length and inserted string do not need to be of the same length.

Stuff Function STUFF(string1, start, length, add_string) The syntax of the stuff() function is shown in this slide. All function arguments are required and must be specified before stuff works.

Stuff Function STUFF(string1, start, length, add_string) The stuff function starts with the reserve word stuff and inside parentheses it provides the function arguments.

Stuff Function STUFF(string1, start, length, add_string) The first entry is the field where we want to make the change. This field must be a text field.

Stuff Function STUFF(string1, start, length, add_string) The second argument is an integer or an expression that produces an integer. The integer indicates where in the string field we would like to make the change.

Stuff Function STUFF(string1, start, length, add_string) The third parameter of the function is the length of characters that we want to delete.

Stuff Function STUFF(string1, start, length, add_string) The last parameter in the stuff() function is a string that we wish to insert at the starting of previous deletion.

, Stuff('I am happy', 5, 1, ' not ') Stuff Function STUFF(string1, start, length, add_string) , Stuff('I am happy', 5, 1, ' not ') Stuff Function Here is an example. We start with the string “I am happy”.

, Stuff('I am happy', 5, 1, ' not ') Stuff Function STUFF(string1, start, length, add_string) , Stuff('I am happy', 5, 1, ' not ') Stuff Function The 5th character is the space between am and happy.

, Stuff('I am happy', 5, 1, ' not ') Stuff Function STUFF(string1, start, length, add_string) , Stuff('I am happy', 5, 1, ' not ') I amhappy Stuff Function We want to delete 1 character starting with the space. So essentially we are just deleting the space between “am” and “happy”

, Stuff('I am happy', 5, 1, ' not ') Stuff Function STUFF(string1, start, length, add_string) , Stuff('I am happy', 5, 1, ' not ') I am not happy Stuff Function In the last parameter, we specify what should be inserted starting at character 5. Instead of the deleted space, we insert the new string “not”, with leading and trailing spaces. We have now changed “I am happy” to “I am not happy.”

, Stuff(All, 2, 1, ‘-') Stuff Function STUFF(string1, start, length, add_string) , Stuff(All, 2, 1, ‘-') Stuff Function Here is another example. Here we are instructing the computer to replace the 2nd character in the string variable All with a dash. Recall that the all variable was a string variable containing binary indications of whether the patient was male, unable to eat or unable to sit.

, Stuff(All, 2, 1, ‘-') Stuff Function All Before 111 110 101 100 011 010 001 000 Stuff Function The stuff command tells us to replace the string starting at 2nd character and 1 character long with the dash

, Stuff(All, 2, 1, ‘-') Stuff Function All Before All After 111 1-1 110 1-0 101 100 011 0-1 010 0-0 001 000 Stuff Function If you recall, the second character was about the patient being unable to eat. We have now eliminated this information from the variable called “all.”

Cross Join Common Problems Stuff is a difficult function to work with as you need to know exactly which character in the field should be manipulated. You need to know the length of the string one must delete. You also need the exact text of the strings that should be stuffed into the field. But once you know this information, you can manipulate sentences and words within them. You can cut out a word and insert another phrase.

Concat & Stuff Functions are Needed in analysis of TEXT data from EHR Concat & Stuff Functions are needed in analysis of text data from electronic health records.