SQL拆卸
㈠ 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
㈡ SQL語句拆分表中內容,並變成兩列
if exists (select 1 from sysobjects where name = 'uf_get_value' and xtype = 'FN')
drop function uf_get_value
go
create function uf_get_value(@value VARCHAR(100))
--獲取列默認值
returns VARCHAR(100) as
begin
declare @rtn VARCHAR(100), @tmp VARCHAR(100), @status CHAR(1)
SET @rtn = ''
SET @tmp = ''
WHILE len(@value) > 0
BEGIN
IF ascii(substring(@value, 1, 1)) = ascii(',')
BEGIN
SET @value = RIGHT(@value, len(@value) - 1)
CONTINUE
END
IF ascii(substring(@value, 1, 1)) BETWEEN ascii('0') AND ascii('9')
BEGIN
SET @tmp = @tmp + substring(@value, 1, 1)
SET @status = 'N'
END
ELSE
BEGIN
SET @rtn = @rtn + substring(@value, 1, 1)
SET @status = 'S'
END
SET @value = RIGHT(@value, len(@value) - 1)
IF @value <> ''
BEGIN
IF @status = 'S' AND ascii(substring(@value, 1, 1)) BETWEEN ascii('0') AND ascii('9') SET @rtn = @rtn + ','
IF @status = 'N' AND NOT ascii(substring(@value, 1, 1)) BETWEEN ascii('0') AND ascii('9') SET @tmp = @tmp + ','
END
END
IF RIGHT(@rtn, 1) = ',' SET @rtn = LEFT(@rtn, len(@rtn) - 1)
SET @rtn = @rtn + '|' + @tmp
RETURN @rtn
END
go
--測試
--SELECT dbo.uf_get_value('A123,B23,BD21')
㈢ SQL如何進行表的拆分
1.復製表結構及數據到新表
create table 新表 select * from 舊表 where ...
2.只復製表結構到新表
create table 新表 select * from 舊表 where 1=2 (即:讓where條件不成立)
3.復制舊表的數據到新表(假設兩個表結構一樣)
insert into 新表 select * from 舊表
4.復制舊表的數據到新表(假設兩個表結構不一樣)
insert into 新表(欄位1,欄位2,.......) select 欄位1,欄位2,...... from 舊表.
㈣ SQL拆分逗號分隔的字元串
1、首先點擊新建查詢按鈕,新建一個查詢。
㈤ sql數據拆分
第一個
FROM ccc A, # B
就該是
FROM ccc A, #臨時表名 B