Solved

MS SQL Query

Posted on 2014-10-10
6
111 Views
Last Modified: 2014-10-10
Hi,

I have a field that contains data like this:

 1 4965 ^^<>1952</>
 1 4965 ^
 1 2365 ^^<>1952</>
 1 5422 ^
 1 5433 ^^<>1800</>

There is space before 1 as well.

I would like to run a query to extract the value between <></> and when I don't have <></> to just return ''. So the result should be

1952

1952

1800

Can you please help me with the query?

Thanks.
0
Comment
Question by:ezzadin
[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
  • 2
  • 2
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40374316
Something like this perhaps:
SELECT CASE 
		WHEN CHARINDEX('<>', Field) > 0 THEN
			CASE 
				WHEN CHARINDEX('</>', Field) > 0 THEN
					CASE 
						WHEN CHARINDEX('<>', Field) < CHARINDEX('</>', Field) THEN SUBSTRING(Field, CHARINDEX('<>', Field) + 2, CHARINDEX('</>', Field) - CHARINDEX('<>', Field) - 2)
						ELSE ''
					END
				ELSE ''
			END
		ELSE ''
	END
FROM	MyTable

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 40374318
This is how I tested it:
DECLARE @MyTable table (Field CHAR(50))
INSERT @MyTable(Field)
VALUES ('1 4965 ^^<>1952</>'), 
		('1 4965 ^'),
		('1 2365 ^^<>1952</>'), 
		('1 5422 ^'), 
		(' 1 5433 ^^<>1800</>')

SELECT CASE 
		WHEN CHARINDEX('<>', Field) > 0 THEN
			CASE 
				WHEN CHARINDEX('</>', Field) > 0 THEN
					CASE 
						WHEN CHARINDEX('<>', Field) < CHARINDEX('</>', Field) THEN SUBSTRING(Field, CHARINDEX('<>', Field) + 2, CHARINDEX('</>', Field) - CHARINDEX('<>', Field) - 2)
						ELSE ''
					END
				ELSE ''
			END
		ELSE ''
	END
FROM	@MyTable

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40374325
|             FIELDX | LFT | RGT | COLUMN_3 |
|--------------------|-----|-----|----------|
| 1 4965 ^^<>1952</> |   8 |  16 |     1952 |
|           1 4965 ^ |   0 |   0 |   (null) |
| 1 2365 ^^<>1952</> |   8 |  16 |     1952 |
|           1 5422 ^ |   0 |   0 |   (null) |
| 1 5433 ^^<>1800</> |   8 |  16 |     1800 |

Open in new window


select
        fieldx
      , ca.lft
      , ca.rgt
      , case when ca.lft > 4 and ca.rgt > 3 then substring(fieldx,ca.lft+4,(ca.rgt-ca.lft)-4) else NULL end
from table1
cross apply (
            select patindex('%^^<>%',fieldx), patindex('%</>%',fieldx)
             ) ca (lft, rgt)

Open in new window


CREATE TABLE Table1
	([fieldx] varchar(40))
;
	
INSERT INTO Table1
	([fieldx])
VALUES
	('1 4965 ^^<>1952</>'),
	('1 4965 ^'),
	('1 2365 ^^<>1952</>'),
	('1 5422 ^'),
	('1 5433 ^^<>1800</>')
; 

Open in new window

http://sqlfiddle.com/#!3/2897a/1
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Closing Comment

by:ezzadin
ID: 40374326
Worked. Great. Thanks.
0
 

Author Comment

by:ezzadin
ID: 40374328
@PortletPaul, sorry, I saw your solution after accepting Anthony's.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40374330
yes, an 18 second gap between those events :)

no matter. I like using cross apply for these sorts of queries as the aliases (such as "lft") can then be used in the select clause making it a bit easier to understand (in my view)
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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