當前位置:首頁 » 編程語言 » sql字元串拆分

sql字元串拆分

發布時間: 2023-08-08 12:30:44

1. sql語句如何將一個字元串拆分重組

您好,您可以參考下面的程序:
declare @str varchar(20)
set @str = 'abcdefg'
declare @i int
declare @count int
select @count = 0
select @i = len(@str)

declare @str1 varchar(20)
select @str1 = ''
while (@i > @count)
begin
select @count = @count + 1
select @str1 = @str1 + substring (@str,@count,1) + '.'

end

print @str1
這個字元串,你可以從表欄位中獲取,也或者可以直接賦值,最好@str1就是你想要的輸出。

2. sql如何根據隔符分割字元串

  1. 資料庫自帶的substring()、charindex()函數,可以根據需要截取字元串,但並不能實現分割

  2. 自己寫分割函數,以下可以參考:

createfunctionGetStr
(
@strvarchar(1024),--要分割的字元串
@splitvarchar(10),--分隔符號
@indexint--取第幾個元素
)
returnsvarchar(1024)
as
begin
declare@locationint
declare@startint
declare@nextint
declare@seedint
set@str=ltrim(rtrim(@str))
set@start=1
set@next=1
set@seed=len(@split)

set@location=charindex(@split,@str)
while@location<>0and@index>@next
begin
set@start=@location+@seed
set@location=charindex(@split,@str,@start)
set@next=@next+1
end
if@location=0select@location=len(@str)+1
returnsubstring(@str,@start,@location-@start)
end

3. 在SQL Server資料庫中拆分字元串函數

SQL Server資料庫中拆分字元串函數的具體方法

CREATE FUNCTION uf_StrSplit (@origStr varchar( ) 待拆分的字元串@markStr varchar( )) 拆分標記 如 RETURNS @splittable table( str_id varchar( ) NOT NULL 編號ID string varchar( ) NOT NULL 拆分後的字元串)ASBEGINdeclare @strlen int @postion int @start int @sublen int @TEMPstr varchar( ) @TEMPid intSELECT @strlen=LEN(@origStr) @start= @sublen= @postion= @TEMPstr= @TEMPid= if(RIGHT(@origStr )<>@markStr )beginset @origStr = @origStr + @markStrendWHILE((@postion<=@strlen) and (@postion != ))BEGINIF(CHARINDEX(@markStr @origStr @postion)!= )BEGINSET @sublen=CHARINDEX(@markStr @origStr @postion) @postion;ENDELSEBEGINSET @sublen=@strlen @postion+ ;ENDIF(@postion<=@strlen)BEGINSET @TEMPid=@TEMPid+ ;SET @TEMPstr=SUBSTRING(@origStr @postion @sublen);INSERT INTO @splittable(str_id string)values(@TEMPid @TEMPstr)IF(CHARINDEX(@markStr @origStr @postion)!= )BEGINSET @postion=CHARINDEX(@markStr @origStr @postion)+ ENDELSEBEGINSET @postion=@postion+ ENDENDENDRETURNEND

例如 select * from uf_StrSplit( )

輸出結果

lishixin/Article/program/SQLServer/201311/22421

4. sql查詢里 怎麼拆分字元串(按「/」拆分)

先建立一個自定義函數,之個函數非常有用,建議收入自已的資料庫
CREATE FUNCTION mysplit--將以某分隔符分段的字串,按指定的順序號提取子串:

java">(@strnvarchar(2000),--源字串
@snint,--提取序號
@Delivarchar(1)--分隔符
)
RETURNSvarchar(100)
AS
BEGIN
declare@firstint,@lastint,@resultvarchar(1000),@sn0int
select@sn0=0,@first=0,@LAST=1,@str=@str+REPLICATE(@DELI,1)
while@sn0!=@sn
begin
select@sn0=@sn0+1,@first=@LAST,@last=charindex(@DELI,@str,@LAST)+1
end
if@last-@first-1<0
set@result=''
else
SET@RESULT=SUBSTRING(@str,@FIRST,@LAST-@FIRST-1)
RETURN(@RESULT)
END

查詢方法:

DECLARE@AVARCHAR(100),@BVARCHAR(100),@CVARCHAR(100)
SELECT
@A=DBO.MYSPLIT('A|B|C',1,'|'),
@B=DBO.MYSPLIT('A|B|C',2,'|'),
@C=DBO.MYSPLIT('A|B|C',3,'|')
SELECT@A,@B,@C

5. sql分割字元串查詢,分組統計

首先掌握sql中分割字元串的方法:
declare @sql varchar(2000),@tsql nvarchar(max),@split varchar(100)
set @sql='1,3,5,6,8' --保存的字元
set @split=','--分隔符
select @tsql='select '''+replace(@sql,@split,''' union all select ''')+''''
exec(@tsql)
----完成之後將查詢結果保存在一張臨時表中。
然後使用 select count(*) , 欄位名 from 表名 group by 欄位名 就可以得到您想要的結果了。
希望對您有所幫助!

6. SQL語言如何將表內字元串按分割符號分割存儲

用replace函數,將分號或者 @ 符號, 替換為你的其他分隔符。

REPLACE
用第三個表達式替換第一個字元串表達式中出現的所有第二個給定字元串表達式。
語法
REPLACE ( ''string_replace1'' , ''string_replace2'' , ''string_replace3'' )
參數
''string_replace1''
待搜索的字元串表達式。string_replace1 可以是字元數據或二進制數據。
''string_replace2''
待查找的字元串表達式。string_replace2 可以是字元數據或二進制數據。
''string_replace3''
替換用的字元串表達式。string_replace3 可以是字元數據或二進制數據。
返回類型
如果 string_replace(1、2 或 3)是支持的字元數據類型之一,則返回字元數據。如果 string_replace(1、2 或 3)是支持的 binary 數據類型之一,則返回二進制數據。
示例
下例用 xxx 替換 abcdefghi 中的字元串 cde。
SELECT REPLACE(''abcdefghicde'',''cde'',''xxx'')GO
下面是結果集:
------------abxxxfghixxx(1 row(s) affected)

7. sql語句中如何分割字元串進行替換

方法一:
varchar和nvarchar類型是支持replace,所以如果你的text不超過8000可以先轉換成前面兩種類型再使用replace
替換
text
ntext
數據類型欄位的語句

update
表名
set
欄位名=replace(cast(與前面一樣的欄位名
as
varchar(8000))
,'原本內容','想要替換成什麼')
方法二:
update
[表名]
set
欄位名
=
replace(與前面一樣的欄位名,'原本內容','想要替換成什麼')

8. sql 語句 怎麼將多個分號字元串拆分成多條數據

sql server中沒有這種函數,可以自建一個function去拆分,
給你個思路,
用charindex 或是instr等函數,在循環體中查找分隔符出現的位置和並記錄出現次數到變數,然後使用substring函數截取字元串,輸出

熱點內容
安卓如何設置桌面返回鍵 發布:2025-02-06 13:58:15 瀏覽:48
bi可視化php 發布:2025-02-06 13:50:15 瀏覽:931
shell寫腳本文件 發布:2025-02-06 13:47:32 瀏覽:231
健身器材腳本 發布:2025-02-06 13:46:36 瀏覽:856
怎麼從手機里卸載存儲卡 發布:2025-02-06 13:35:04 瀏覽:644
誅仙青雲志2ftp 發布:2025-02-06 13:34:48 瀏覽:34
mill91編程 發布:2025-02-06 13:10:27 瀏覽:294
華為平板怎麼儲存伺服器文件 發布:2025-02-06 12:49:21 瀏覽:482
php查詢結果數組 發布:2025-02-06 12:31:05 瀏覽:717
怎樣把照片壓縮打包 發布:2025-02-06 12:15:19 瀏覽:498