Solved

T-SQL using Replace and Substring

Posted on 2010-09-21
3
817 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
Comment Utility
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
Comment Utility
That worked out perfectly

Thank you for all of your help
0
 

Author Closing Comment

by:thomasm1948
Comment Utility
This worked perfectly

Thank you for all of your help
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now