Solved

T-SQL using Replace and Substring

Posted on 2010-09-21
3
842 Views
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

0
Comment
Question by:thomasm1948
[X]
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
3 Comments
 
LVL 32

Accepted Solution

by:
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



0
 

Author Comment

by:thomasm1948
ID: 33727854
That worked out perfectly

Thank you for all of your help
0
 

Author Closing Comment

by:thomasm1948
ID: 33727862
This worked perfectly

Thank you for all of your help
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 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