Solved

SQL Server Query

Posted on 2011-09-23
8
246 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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