[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 877
  • Last Modified:

T-SQL using Replace and Substring

I have a basic query that I created to abstract the mbps from a sql table.  the issue that I have his when the mbps (megabits per second) is double digits.  Below is my code.  Basically the SQL table has a column that has all of the iformation that I need in it but I need to seperate the data in other columns for reporting purposes.  The alrstr column is the column that has all of my information.

Sample data in the alrstr columns that I am extracting data from:

1.  Single Digit Mbps
 Interface Threshold [Intf=15:out_utilization=5.038173 % out_errors=0 out_wraps=0 out_speed=5038173 bps = 5.038173 Mbps > 5.000000 Mbps]

2.  Double Digit Mbps
 Interface Threshold [Intf=15:inp_utilization=16.331017 % inp_errors=2998 inp_wraps=26 inp_speed=16331017 bps = 16.331017 Mbps > 5.000000 Mbps]

My code is looking looking for the actual Mpbs and the rate limit  (16.331017 Mbps > 5.000000 Mbps)
SELECT  TOP (100) PERCENT CAST(REPLACE(SUBSTRING(alrstr, 28, 2), ':', '') AS int) AS Interface, 
REPLACE(REPLACE(REPLACE(SUBSTRING(alrstr, 30, 4), ':', ''), '_', ''), 'p', '') AS [IN/OUT], 
REPLACE(REPLACE(REPLACE(REPLACE(SUBSTRING(alrstr, 105, 15), '=', ''), 's', ''), ' ', ''), 'p', '') AS [Current Mbps],
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(SUBSTRING(alrstr, 122, 10), '>', ''), ' ', ''), 'M', ''), 'ps', ''), 's', '') AS [Alarm Rate Limit], 
devname, alrstr, alrDateTime
FROM   dbo.alarmslog
WHERE (alrstr LIKE '%Intf=15%')

Open in new window

  • 2
1 Solution
Brendt HessSenior DBACommented:
The major problem I see is that you are trying to parse a variable length string as if it were a fixed length string.  Let's see.....

The first value (interface) is probably fixed enough to use your code snippet.
Same for the second value (IN/OUT)

However, since the number of characters in the _errors fields and the _wraps fields are variable, you cannot treat the rest of the string as if it were fixed.  In this case, you need to locate the positions of the fields more precisely.

Although this is not necessarily how I would do it, this fits your coding pattern in general, and should work for you:

REPLACE(REPLACE(SUBSTRING(alrstr, CHARINDEX(' bps =', alrstr) + 7, 11), 'M', ''), ' ', '')  as [Current Mbps],   -- this version will handle up to three digits before the decimal point
REPLACE(SUBSTRING(alrstr, CHARINDEX(' Mbps >', alrstr) + 8, 9), ' ', '') AS [Alarm Rate Limit],                      -- this will handle up to 2 digits before the decimal point

thomasm1948Author Commented:
That worked out perfectly

Thank you for all of your help
thomasm1948Author Commented:
This worked perfectly

Thank you for all of your help

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now