关于SQL语句中的patindex函数使用及PB版本兼容问题

「已注销」 2002-07-12 08:27:46
Update jlupiao set tjs='1' where patindex("%"+tid+"%",:ls_ld)>0

其中 tid 为一字段

ls_id为一变量

如 ls_id="1|2|";

则若 tid="1" 即被更新

这条语句在PB6.5.1+SQL Server2000下我运行成功过

但在 PB8.0+SQL Server2000下行不通了

如果把 tid 换成常量 仍然是正常的

谁遇到过 还请不吝赐教

yeqiufeng
...全文
173 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
人生如梦 2002-07-12
  • 打赏
  • 举报
回复
学习~~
「已注销」 2002-07-12
  • 打赏
  • 举报
回复
最近用PB写程序呵

@_@
linger_liang 2002-07-12
  • 打赏
  • 举报
回复
对了,秋枫大哥您不是一直在bcb里吗?怎么晃到pb来了。。。
linger_liang 2002-07-12
  • 打赏
  • 举报
回复
是阿。sql-server是有这样的问题。
WorldMobile 2002-07-12
  • 打赏
  • 举报
回复
我也遇过这样的问题,就是右边的空格造成的

害的我检索不出数据
myclife 2002-07-12
  • 打赏
  • 举报
回复
:)我瞎蒙了,学习.
myclife 2002-07-12
  • 打赏
  • 举报
回复
:)我瞎蒙了,学习.
pbsql 2002-07-12
  • 打赏
  • 举报
回复
这是char与varchar的区别
「已注销」 2002-07-12
  • 打赏
  • 举报
回复
Update jlupiao set tjs='1' where patindex('%'+rtrim(tid)+'%','7605684|')>0;

我用这句搞定了

可能SQL Server的char的定长空格问题

可能改成varchar应该可以

只是我懒得修改文档了

@_2

它有空格的
wallis 2002-07-12
  • 打赏
  • 举报
回复
没试过帮你推一下
myclife 2002-07-12
  • 打赏
  • 举报
回复
我在用%匹配的时候基本上都用~,特殊字符存放于字符串中使用的,怎么解释呢,就好象C中的转义字符差不多(不太贴切)。
「已注销」 2002-07-12
  • 打赏
  • 举报
回复
~

在这里是什么意思
myclife 2002-07-12
  • 打赏
  • 举报
回复
pattern

一个字符串。可以使用通配符,但 pattern 之前和之后必须有 % 字符(搜索第一个和最后一个字符时除外)。pattern 是短字符数据类型类别的表达式。

改改看:
Update jlupiao set tjs='1' where patindex("~%"+tid+"~%",:ls_ld)>0
sh_yjy 2002-07-12
  • 打赏
  • 举报
回复
我的经验是:
如果是CHAR字段(aa)=(aa )
如果是VARCHAR字段(aa)<>(aa )
leonwan 2002-07-12
  • 打赏
  • 举报
回复
同意楼上意见。
fallstone 2002-07-12
  • 打赏
  • 举报
回复
经常碰到有空格找不到数据。
是不是每个检索条件都要加上TRIM呢?
--计算当前月的实际天数 Create FUNCTION dbo.CalcDaysOfMonth (@time varchar(6)) RETURNS int AS BEGIN DECLARE @Days int DECLARE @Month int DECLARE @Year int SET @Year=SUBSTRING(@time,1,4) SET @Month=SUBSTRING(@time,5,6) if( @Month='1' OR @Month='3' OR @Month='5' OR @Month='7' OR @Month='8' OR @Month='10' OR @Month='12' ) set @Days=31 else if( @Month='4' OR @Month='6' OR @Month='9' OR @Month='11' ) set @Days=30; else if(@Year%400=0 OR (@Year%4=0 AND @Year%1000)) set @Days=29 else set @Days=28 RETURN(@Days) END --确定某年某月有多少天 Create FUNCTION DaysInMonth ( @date datetime ) Returns int AS BEGIN RETURN Day(dateadd(mi,-3,DATEADD(m, DATEDIFF(m,0,@date)+1,0))) END --哪一天是输入时间的星期一 Create FUNCTION MondayInDate ( @date datetime ) RETURNS DATETIME AS BEGIN RETURN DATEADD(week, DATEDIFF(week,0,@date),0) END --输入时间的季度的第一天 Create FUNCTION QuarterInDate ( @date datetime ) RETURNS DATETIME AS BEGIN RETURN DATEADD(quarter, DATEDIFF(quarter,0,@date), 0) END --输入时间的季度的天数 Create FUNCTION QuarterDaysInDate ( @date datetime ) RETURNS INT AS BEGIN declare @m tinyint,@time SMALLDATETIME select @m=month(@date) select @m=case when @m between 1 and 3 then 1 when @m between 4 and 6 then 4 when @m between 7 and 9 then 7 else 10 end select @time=datename(year,@date)+'-'+convert(varchar(10),@m)+'-01' return datediff(day,@time,dateadd(mm,3,@time)) END --按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串存在多少个分隔符号,然后再加一,就是要求的结果。 Create function Get_StrArrayLength ( @str varchar(1024), --要分割的字符串 @split varchar(10) --分隔符号 ) returns int as begin declare @location int declare @start int declare @length int set @str=ltrim(rtrim(@str)) set @location=charindex(@split,@str) set @length=1 while @location0 begin set @start=@location+1 set @location=charindex(@split,@str,@start) set @length=@length+1 end return @length END --按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便 Create function Get_StrArrayStrOfIndex ( @str varchar(1024), --要分割的字符串 @split varchar(10), --分隔符号 @index int --取第几个元素 ) returns varchar(1024) as begin declare @location int declare @start int declare @next int declare @seed int set @str=ltrim(rtrim(@str)) set @start=1 set @next=1 set @seed=len(@split) set @location=charindex(@split,@str) while @location0 and @index>@next begin set @start=@location+@seed set @location=charindex(@split,@str,@start) set @next=@next+1 end if @location =0 select @location =len(@str)+1 --这儿存在两种情况:1、字符串不存在分隔符号 2、字符串存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。 return substring(@str,@start,@location-@start) END select dbo.Get_StrArrayStrOfIndex('8,9,4','',4) --结合上边两个函数,象数组一样遍历字符串的元素 create function f_splitstr(@SourceSql varchar(8000),@StrSeprate varchar(100)) returns @temp table(F1 varchar(100)) as begin declare @ch as varchar(100) set @SourceSql=@SourceSql+@StrSeprate while(@SourceSql'') begin set @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1) insert @temp values(@ch) set @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'') end return end select * from dbo.f_splitstr('1,2,3,4',',') --全角和半角转换函数 Create FUNCTION f_Convert( @str NVARCHAR(4000), --要转换的字符串 @flag bit --转换标志,0转换成半角,1转换成全角 )RETURNS nvarchar(4000) AS BEGIN DECLARE @pat nvarchar(8),@step int,@i int,@spc int IF @flag=0 Select @pat=N'%[!-~]%',@step=-65248, @str=REPLACE(@str,N' ',N' ') ELSE Select @pat=N'%[!-~]%',@step=65248, @str=REPLACE(@str,N' ',N' ') SET @i=PATINDEX(@pat COLLATE LATIN1_GENERAL_BIN,@str) WHILE @i>0 Select @str=REPLACE(@str, SUBSTRING(@str,@i,1), NCHAR(UNICODE(SUBSTRING(@str,@i,1))+@step)) ,@i=PATINDEX(@pat COLLATE LATIN1_GENERAL_BIN,@str) RETURN(@str) END GO declare @s1 varchar(8000) select @s1='  2-3456a78STUVabn国opwxyz' select dbo.f_convert(@s1,0),dbo.f_convert(@s1,1) 函数返回值是表 create table test(id int primary key,name char(10)) insert into test values(1,'test1') insert into test values(2,'test2') insert into test values(3,'test3') insert into test values(4,'test4') 1、标量函数 create function return_count() returns int as begin declare @count int select @count=count(*) from test return @count end --调用 select dbo.return_count() cont --count为显示的列头 --运行结果 --count --4 2、内嵌表值函数 create function return_test() returns table as --begin 内联表值函数不能用begin-end return select name from test --end --调用 select * from return_test() --运行结果 --name --test1 --test2 --test3 --test4 3、多语句表值函数 create function return_test_multi() returns @temp table(id int,name char(10)) as begin insert into @temp select * from test where id in(1,2) return --记住,一定不要忘记写return end --调用 select * from dbo.return_test_multi() --运行结果 --id name --1 test1 --2 test2 在查询结果增加一个自动增长的ID select id=identity(int, 1, 1), * into #T from testTable select * from #T drop table #T sql删除重复的记录 打开测试数据库test,并以表w01为例,将下面的SQL语句放入sql2000查询分析器,一段一段执行即可看到效果 ---在sql2000下创建测试数据表 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[w01]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[w01] ---在sql2005下创建测试数据表,如果是sql2005则用本段来判断数据表是否存在 ---if exists(select 1 from sys.tables where name='w01') ---drop table w01 ----开始创建测试数据库 GO create table w01(gs903 varchar(32),gs1002 varchar(32)) insert into w01 select '1','a' union all select '1','a' union all select '1','a' union all select '2','a' union all select '2','e' union all select '3','b' union all select '3','d' go select * from w01 go ---为表w01添加一个可以表示唯一标示的自增字段ID alter table w01 add [ID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ---查询删除前的数据和记录数:7 select * from w01 select count(*) from w01 ---查询具有重复记录的所有记录;3 select gs903,gs1002,count(*) as count from w01 group by gs903,gs1002 having count(*)>1 order by count desc ---删除重复的数据:2行 delete from w01 where id not in (select max(id) from w01 group by gs903,gs1002) ---看看删除后还有没有重复记录:0 select gs903,gs1002,count(*) as count from w01 group by gs903,gs1002 having count(*)>1 order by count desc ---删除后的数据和记录数:7-2=5 select * from w01 select count(*) from w01 用SQL语句添加删除修改字段 增加字段 alter table docdsp add dspcode char(200) 删除字段 Alter TABLE table_NAME Drop COLUMN column_NAME 修改字段类型 Alter TABLE table_name Alter COLUMN column_name new_data_type 改名 sp_rename 更改当前数据库用户创建对象(如表、列或用户定义数据类型)的名称。 语法 sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name' [ , [ @objtype = ] 'object_type' ] --假设要处理的表名为: tb --判断要添加列的表是否有主键 if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK') begin print '表已经有主键,列只能做为普通列添加' --添加int类型的列,默认值为0 alter table tb add 列名 int default 0 end else begin print '表无主键,添加主键列' --添加int类型的列,默认值为0 alter table tb add 列名 int primary key default 0 end

1,109

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 相关问题讨论
社区管理员
  • 基础类社区
  • WorldMobile
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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