T-SQL using Replace and Substring

Posted on 2010-09-21
Last Modified: 2012-05-10

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

Question by:thomasm1948
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 32

Accepted Solution

bhess1 earned 500 total points
ID: 33727701
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


Author Comment

ID: 33727854
That worked out perfectly

Thank you for all of your help

Author Closing Comment

ID: 33727862
This worked perfectly

Thank you for all of your help

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question