2009-04-15 37 views
0

我在T-SQL中有這樣的說法。在T-SQL中實現和應用字符串拆分

SELECT Bay From TABLE where uid in (
    select B_Numbers from Info_Step WHERE uid = 'number' 
) 

我從TABLE選擇 「多重」 BAY S其中其uid等於號碼這樣的字符串:

B_Numbers = 1:45:34:98 

因此,我應該從TABLE被選擇4角不同的BAY秒。我基本上需要將字符串1:45:34:98分成4個不同的數字。

我在想Split()會工作,但它不會,我得到一個語法錯誤。 來自T-SQL神的任何想法都會很棒!

回答

0

你應該保持你的數組爲行,但如果我理解你的問題,我認爲這將工作。

SELECT 
    Bay 

From 
    TABLE 
    join Info_Step 
    on B_Numbers like '%'+ uid +'%' 

where 
    Info_Step.uid = 'number' 

由於類似的運算符,此查詢將執行全表掃描。

+0

這不會起作用,因爲輸入字符串需要被分成標記(4個不同的行)和然後把桌子連在一起。你的方法將嘗試找到像'..... 1:45:34:98 .....'這樣的UID,但UID可能不是整數。 – beach 2009-08-04 04:44:18

0

你可以什麼do循環通過B_Numbers條目,並在:上執行自己的拆分將這些條目插入臨時表中,然後執行查詢。

DECLARE @i    int 
DECLARE @start   int 
DECLARE @B_Numbers  nvarchar(20) 
DECLARE @temp   table (
    number nvarchar(10) 
) 

-- SELECT B_Numbers FROM Info_Step WHERE uid = 'number' 
SELECT @B_Numbers = '1:45:34:98' 

SET @i = 0 
SET @start = 0 

-- Parse out characters delimited by ":"; 
-- Would make a nice user defined function. 
WHILE @i < len(@B_Numbers) 
BEGIN 
    IF substring(@B_Numbers, @i, 1) = ':' 
    BEGIN 
     INSERT INTO @temp 
     VALUES (substring(@B_Numbers, @start, @i - @start)) 
     SET @start = @i + 1 
    END 

    SET @i = @i + 1 
END 

-- Insert last item 
INSERT INTO @temp 
VALUES (substring(@B_Numbers, @start, @i - @start + 1)) 

-- Do query with parsed values 
SELECT Bay FROM TABLE WHERE uid in (SELECT * FROM @temp) 
2

這是一種使用輔助數字表來解析輸入字符串的方法。該邏輯可以很容易地添加到返回表的函數中。然後可以將該表連接起來查找正確的行。

第1步:創建Numbers表格

SET NOCOUNT ON 
GO 

IF EXISTS 
(
    SELECT 1 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE  TABLE_NAME = 'Numbers' 
      AND  TABLE_SCHEMA = 'dbo' 
      AND  TABLE_TYPE = 'BASE TABLE' 
) 
BEGIN 
    DROP TABLE dbo.Numbers 
END 
GO 

CREATE TABLE dbo.Numbers 
(
    Number smallint IDENTITY(1, 1) PRIMARY KEY 
) 
GO 

WHILE 1 = 1 
BEGIN 
    INSERT INTO dbo.Numbers DEFAULT VALUES 

    IF SCOPE_IDENTITY() = 32767 
    BEGIN 
     BREAK 
    END 
END 
GO 

第2步:分析輸入字符串

CREATE FUNCTION dbo.ParseString(@input_string varchar(8000), @delim varchar(8000) = " ") 
RETURNS TABLE 
AS RETURN 
(
    SELECT Number 
    FROM dbo.Numbers 
    WHERE CHARINDEX 
    ( 
     @delim + CONVERT(VARCHAR(12),Number) + @delim, 
     @delim + @input_string + @delim 
    ) > 0 
) 
GO 

**EXAMPLE** 
SELECT * FROM dbo.ParseString('1:45:34:98',':') 

第3步:使用,但是你想要的結果/需要

Number 
------ 
1 
34 
45 
98 

末到終端的實例

創建函數,返回相應的B-號碼(當然改變它使用註釋掉SQL)

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE FUNCTION dbo.GetBNumber (@uid int) 
RETURNS VARCHAR(8000) 
AS 
BEGIN 
    RETURN '1:45:34:98' 
    --select B_Numbers from Info_Step WHERE uid = @uid 
END 
GO 

使用使用函數返回期望的結果

-- Using Test Data 
SELECT N.Number FROM Numbers N 
JOIN dbo.ParseString(dbo.GetBNumber(12345),':') Q ON Q.Number = N.Number 

-- Using Your Data (Untested but should work.) 
SELECT N.Bay 
FROM TABLE N 
JOIN dbo.ParseString(dbo.GetBNumber(ENTER YOU NUMBER HERE),':') Q ON Q.Number = N.uid 

結果

Number 
------ 
1 
34 
45 
98 
0

你甚至可以嘗試這個

declare @str varchar(50) 
set @str = '1:45:34:98' 

;with numcte as(
select 1 as rn union all select rn+1 from numcte where rn<LEN(@str)), 
getchars as(select 
ROW_NUMBER() over(order by rn) slno, 
rn,chars from numcte 
cross apply(select SUBSTRING(@str,rn,1) chars)X where chars = ':') 

select top 1 
    Bay1 = SUBSTRING(@str,0,(select rn from getchars where slno = 1)) 
    ,Bay2 = SUBSTRING(@str, 
      (select rn from getchars where slno = 1) + 1, 
      (((select rn from getchars where slno = 2)- 
      (select rn from getchars where slno = 1) 
      )-1)) 
    ,Bay3 = SUBSTRING(@str, 
      (select rn from getchars where slno = 2) + 1, 
      (((select rn from getchars where slno = 3)- 
      (select rn from getchars where slno = 2) 
      )-1)) 
    ,Bay4 = SUBSTRING(@str, 
      (select rn from getchars where slno = 3)+1, 
      LEN(@str)) 
    from getchars 

輸出:

Bay1 Bay2 Bay3 Bay4 
1 45 34 98