数据库字段选择的问题

来源:百度知道 编辑:UC知道 时间:2024/08/21 17:24:28
表格的数据如下:
music
clothes > man > young man
computer > HP > Notbook
....

现在我只要选择第一个>之前的字段,结果如下:
music
clothes
computer
...
用SQL语句能不能实现? 比如sql="select (第一个>之前的部分) from "?

你的操作需要新建一个split函数,先分解数据,然后输出.代码如下:

-------函数split()
CREATE FUNCTION [dbo].[split]
(@str nvarchar(4000),@code varchar(10),@no int )
RETURNS varchar(200)
AS
BEGIN

declare @intLen int
declare @count int
declare @indexb int
declare @indexe int
set @intLen=len(@code)
set @count=0
set @indexb=1

if @no=0
if charindex(@code,@str,@indexb)<>0
return left(@str,charindex(@code,@str,@indexb)-1)
else
return @str

while charindex(@code,@str,@indexb)<>0
begin
set @count=@count+1
if @count=@no
break
set @indexb=@intLen+charindex(@code,@str,@indexb)
end

if @count=@no
begin

set @indexe=@intLen+charindex(@code,@str,@indexb)
if charindex(@code,@str,@indexe)<>0
return substring(@str,charindex(@code,@str,@indexb)+le