当前位置:首页 » 编程语言 » sql如何设置字符排序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql如何设置字符排序

发布时间: 2022-07-27 01:03:23

1. sql按关键字符排序问题,请高手帮解答一下如何按特定字符排序

笨办法:
select *(
select * ,type 1 from A where (查出含北京
)
union
select * ,type 2 from A where (查出不含北京
)
) order by type

2. SQL如何写个函数将字符串按单字符进行排序

看到你这个需求,我的第一反应就是函数要递归,如果你这个字符串长度很长,再递归的时候数据库就呵呵了, 在这里很想问一句,数据库主要职责还是存储数据,你这个字符串排序完全可以在后台程序中处理干嘛非要使用数据库函数呢? 这就一点类似加密解密了,这些全都应该是后台程序完成的呀.

3. 如何用SQL语句修改字段的排序规则

SQL排序子句的语法是:ORDER
BY
{column_name
[ASC|DESC]}
[,…n]
大括号{}的内容表示是必有的内容(这里应该是你提问的内容)
中括号[]表示的是可选的内容
连接符|连接的是任意有一个的内容
例如:order
by
seq_id;--seq_id假设是表中的序号字段,这样是缺省按asc顺序排序
order
by
seq_id
desc;--显式指定排序的方式,desc降序排序
order
by
1;--按输出结果集的第一个字段,缺省按asc顺序排序
order
by
1,3
desc,5;--按输出结果集的第一个字段,缺省按asc顺序排序;第3个字段,显式明确按降序排序;第5个字段,缺省按asc顺序排序

4. SQL如何按指定字符在字符串中的位置来排序

CreateTableT
(
idint,
StVarchar(100)
)

InsertIntoTValues(1,'魂牵梦萦复方丹参a草叶魂牵梦萦')
InsertIntoTValues(2,'魂牵梦萦复方丹参草叶a魂牵梦萦')
InsertIntoTValues(3,'魂牵梦萦复方丹参草叶b魂牵梦萦')
InsertIntoTValues(4,'魂牵梦萦复方丹参b草叶魂牵梦萦')
InsertIntoTValues(5,'魂牵梦萦复方丹参草叶魂牵abc梦萦')
InsertIntoTValues(6,'魂牵梦萦复方丹参草叶什么都没有魂牵梦萦')

--先按是否包含a/b排序(包含的在前面,不包含的在后面)
--再按a/b在字符串中出现的位置排序
Select*FromT
OrderbyCaseWhenPATINDEX('%[ab]%',St)>0Then0Else1End,PATINDEX('%[ab]%',St)

5. sql 字符串如何排序

方法一:
declare @a varchar(200),@i int,@m varchar(200),@n int
set @a=';djfalsdjflaksjf'
set @i=1
set @n=1
-- select substring(@a,@i,1)
while @i<len(@a)
begin
while @n<len(@a)
begin
if substring(@a,@n,1)>=substring(@a,@n+1,1)
begin
set @m=isnull(left(@a,@n-1),'')+substring(@a,@n+1,1)+substring(@a,@n,1)+isnull(right(@a,len(@a)-1-@n),'')
set @n=@n+1
-- select @i-1,@m,'1'
end
else
begin
set @m=isnull(left(@a,@n-1),'')+substring(@a,@n,1)+substring(@a,@n+1,1)+isnull(right(@a,len(@a)-1-@n),'')
set @n=@n+1
-- select @i-1,@m,'2'
end
set @a=@m
end
-- select @a,'3'
set @n=1
set @i=@i+1
end
select @a

方法二:
DECLARE @StrIn VARCHAR(30),@StrOut VARCHAR(30)
SET @StrIn='我是DB不要你说是吗select'
DECLARE @TmpChar VarCHAR(2)
DECLARE @i INT,@j INT,@l int
DECLARE @t TABLE(id INT,cs VarCHAR(2))
--DECLARE @m TABLE(StrCol VARCHAR(30))
SET @l=DATALENGTH(@StrIn)
SET @i=1
--将字符串分拆到临时表
WHILE @i<=@l
BEGIN
INSERT INTO @t VALUES(@i,SUBSTRING(@StrIn,@i,1))
SET @i=@i+1
END
--从临时表中重新组织字符串
DECLARE ct CURSOR FOR SELECT cs FROM @t ORDER BY cs
OPEN ct
FETCH NEXT FROM ct INTO @TmpChar
WHILE @@FETCH_STATUS=0
BEGIN

SET @StrOut=ISNULL(@StrOut,'')+@TmpChar

FETCH NEXT FROM ct INTO @TmpChar

END
CLOSE ct
DEALLOCATE ct
SELECT @StrOut
对字符串排序二:
create Function [dbo].[F_OrderbyStr]
(
@str varchar(200)--排序字符串
/*
eg:
select dbo.F_OrderbyStr('12,25,10,13,96,25,41')
*/
)
returns varchar(200)
as
begin
declare @table table (idx int)
declare @tbl table (id int identity(1,1),idx int)
while(CHARINDEX(',',@str)>=0)
begin
if (CHARINDEX(',',@str)=0)
begin
insert into @table(idx) values(@str)
break
end
else
begin
insert into @table(idx) values(LEFT(@str,CHARINDEX(',',@str)-1))
set @str=SUBSTRING(@str,CHARINDEX(',',@str)+1,255)
end
end
insert into @tbl
select * from @table order by idx
declare @max int,@min int,@strNew varchar(200),@value varchar(20)
set @strNew=''
select @max=MAX(id) from @tbl
select @min=min(id) from @tbl
while(@min<=@max)
begin
select @value=idx from @tbl where id=@min
set @strNew=@strNew+','+@value

set @min=@min+1
end
return substring(@strNew,2,200)
end

6. sql中表的数据的排序方式如何修改

系统默认是以聚集索引排序,主键默认是聚集索引,也可手动改成非聚集索引,你可以添加一列标识列,就是自增列,然后把聚集索引给这列,系统就会默认按添加顺序排了

7. 用PL/SQL如何给一条字符串内容排序

如果要替换的话,要update
oracle中要
update
table
set
xx=substr(xx,7,2000)
注意一点的是,2000是代表xx字段长度,如果你那个内容长度>2000,那地方你自己修改,意思是从第7位开始截取,截取长度是2000个字符
排序的话就order
by
那个字段就行了
如果你单纯要select的话,那么
select
substr(xx,7,2000)
xx
from
table
order
by
substr(xx,7,2000)就行了
---------------补充-------------
你的xxxxxx的内容要是相同的话
select
replace(字段名,'xxxxxx','')
from
table
order
by
replace(字段名,'xxxxxx','');

8. sql数字字符串排序

--负责把字符串转换为Varbinary
--思路,把字符串按.拆分,然后转换成int,再转换成varbinary拼接
CreateFunctionf_Order(@SourceSqlVarchar(8000),@StrSeprateVarchar(2))
ReturnsVarbinary(8000)
As
Begin
Declare@tempVarbinary(8000)=0x0
Declare@chVarchar(100)
Set@SourceSql=@SourceSql+@StrSeprate
While(@SourceSql<>'')
Begin
Set@ch=left(@SourceSql,Charindex(@StrSeprate,@SourceSql,1)-1)
Set@temp=@temp+Convert(Varbinary,Convert(Int,@ch))
Set@SourceSql=Stuff(@SourceSql,1,Charindex(@StrSeprate,@SourceSql,1),'')
End
Return@temp
End
Go

--建表
CreatetableT
(
AVarchar(100)
)

--插入数据
InsertIntoTValues('1.1')
InsertIntoTValues('1.1.1')
InsertIntoTValues('1.1.2')
InsertIntoTValues('1.2')
InsertIntoTValues('10.1')
InsertIntoTValues('10.1.1')
InsertIntoTValues('10.1.2')
InsertIntoTValues('11.1')
InsertIntoTValues('2.1')
InsertIntoTValues('3.1')
InsertIntoTValues('4.1')

--测试
Select*fromT
orderbydbo.f_Order(A,'.')