Whether the query in SQL Server 2005 contains one of the content in the delimiter-separated content _mssql2005

Source: Internet
Author: User

There is a problem with doing project today,

There are product categories A,b,c top-level classification,

There are A1,A2,A3 subcategories below midterm a.

But A1 may belong to both A and B, and then my database is designed like this.

Id Name Parnet
1 A 0
2 B 0
3 A1 1,2

If you want to query all the subclasses of a, check that the parent contains 1, so this is the way. Hehe,

There is no good way for the moment, welcome to make bricks ~ ~

In addition, this thing I want to get back to the value of bool, which brotherhood of hope to help solve ....

Copy Code code as follows:

SET ANSI_NULLS on
Go
SET QUOTED_IDENTIFIER ON
Go
/*
-- =============================================
--Author: < Rice >
--Create Date: <2011-10-19>
--Description: < query delimiter split data contains an item >
-- =============================================
*/
ALTER Function [dbo]. [Haskey] (@DataStr varchar, @key varchar (8))
returns int
As
Begin
DECLARE @return_str varchar (50)
DECLARE @isok bit
DECLARE @posi int
Set @isok = 0
--set @DataStr = ' A,aaaa,aaaddd,ffass '
--set @key = ' aaaa '
Set @DataStr = @DataStr + ', '
While Len (@DataStr) > 0 and @isok =0
Begin
Set @posi = charindex (', ', @DataStr)
Set @return_str = replace (substring (@DataStr, 1,charindex (', ', @DataStr)), ', ', ', '
Set @DataStr = substring (@DataStr, @posi +1,len (@DataStr))
if (@return_str = @key)
Begin
Set @isok =1
Break
End
End
Return @isok
End
Go

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.