Solved

SQL Server Query

Posted on 2011-09-23
8
244 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Caste datetime 2 51
Delete Trigger in SQL Server2008R2 5 20
triggered use of sp_send_dbmail failure 2 22
MS SQL Inner Join - Multiple Join Parameters 2 19
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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

22 Experts available now in Live!

Get 1:1 Help Now