Solved

SQL Server Query

Posted on 2011-09-23
8
245 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
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.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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