Learn SQL Server from the Best Tutors
Search in
PATINDEX()
and STUFF()
are built-in functions in SQL Server that are commonly used for string manipulation.
PATINDEX()
Function:The PATINDEX()
function is used to find the starting position of a pattern in a string. It returns the starting position of the first occurrence of a pattern in a specified expression.
Syntax:
PATINDEX('%pattern%', expression)
Example:
-- Find the position of 'world' in the string 'Hello, world!' SELECT PATINDEX('%world%', 'Hello, world!') AS Position;
STUFF()
Function:The STUFF()
function is used to replace a specified length of characters in a string with another set of characters.
Syntax:
STUFF(expression, start, length, replacement)
expression
: The original string.start
: The starting position for the replacement.length
: The number of characters to replace.replacement
: The new set of characters to insert into the string.Example:
-- Replace 'apple' with 'orange' starting from position 7 and replacing 5 characters SELECT STUFF('I like apple pie', 7, 5, 'orange') AS Result;
These functions can be used together in various scenarios. For example, you might use PATINDEX()
to find the position of a substring in a column and then use STUFF()
to replace or manipulate the string based on that position.
Here's a combined example:
-- Replace the first occurrence of 'fox' with 'dog' in a column named 'Description' UPDATE YourTable SET Description = STUFF(Description, PATINDEX('%fox%', Description), 3, 'dog') WHERE PATINDEX('%fox%', Description) > 0;
This example updates the 'Description' column by replacing the first occurrence of 'fox' with 'dog' where 'fox' is found. Note that PATINDEX()
is used to find the position of 'fox' in the string, and STUFF()
is used to replace the substring.
Related Questions
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Why Should you Learn Microsoft Office
Microsoft Office is a very popular tool amongst students and C-Suite. Today, approximately 1.2 billion people across 140 countries use the office programme. It is used at home, schools and offices on a daily basis for organizing, handling and presenting data and information. Microsoft Office Suite offers programs that can...
Make a Career as a BPO Professional
Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...
Top 5 Skills Every Software Developer Must have
Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today. In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...
8 Hottest IT Careers of 2014!
Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...
Looking for SQL Server Training?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for SQL Server Classes are on UrbanPro
The best Tutors for SQL Server Classes are on UrbanPro