Solved

SQL Server Query

Posted on 2011-09-23
8
248 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
[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
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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