1. SQL Server 2005->Object Explorer->Databases->New Query
2. Create split function
IF OBJECT_ID (N'dbo.split', N'IF') IS NOT NULL3. Using nested queries with split function
DROP FUNCTION dbo.split
GO
CREATE FUNCTION dbo.split(@String VARCHAR(8000), @Delimiter CHAR(1))
RETURNS @temptable TABLE (items VARCHAR(8000))
AS
BEGIN
DECLARE @idx INT
DECLARE @slice VARCHAR(8000)
SELECT @idx = 1
IF LEN(@String)<1 idx =" CHARINDEX(@Delimiter,@String)" slice =" LEFT(@String,@idx" slice =" @String">0)
INSERT INTO @temptable(Items) VALUES(@slice)
SET @String = RIGHT(@String,len(@String) - @idx)
IF LEN(@String) = 0 BREAK
END
RETURN
SELECT DISTINCT FIELD1_NAME FROM TABLE1_NAME WHERE FIELD2_NAME = (
SELECT TOP (1) items FROM
dbo.split(
(SELECT FIELD3_NAME
FROM TABLE2_NAME
WHERE (FIELD4_NAME = 'FIELD4_VALUE')), ':')
)
參考Split Function in Sql Server
沒有留言:
張貼留言