EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

SQL Stuff

By Payal UdhaniPayal Udhani

Home » Data Science » Data Science Tutorials » SQL Tutorial » SQL Stuff

SQL Stuff

Introduction to SQL Stuff

SQL stuff is the function available in SQL to manipulate the strings and concatenate one string to another at the position where we want and even modifying the previous string by deleting some of the characters from the original string and then placing the other string in the main string. When dealing with string manipulations in SQL we often face the situation when we have to remove some of the parts from the original string and replace it with some other string value by specifying the place where we want to add the second string and mentioning part of original string that is too be removed. All these operations in the SQL can be done by using a single method named Stuff().

In this article, we will learn about syntax, general usage of stuff function and also learn how we can implement it while manipulating string values in SQL with the help of some of the examples.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

The syntax of stuff() function in SQL is as shown below –

STUFF(original string, starting point, length, attached string)

In the above syntax, the original string is any string literal value of the name of the column whose database is character-based such as VARCHAR or string-related which stores string value and the starting point is the position in the original string that is to be considered as the beginning point for removal of characters from the original string while the length helps us specify the number of the characters that are to be removed from the original string. The attached string is the new string that is to be appended at the position from where the characters in the original string were modified.

The new string is placed at the position where the characters from the original strings are removed. One of the key things that are to be noted here is that the number of the characters that are to be deleted and the number of the characters that are to be added that are present in the new string to be attached are not compulsorily to be same.

Examples of SQL Stuff

Let us consider a few examples where we will remove some of the characters from the original string and place the new string at the position from where the characters were removed. Firstly, we will consider a string literal value say “EDUCBA_CONSULTANCY.com is the best place to learn new things.” in which we want to remove the part that includes _consultancy.com and place a new word named platform at that place. For this, we will have to mention the start position as 7 and the number of characters to be removed as 16 because the string “_CONSULTANCY.com” contains 16 characters in it. We will use stuff() function and our query statement will be somewhat as shown below –

Popular Course in this category
JDBC Training (6 Courses, 7+ Projects)6 Online Courses | 7 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,044 ratings)
Course Price

View Course

Related Courses
PHP Training (5 Courses, 3 Project)Windows 10 Training (4 Courses, 4+ Projects)SQL Training Program (7 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)Oracle Training (14 Courses, 8+ Projects)

SELECT STUFF('EDUCBA_CONSULTANCY.com is the best place to learn new things.', 7, 16, ' Platform ');

The execution of the above query statement gives an output which is as shown below –

SQL Stuff-1.1

We can observe the new string that is prepared contains platform word after EDUCBA and the final string formed is “EDUCBA Platform is the best place to learn new things.

Now, we will consider one more example in which we will replace the deleted characters with string that will contain more than one words. Consider a string “To travel is to discover yourself and retreat the gift of life provided by god to you”. In this string we want to remove the characters “provided by god to you” and place a new string containing more than one word say “at fullest”.

The number of characters to be removed i.e “provided by god to you” are located at 65 position and the number of the characters to be removed are 23 because this string contains 23 characters in it. The final query string for doing so will be as follows –

SELECT STUFF('To travel is to discover yourself and retreat the gift of life provided by god to you', 65, 23, ' at fullest.');

The execution of the above query statement gives an output which is as shown below –

SQL Stuff-1.2

Now, we will learn how we can use stuff() function with column values stored in the tables by using it in the SQL query statements. Consider and existing table named dictionary whose contents and structure are as shown in the output of the following query statement –

SELECT * FROM dictionary;

The execution of the above query statement gives an output which is as shown below –

SQL Stuff-1.3

Now, we have to retrieve the value of the meaning of word named lexicography whose meaning string needs to replaced while retrieving such that instead of “the job or skill of writing dictionaries.” it will retrieve “the job or skill of writing articles.”. We can simply use the stuff function to do so by placing the start position of the characters to be removed as 29 because the dictionaries word is located at that position and the number of characters as 12 because it contains 12 characters in it. Our query statement will be as shown below –

SELECT STUFF(meaning, 29, 12, ' articles.') FROM dictionary WHERE word = 'lexicography';

The execution of the above query statement gives an output which is as shown below –

Output-1.4

We can make the use of the function named CHRINDEX() to locate the position of particular string inside the main string by specifying the string whose position is to be found out at first position and the string value in which the search is to be made as its second position. Consider the same example in which we want to find out the position of dictionaries in the string “the job or skill of writing dictionaries” as and place this in the second parameter of the stuff method. All this things can be combined in a single query statement using following query –

SELECT STUFF('the job or skill of writing dictionaries', CHARINDEX('dictionaries','the job or skill of writing dictionaries'), 12, 'articles.');

The execution of the above query statement gives an output which is as shown below –

Output-1.5

Note that instead of string literal column name can also be mentioned by mentioning the name of the table from which data is to be retrieved as shown in the example before the current one.

Recommended Articles

This is a guide to SQL Stuff. Here we also discuss the introduction and syntax of SQL Stuff along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. SQL Compare String
  2. SQL UPDATE Trigger
  3. SQL DELETE
  4. PostgreSQL SET

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • SQL Window Functions
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • ANY in SQL
    • LIKE Query in SQL
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL MOD()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Server Permission
    • T-SQL INSERT
    • SQL Ranking Function
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
    • SQL Super Key
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • SQL Formatter
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL INSTR()
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - JDBC Training Course Learn More