字符串分拆查询

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

原帖地址:http://community.csdn.net/Expert/topic/3230/3230422.xml?temp=.7884485

有这样的数据

字段1     字段2 2,4,23   3,6,345 23,56,4  3,3,67取数据的是查询字段1中条件是4那么在字段2 在取的是6与67结果如下============4    64    67

-------------------------------------------------------------------------------

--处理示例

--测试数据createtabletb(字段1varchar(10),字段2varchar(10))inserttbselect'2,4,23','3,6,345'unionallselect'23,56,4','3,3,67'go

--写个自定义函数来处理createfunctionf_value(@avarchar(10),@bvarchar(10),@cvarchar(10))returnsvarchar(10)asbegin declare@iint,@posint  select@a=left(@a,charindex(','+@c+',',','+@a+',')-1)  ,@pos=len(@a)-len(replace(@a,',',''))+1  ,@i=charindex(',',@b)

 while@i>0and@pos>1  select@b=substring(@b,@i+1,8000)   ,@i=charindex(',',@b)   ,@pos=@pos-1 return(case@poswhen1   thencasewhen@i>0thenleft(@b,@i-1)else@bend   else''end)endgo

--查询declare@avarchar(10)set@a='23' --查询参数

--查询语句selectA=@a,B=dbo.f_value(字段1,字段2,@a)fromtbgo

--删除测试droptabletbdropfunctionf_value

/*--测试结果

A         B         --------------------23        34523        3

(所影响的行数为2行)--*/
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
文章标签: 测试 c
个人分类: 数据库
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭