Solved

SQL Separate and lookup value - follow up question

Posted on 2011-03-19
2
300 Views
Last Modified: 2012-05-11
Hi,

cyberkiwi answered my provious question, but since then I need to modify this slightly to filter only the relevant records for the particular companyid, I have attached a spreadsheet to show a sample of the data.

so with the following sp:
@id int,
@CompanyID varchar

AS
BEGIN

SET NOCOUNT ON;

select a.id, v.value, b.DocumentReference, '<a href="uploads''' + @CompanyID + '''/' + b.filename + '">' + b.filename + '</a>' As LinkFile

from tbl_permits a
cross apply dbo.values2table(a.MethodStatements, ',') v
inner join tbl_documents b on b.DocumentReference=v.value
where a.id=@id

END

If ID = 23 and CompanyID=18

the the returned value should be:

MS001      MS001      <a href="uploads18/MS 14 Document.pdf">MS001</a>

alternatively,
If ID = 3 and CompanyID=1

the the returned value should be:
MS001      MS001      <a href="uploads1/MS 1 Document.pdf">MS001</a>
MS002      MS002      <a href="uploads1/MS 2 Document.pdf">MS002</a>
MS003      MS003      <a href="uploads1/MS 3 Document.pdf">MS003</a>

How this makes sense.

 Book1.xls Book1.xls
0
Comment
Question by:sanjshah12
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35173029
interesting, your attached files has tbl_Documents and tbl_main as table names, your query has tbl_permit and tbl_documents ...

so, I presume the companyid is the one from tbl_Documents ...

@id int,
@CompanyID varchar

AS
BEGIN

SET NOCOUNT ON;

select a.id, v.value, b.DocumentReference, '<a href="uploads''' + @CompanyID + '''/' + b.filename + '">' + b.filename + '</a>' As LinkFile

from tbl_permits a
cross apply dbo.values2table(a.MethodStatements, ',') v 
inner join tbl_documents b on b.DocumentReference=v.value AND b.companyID = @CompanyID
where a.id=@id

END

Open in new window

0
 

Author Comment

by:sanjshah12
ID: 35173455
Thanks angelIII, Yes I had to change the code hence the slight change, anyway, I tried something similar but could not get it work, but your code works.

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

21 Experts available now in Live!

Get 1:1 Help Now