Splitting text in SQL Server stored procedure

I’m working with a database, where one of the fields I extract is something like:

1-117 3-134 3-133

Each of these number sets represents a different set of data in another table. Taking 1-117 as an example, 1 = equipment ID, and 117 = equipment settings.

  • sum of time based on flag from multiple rows SQL Server
  • Sql select statement with two conditions in visual basic
  • Login failed for user. Trying to access a SQL Server Express database
  • SQL Server : error converting data type varchar to numeric
  • Match comma-separated values in SQL Server column
  • invalid object name 'tableName' error
  • I have another table from which I need to extract data based on the previous field. It has two columns that split equipment ID and settings. Essentially, I need a way to go from the queried column 1-117 and run a query to extract data from another table where 1 and 117 are two separate corresponding columns.

    So, is there anyway to split this number to run this query?

    Also, how would I split those three numbers (1-117 3-134 3-133) into three different query sets?

    The tricky part here is that this column can have any number of sets here (such as 1-117 3-133 or 1-117 3-134 3-133 2-131).

    I’m creating these queries in a stored procedure as part of a larger document to display the extracted data.

    Thanks for any help.

    3 Solutions collect form web for “Splitting text in SQL Server stored procedure”

    Since you didn’t provide the DB vendor, here’s two posts that answer this question for SQL Server and Oracle respectively…

    T-SQL: Opposite to string concatenation – how to split string into multiple records

    Splitting comma separated string in a PL/SQL stored proc

    And if you’re using some other DBMS, go search for “splitting text “. I can almost guarantee you’re not the first one to ask, and there’s answers for every DBMS flavor out there.

    As you said the format is constant though, you could also do something simpler using a SUBSTRING function.

    EDIT in response to OP comment…

    Since you’re using SQL Server, and you said that these values are always in a consistent format, you can do something as simple as using SUBSTRING to get each part of the value and assign them to T-SQL variables, where you can then use them to do whatever you want, like using them in the predicate of a query.

    Assuming that what you said is true about the format always being #-### (exactly 1 digit, a dash, and 3 digits) this is fairly easy.

    WITH EquipmentSettings AS (
          Convert(int, Substring(S.AwfulMultivalue, V.Value * 6 - 5, 1) EquipmentID,
          Convert(int, Substring(S.AwfulMultivalue, V.Value * 6 - 3, 3) Settings
          SourceTable S
          INNER JOIN master.dbo.spt_values V
             ON V.Value BETWEEN 1 AND Len(S.AwfulMultivalue) / 6
          V.type = 'P'
       EquipmentSettings E
       INNER JOIN DestinationTable D
          ON E.EquipmentID = D.EquipmentID
          AND E.Settings = D.Settings

    In SQL Server 2005+ this query will support 1365 values in the string.

    If the length of the digits can vary, then it’s a little harder. Let me know.

    Incase if the sets does not increase by more than 4 then you can use Parsename to retrieve the result

     Declare @Num varchar(20)
     Set @Num='1-117 3-134 3-133'
     select parsename(replace (@Num,' ','.'),3)
     Result :- 1-117
     Now again use parsename on the same resultset 
     Select parsename(replace(parsename(replace (@Num,' ','.'),3),'-','.'),1)
     Result :- 117

    If the there are more than 4 values then use split functions

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.