趣味SQL (求最佳算法)

N_chow 2002-07-10 05:10:02
DECLARE @sItem NVARCHAR(400)
SET @sItem=' Microsoft SQL Server 2000 最 好 的 RDBMS'

其中,若子符之間的連續空格數超過10個(有可能是10個,20個等等不固定)的話,則把這些空格視為是分隔符。
現要求:
把這些子串提取出來,并同時計算子串的個數。
例子的結為:(我用[]來標示)
[ Microsoft SQL Server 2000]
[最 好 的]
[RDBMS]

200分送給最佳算法的同志。謝謝。




...全文
35 96 打赏 收藏 转发到动态 举报
写回复
用AI写文章
96 条回复
切换为时间正序
请发表友善的回复…
发表回复
N_chow 2002-07-20
  • 打赏
  • 举报
回复
结帖先。
OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
一路顺风,周先生!
OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
飘香兄:
如果开头是10个以上空格,应该返回[],如果用REPLACE的方法就要多考虑些了
OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
哇!!!48.9M,哪是补丁,是泥巴~~~~~,明天到单位再打吧
OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
MM,你开头的空格有吗?我是没有打补丁
icevi 2002-07-11
  • 打赏
  • 举报
回复
OpenVMS(半知半解) :你是不是没打补丁?我试是好的了啊。

上面贴错了一点,刚才回了三次,回不进来了。
create table aaa(str varchar(2000) not null)

declare @tmp as varchar(1000)
select @tmp=' Microsoft SQL Server 2000 最 好 的 RDBMS '
select @tmp=ltrim(rtrim(@tmp))
declare @i int
select @i=1

while @i>0
begin
select @i=charindex(replicate(' ',10),@tmp)
if @i>0
begin
insert into aaa values(ltrim(left(@tmp,@i-1)))
select @tmp=ltrim(right(@tmp,len(@tmp)-@i-1))
end
else
begin
insert into aaa values (ltrim(@tmp))
end
end

select '['+str+']' from aaa
N_chow 2002-07-11
  • 打赏
  • 举报
回复
Sorry.沒時間測試了。

明天我就回家了(真想不到這麼順利)
十天後再來結帖吧。

謝謝大家的關註。謝謝。
Nipsan Chow 2002/07/11
N_chow 2002-07-11
  • 打赏
  • 举报
回复
本來,@sItem是一個TEXT類型的字串,可是在測試時,又不可以Declare成TEXT類類型,所以這里是一個VARCHAR(8000)。
這個字串的長度可能會很長,(因為是從前端的一份文本文件傳過來的值)。所以我對執行時間很在意。

N_chow 2002-07-11
  • 打赏
  • 举报
回复
本來,@sItem是一個TEXT類型的字串,可是在測試時,又不可以Declare成TEXT類類型,所以這里是一個VARCHAR(8000)。
這個字串的長度可能會很長,(因為是從前端的一份文本文件傳過來的值)。所以我對執行時間很在意。

OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
Results:

txt
------------------------------------------------------------
[ Microsoft SQL Server 2000]
[最 好 的]
[RDBMS ]

cnt
===========
3



问题是恐怕好多结果不对,如头尾空格的问题


测试办法:(也不一定准,我试过10万条记录查询误差在2ms左右,和系统忙闲有关系,我的测了 1ms,估计这样的小程序,时间差不多,除非你把他换成一个大的TEXT类型看看)
declare @i int
set @i=@@cpu_busy

run ...

select @@cpu_busy-@i as 'time-ms'
Yang_ 2002-07-11
  • 打赏
  • 举报
回复
测试方法:
前面加
SELECT convert(char(30),GetDate(),113)
go

set nocount on

在語句的結束處寫上
go

SELECT convert(char(30),GetDate(),113)
go

要自己算是多少毫秒!!
xzou 2002-07-11
  • 打赏
  • 举报
回复
"頭尾若是有十個以上的空格,則不視為是分隔符,而要把它當作第一個字串及最後一個字串的一部份。"
加条件了?修改一下,在字符串前后多加10个以上的空格测试
DECLARE @sItem NVARCHAR(400),@tmp1 NVARCHAR(400),@space nchar(10),@int int,@count int
begin
set nocount on
set @space = ' '
SET @sItem=' Microsoft SQL Server 2000 最 好 的 RDBMS '
select @tmp1 = @sItem,@count = 0
--加上全空格异常处理
if @sItem = ''
begin
select @sItem
select 1
return
end
while(@sItem <> '' and @sItem is not null)
begin
select @int = charindex(@space,ltrim(rtrim(@sItem)))
if @int > 0
begin
select @int = @int + len(@sItem)-len(ltrim(@sItem))
select @tmp1 = substring(@sItem,@int,400)
select '['+substring(@sItem,1,@int - 1)+']'
select @sItem = ltrim(@tmp1)
select @count = @count + 1
end
else
begin
select '['+@sItem+']'
select @sItem = ''
select @count = @count + 1
end
end
select @count
set nocount off
end
结果:
[ Microsoft SQL Server 2000]
[最 好 的]
[RDBMS ]
3
OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
Results:

txt
------------------------------------------------------------
[ Microsoft SQL Server 2000]
[最 好 的]
[RDBMS ]

cnt
===========
3



问题是恐怕好多结果不对,如头尾空格的问题
OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
Results:

txt
------------------------------------------------------------
[ Microsoft SQL Server 2000]
[最 好 的]
[RDBMS ]

cnt
===========
3



问题是恐怕好多结果不对,如头尾空格的问题
xzou 2002-07-11
  • 打赏
  • 举报
回复
"頭尾若是有十個以上的空格,則不視為是分隔符,而要把它當作第一個字串及最後一個字串的一部份。"
加条件了?修改一下,在字符串前后多加10个以上的空格测试
DECLARE @sItem NVARCHAR(400),@tmp1 NVARCHAR(400),@space nchar(10),@int int,@count int
begin
set nocount on
set @space = ' '
SET @sItem=' Microsoft SQL Server 2000 最 好 的 RDBMS '
select @tmp1 = @sItem,@count = 0
--加上全空格异常处理
if @sItem = ''
begin
select @sItem
select 1
return
end
while(@sItem <> '' and @sItem is not null)
begin
select @int = charindex(@space,ltrim(rtrim(@sItem)))
if @int > 0
begin
select @int = @int + len(@sItem)-len(ltrim(@sItem))
select @tmp1 = substring(@sItem,@int,400)
select '['+substring(@sItem,1,@int - 1)+']'
select @sItem = ltrim(@tmp1)
select @count = @count + 1
end
else
begin
select '['+@sItem+']'
select @sItem = ''
select @count = @count + 1
end
end
select @count
set nocount off
end
结果:
[ Microsoft SQL Server 2000]
[最 好 的]
[RDBMS ]
3

OpenVMS 2002-07-11
  • 打赏
  • 举报
回复
那稍微改一下即可!

SET NOCOUNT ON
create table ##T1 (txt NVarchar(400))
DECLARE @sItem NVARCHAR(400),@tmp nvarchar(30),@x nvarchar(400)
SET @sItem=' Microsoft SQL Server 2000 最 好 的 RDBMS '
set @tmp='%'+REPLICATE(' ',10)+'%'

while PATINDEX(@tmp,@sItem)>0 AND PATINDEX(@tmp,@sItem)<len(@sItem)
BEGIN

SET @x='['+RTRIM(SUBSTRING(@sItem,1,(case when PATINDEX(@tmp,@sItem)=1 then PATINDEX(@tmp,LTRIM(@sItem))+len(@sItem)-len(LTRIM(@sItem)) else PATINDEX(@tmp,@sItem) end)))+']'
INSERT ##T1 VALUES(@x)
SET @sItem=LTRIM(RIGHT(@sItem,len(reverse(@sItem))-len(@x)+2))
END
INSERT ##T1 VALUES('['+@sItem+']')
SELECT txt FROM ##T1 COMPUTE COUNT(txt)
drop table ##t1
gzhughie 2002-07-11
  • 打赏
  • 举报
回复
你可以建立一个跟踪 可以测出很多东西 具体怎么做 说起来很罗索 你可以看SQL 2000的帮助
xzou 2002-07-11
  • 打赏
  • 举报
回复
"頭尾若是有十個以上的空格,則不視為是分隔符,而要把它當作第一個字串及最後一個字串的一部份。"
加条件了?修改一下
DECLARE @sItem NVARCHAR(400),@tmp1 NVARCHAR(400),@space nchar(10),@int int,@count int
begin
set nocount on
set @space = ' '
SET @sItem=' Microsoft SQL Server 2000 最 好 的 RDBMS'
select @tmp1 = @sItem,@count = 0
--加上全空格异常处理
if @sItem = ''
begin
select @sItem
select 1
return
end
while(@sItem <> '' and @sItem is not null)
begin
select @int = charindex(@space,ltrim(rtrim(@sItem)))
if @int > 0
begin
select @int = @int + len(@sItem)-len(ltrim(@sItem))
select @tmp1 = substring(@sItem,@int,400)
select '['+substring(@sItem,1,@int - 1)+']'
select @sItem = ltrim(@tmp1)
select @count = @count + 1
end
else
begin
select '['+@sItem+']'
select @sItem = ''
select @count = @count + 1
end
end
select @count
set nocount off
end
N_chow 2002-07-11
  • 打赏
  • 举报
回复
暈~
這種測試方法好像不行,按鈕JJ的SQL執行時間竟然是0!


gzhughie 2002-07-11
  • 打赏
  • 举报
回复
嘻 侧了一下 好像是0毫秒
加载更多回复(76)

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧