Determining where strings appear and how to intercept strings in SQL Server

Source: Internet
Author: User

First build a test sheet:

Create Table nvarchar (+)); Insert  into teststring Values (' Zhang San, John Doe, Harry, Caifan, Xiao Rambler, royal palace ');

1. Determine the number of occurrences of a character (string) in a string, the position where the first occurrence occurs last:

View ', ' The number of occurrences

Select LEN (name) - LEN (REPLACE(name,',', ' from teststring;

View ', ' the first occurrence of the location:

Select CHARINDEX (',' from teststring;

View ', ' last seen location:

Select Len (name) - (CHARINDEX(',',REVERSE(name))-1 from teststring;

2. Remove the character (string) separated by ', ', in this case, ' Zhang San ', ' John Doe ', etc.

This is a regular, first of all should think of there is no specific system function implementation, as if no, second should think of loops.

When I take ' Zhang San ' out, how do you remove ' Zhang San '?

It's not hard to take ' Zhang San '

Select SUBSTRING (Name,1,CHARINDEX(',', name)-1 from teststring;

How to get rid of ' Zhang San '? At first I do not know, Baidu, Google ah, the keyword SQL, split bar, anyway, I also search

Select STUFF (Name,1,CHARINDEX(',', name),'from teststring;

It seems to be possible with replace.

Select REPLACE (Name,substring(name,1,CHARINDEX(',', name)),"  from TestString;

That is now the cycle, I seldom write stored procedures or functions, grammar do not remember, when used to check (often used to remember), I write anonymous stored procedures, haha

Create TableTeststring2 (namenvarchar( -));Declare @name nvarchar( -);Select @name=Name fromteststring; while(CHARINDEX(',',@name)<>0)beginInsert  intoTeststring2Select SUBSTRING(@name,1,CHARINDEX(',',@name)-1);Set @name=STUFF(@name,1,CHARINDEX(',',@name),"');Set @i=@i+1;EndInsert  intoteststring2Select @name

The SELECT * from Teststring2 can be seen.

This is only one field in the table, if more, use the table variable should be.

The following example:

  --when the product name contains parentheses, the bracketed content is truncated from the product name  SELECTProduct_code, Case  when CHARINDEX('(', Product_Name)!= 0  Then       substring(Product_Name,CHARINDEX('(', Product_Name)+1,CHARINDEX(')', Product_Name)-CHARINDEX('(', Product_Name)-1)        End  asPcode, Product_Name fromProduct

Determining where strings appear and how to intercept strings in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.