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,'.')