Solved

T-SQL using Replace and Substring

Posted on 2010-09-21
3
826 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
  • 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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