How to Split String by Character into Separate Columns in SQL Server

前端 未结 5 1208
清歌不尽
清歌不尽 2020-12-03 19:17

I have one field in SQL Server containing section, township and range information, each separated by dashes; for example: 18-84-7. I\'d like to have this infor

相关标签:
5条回答
  • 2020-12-03 19:20

    Its better to replace the second section answered by BWS by this one :

    select SUBSTRING(dat,CHARINDEX('-', dat) + 1,LEN(dat) - CHARINDEX('-', dat) - CHARINDEX('-', REVERSE(dat)) ) from myTable.
    
    0 讨论(0)
  • 2020-12-03 19:32

    Please try more reliable code

    CREATE BELOW FUNCTION

    CREATE FUNCTION dbo.UFN_SEPARATES_COLUMNS(
     @TEXT      varchar(8000)
    ,@COLUMN    tinyint
    ,@SEPARATOR char(1)
    )RETURNS varchar(8000)
    AS
      BEGIN
           DECLARE @POS_START  int = 1
           DECLARE @POS_END    int = CHARINDEX(@SEPARATOR, @TEXT, @POS_START)
    
           WHILE (@COLUMN >1 AND @POS_END> 0)
             BEGIN
                 SET @POS_START = @POS_END + 1
                 SET @POS_END = CHARINDEX(@SEPARATOR, @TEXT, @POS_START)
                 SET @COLUMN = @COLUMN - 1
             END 
    
           IF @COLUMN > 1  SET @POS_START = LEN(@TEXT) + 1
           IF @POS_END = 0 SET @POS_END = LEN(@TEXT) + 1 
    
           RETURN SUBSTRING (@TEXT, @POS_START, @POS_END - @POS_START)
      END
    GO
    

    AND Then try below code

    DECLARE @STRING VARCHAR(20) ='1-668-333'
    SELECT
      dbo.UFN_SEPARATES_COLUMNS(@STRING, 1, '-') AS VALUE1,
      dbo.UFN_SEPARATES_COLUMNS(@STRING, 2, '-') AS VALUE2,
      dbo.UFN_SEPARATES_COLUMNS(@STRING, 3, '-') AS VALUE3
    

    RESULT

    If you need more understanding please go

    https://social.technet.microsoft.com/wiki/contents/articles/26937.t-sql-splitting-a-string-into-multiple-columns.aspx

    0 讨论(0)
  • There are probably several different ways to do it, some uglier than others. Here's one:

    (Note: dat = the string of characters)

    select *,
      substring(dat,1,charindex('-',dat)-1) as Section,
      substring(dat,charindex('-',dat)+1,charindex('-',dat)-1) as TownShip,
      reverse(substring(reverse(dat),0,charindex('-',reverse(dat)))) as myRange
    from myTable
    
    0 讨论(0)
  • 2020-12-03 19:41

    you could use something like this (posted by @canon)

    CREATE FUNCTION [dbo].[Split]
    (   
     @String varchar(max)
    ,@Delimiter char
    )
    RETURNS @Results table
    (
     Ordinal int
    ,StringValue varchar(max)
    )
    as
    begin
    
        set @String = isnull(@String,'')
        set @Delimiter = isnull(@Delimiter,'')
    
        declare
         @TempString varchar(max) = @String
        ,@Ordinal int = 0
        ,@CharIndex int = 0
    
        set @CharIndex = charindex(@Delimiter, @TempString)
        while @CharIndex != 0 begin     
            set @Ordinal += 1       
            insert @Results values
            (
             @Ordinal
            ,substring(@TempString, 0, @CharIndex)
            )       
            set @TempString = substring(@TempString, @CharIndex + 1, len(@TempString) - @CharIndex)     
            set @CharIndex = charindex(@Delimiter, @TempString)
        end
    
        if @TempString != '' begin
            set @Ordinal += 1 
            insert @Results values
            (
             @Ordinal
            ,@TempString
            )
        end
    
        return
    end
    

    for more take a look at How to split string using delimiter char using T-SQL?

    0 讨论(0)
  • 2020-12-03 19:41
    DROP PROCEDURE getName
    GO
    create proc getName as
    begin
    select * , substring(name, 1 , CHARINDEX(' ', name)-1) as 'First Name',
    SUBSTRING(name, CHARINDEX(' ', name)+1, len(name)) as 'Last Name' 
    from Employee 
    order by [Last Name]
    end
    go
    exec getName
    
    0 讨论(0)
提交回复
热议问题