SQL checks whether two strings have the same content

Source: Internet
Author: User
Tags rtrim

 

-- ===================================================== ====== -- Author: wuchaorang -- Create date: 2010-12-22 -- Description: determines whether the two strings have the same content (find whether the same string exists in @ newSplit) -- Debug: select dbo. fun_charindex ('a, B, c, D', 'd, ',') or select dbo. fun_charindex ('a, B, C', 'B,') -- Return: 1 is returned, otherwise, 0 is returned -- ======================================================== =========== ALTER function fun_charindex (@ orginSplit varchar (4000 ), -- original value @ newSplit varchar (4000), -- Value of the separator @ speaterString varchar (20) -- separator) returns smallintasbegin declare @ val smallint, @ I int, @ helpSplit varchar (4000) set @ val = 0 set @ newSplit = rtrim (ltrim (@ newSplit) set @ speaterString = rtrim (ltrim (@ speaterString )) set @ orginSplit = rtrim (ltrim (@ orginSplit) if left (@ orginSplit, len (@ speaterString )) <> @ speaterString set @ orginSplit = @ speaterString + @ orginSplit if right (@ orginSplit, len (@ speaterString )) <> @ speaterString set @ orginSplit = @ orginSplit + @ speaterStringif @ newSplit = ''return @ val if charindex (@ speaterString, @ newSplit) = 0 begin -- or change to set @ helpSplit = replace (@ orginSplit, @ speaterString + @ newSplit + @ speaterString, '') -- if len (@ speaterString + @ newSplit + @ speaterString) + len (@ helpSplit) = len (@ orginSplit) set @ helpSplit = @ speaterString + @ newSplit + @ speaterStringif patindex ('%' + @ helpSplit + '% ', @ orginSplit)> 0 set @ val = 1 end else begin if right (@ newSplit, len (@ speaterString )) <> @ speaterString set @ newSplit = @ newSplit + @ speaterStringset @ I = charindex (@ speaterString, @ newSplit) while @ I> 0 begin if rtrim (ltrim (left (@ newSplit, @ i-1) = ''begin set @ newSplit = SUBSTRING (@ newSplit, @ I + 1, LEN (@ newSplit)-@ I) set @ I = CHARINDEX (@ speaterString, @ newSplit) continueend set @ helpSplit = @ speaterString + rtrim (ltrim (left (@ newSplit, @ i-1 ))) + @ speaterString -- or change to len (@ helpSplit) + len (replace (@ orginSplit, @ helpSplit, '') = len (@ orginSplit) if patindex ('%' + @ helpSplit + '%', @ orginSplit)> 0 begin set @ val = 1 breakend set @ newSplit = SUBSTRING (@ newSplit, @ I + 1, LEN (@ newSplit)-@ I) set @ I = CHARINDEX (@ speaterString, @ newSplit) end endreturn @ valend instance: select dbo. fun_charindex ('aa, bb, cc, dd', 'B, B, cct',', ') returns 1; otherwise, 0 is returned.

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.