Solved

SQL Server Query

Posted on 2011-09-23
8
243 Views
Last Modified: 2012-05-12
I am trying to create a query in sql, what i am trying to do is extract certain text out of a memo feild, to be displayed in a new column, and example of the memo feild is as follows

With reference to This Spec: 102-004440 - issue 2USML:  X1(c) J000Export Licence No. 050304646Line Item 4

and what i would like to be able to extract to a new column is simply the export licence no (0503046) i need to do this for a bunch of data so is this possible. once i know how to do this i would need to also be able to do it for other text in the memo feild.

Thanks

john
0
Comment
Question by:pepps11976
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:carsRST
ID: 36586272
Assming the License number is always the same length and comes after "No." you could try this approach to isolate that value.


DECLARE @val varchar(max)
DECLARE @searchFor varchar(10)
set @val = 'With reference to This Spec: 102-004440 - issue 2USML:  X1(c) J000Export Licence No. 050304646Line Item 4'
set @searchFor = 'No.'

select SUBSTRING(@val, CHARINDEX(@searchFor, @val)+4,9)
0
 

Author Comment

by:pepps11976
ID: 36586714
Hi carsRST could you help me put this into a view my current view is like this

SELECT     TOP (100) PERCENT dbo.ihead.ih_sorder, dbo.itran.it_memo, dbo.itran.it_anal, dbo.itran.it_stock, dbo.itran.it_quan, dbo.itran.it_dtedelv
FROM         dbo.ihead LEFT OUTER JOIN
                      dbo.itran ON dbo.ihead.ih_doc = dbo.itran.it_doc
WHERE     (dbo.itran.it_memo LIKE '%Foreign End User%') AND (dbo.ihead.ih_sorder = 'ORD15686') AND (dbo.itran.it_status = 'a') AND (dbo.itran.it_recno = '3')
ORDER BY dbo.itran.it_recno

john
0
 
LVL 16

Accepted Solution

by:
carsRST earned 500 total points
ID: 36586840
I obviously can't test but try...


SELECT     TOP (100) PERCENT dbo.ihead.ih_sorder, SUBSTRING(dbo.itran.it_memo, CHARINDEX('No.', dbo.itran.it_memo)+4,9), dbo.itran.it_anal,
dbo.itran.it_stock, dbo.itran.it_quan, dbo.itran.it_dtedelv
FROM         dbo.ihead LEFT OUTER JOIN
                      dbo.itran ON dbo.ihead.ih_doc = dbo.itran.it_doc
WHERE     (dbo.itran.it_memo LIKE '%Foreign End User%') AND (dbo.ihead.ih_sorder = 'ORD15686') AND (dbo.itran.it_status = 'a') AND (dbo.itran.it_recno = '3')
ORDER BY dbo.itran.it_recno
0
 

Author Comment

by:pepps11976
ID: 36586868
Ok that seems to work could you just walk me through what this is actually doing i am fairly new to sql, and what is the criteria for this to always work?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 16

Expert Comment

by:carsRST
ID: 36586919
Yep...

Just using two built-in SQL Server functions.

1.  SUBSTRING: grabs a portion of your text.  You just tell it at what character index to start.  

2.  CHARINDEX: searches your text for whatever you tell it to search for and returns the starting position.


So I used the SUBSTRING function to pull out JUST the license number.   And I used the CHARINDEX to tell the SUBSTRING function where to start stripping the data.  The SUBSTRING function's final parameter wants to know how many characters to strip - for you I knew your license number is 9 characters, so that's what I put in the function.

 
0
 

Author Comment

by:pepps11976
ID: 36586929
Brilliant thanks for your help on this
0
 
LVL 16

Expert Comment

by:carsRST
ID: 36586931
You bet.

0
 

Author Comment

by:pepps11976
ID: 36587050
CarsRST

I have just run into another issue i am going to post as a new question hopefully with your expertise you may be able to help again
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

18 Experts available now in Live!

Get 1:1 Help Now