UrbanPro

Learn SQL Server from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do you use SQL Server built-in functions PatIndex() and Stuff()?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

PATINDEX() and STUFF() are built-in functions in SQL Server that are commonly used for string manipulation. 1. 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...
read more

PATINDEX() and STUFF() are built-in functions in SQL Server that are commonly used for string manipulation.

1. 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:

sql
PATINDEX('%pattern%', expression)

Example:

sql
-- Find the position of 'world' in the string 'Hello, world!' SELECT PATINDEX('%world%', 'Hello, world!') AS Position;

2. STUFF() Function:

The STUFF() function is used to replace a specified length of characters in a string with another set of characters.

Syntax:

sql
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:

sql
-- 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:

sql
-- 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.

 
 
 
read less
Comments

Related Questions

I need your help to choose my career as I have three years of gap between my education. I know SQL. Can anyone tell me should I choose MS SQL server or oracle because I am interested in the database? Which one will be better to learn to get into the IT industry? Please help me! Thank you!
As you are strong and interested in SQL, why not to choose ETL Testing? It has a good career ahead in data projects. To survive in the market, you need both Oracle & SQL Server. Actually, both are almost same but are different products.
Santhosh
My name is Rajesh , working as a Recruiter from past 6 years and thought to change my career into software (development / admin/ testing ) am seeking for some suggestion which technology I need to learn ? Any job after training ? Or where I can get job within 3 months after finishing my training programme- your advices are highly appreciated
Mr rajesh if you want to enter in to software Choose SAP BW AND SAP HANA because BW and HANA rules the all other erp tools next 50 years.it provides rubust reporting tools for quicker decesion of business It very easy to learn
Rajesh
1 0
6
I need a trainer for SQL-Programming in Tirupur Dist-Tamilnadu I need a trainer who knows to speak in Tamil Preferably..
I am from Hyderabad,I am expert in sql server (TSQL and TSQL Programming),I can teach you that no one teach in India if you are fine with normal English let me know.
Murthy

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

How To Minimize The Page Splits In Sqlserver To Improve The Performane Of Database?
How to minimize the page splits in sqlserver to improve the performane of database? Page Splits: A page is 8Kbytes of data which can be index related, data related, large object binary (lob’s)...

What is database backup? How backup works in SQL Server?
Database Backup in normal terms can be said as a copy of data which can be used in a situation when the things go wrong with your system/server. If your role is IT/Database Administrator, then it is your...

Stuff in SQL Server
SQL Stuff is most commonly use inbuilt function in SQL Server. Stuff basically use for manipulate the string. According to MSDN stuff function insert a string into another string. SQL Stuff function takes...

Cursors In SQL Server
First thing first Usage of Cursors is not encouraged in SQL Server as they are slow. You may go with While loop if you need to iterate through a recordset. Cursor is a database object to retrieve data...

New Features Worth Exploring in SQL Server 2016
New Features Worth Exploring in SQL Server 2016 There is a lot of buzz around SQL Server 2016. Microsoft announced the release of SQL Server 2016 at the Microsoft Ignite Conference during the first...

Recommended Articles

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...

Read full article >

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...

Read full article >

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...

Read full article >

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...

Read full article >

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 you
X

Looking for SQL Server Classes?

The best tutors for SQL Server Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn SQL Server with the Best Tutors

The best Tutors for SQL Server Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more