SQL Server: any equivalent of strpos()?

后端 未结 3 1256
失恋的感觉
失恋的感觉 2020-12-16 12:31

I\'m dealing with an annoying database where one field contains what really should be stored two separate fields. So the column is stored something like \"The first string~

3条回答
  •  情书的邮戳
    2020-12-16 13:06

    If you need your data in columns here is what I use:

      create FUNCTION [dbo].[fncTableFromCommaString] (@strList varchar(8000))  
    RETURNS @retTable Table (intValue int) AS  
    BEGIN 
    
        DECLARE @intPos tinyint
    
        WHILE CHARINDEX(',',@strList) > 0
        BEGIN   
            SET @intPos=CHARINDEX(',',@strList) 
            INSERT INTO @retTable (intValue) values (CONVERT(int, LEFT(@strList,@intPos-1)))
            SET @strList = RIGHT(@strList, LEN(@strList)-@intPos)
        END
        IF LEN(@strList)>0 
            INSERT INTO @retTable (intValue) values (CONVERT(int, @strList))
    
        RETURN
    
    END
    

    Just replace ',' in the function with your delimiter (or maybe even parametrize it)

提交回复
热议问题