views:

2944

answers:

3

Hi all,

Previously I have asked to strip text from a field and convert it to an int, this works successfully. But now I would like to do an INNER JOIN on this new value.

So I have this:

SELECT CONVERT(int, SUBSTRING(accountingTab.id, PATINDEX('%[0-9]%', accountingTab.id), 999)) AS 'memId', userDetails.title, userDetails.lname

FROM accountingTab INNER JOIN (SELECT id, title, first, last FROM memDetTab) AS userDetails ON memID = userDetails.id

And then I get the Invalid Column Name 'memID' error.

Anybody a suggestion on how I can fix this?

Thanks in advance!

P.s. this naming convention is just as an example to simplify the scenario, in case you were wondering ;-)

+1  A: 

Instead of memId, repeat the whole expression.

Milan Babuškov
+1  A: 

If you have to do this, you have design problems. If you're able, I would suggest you need to refactor your table or relationships.

Remi Despres-Smyth
The original relationship is with a completely separate table and that table in time has lost information so the next best table is this accounting table :(
Symbioxys
Perhaps you can regenerate a key field from the id field, and/or split the accountingTab.id field into components which can be assembled into the original id field?
Remi Despres-Smyth
Thanks I'll give that a go, I'd rather do that then mess with this.
Symbioxys
For those who voted this answer down: sometimes the answer a person needs or wants to hear is "you shouldn't be doing this, try this other thing instead."
Remi Despres-Smyth
+4  A: 

You can either repeat the whole expression or reverse your join:


SELECT *
FROM memDetTab
    JOIN (SELECT CONVERT(int, SUBSTRING(accountingTab.id, PATINDEX('%[0-9]%', accountingTab.id), 999)) AS 'memId', userDetails.title, userDetails.lname
FROM accountingTab) subquery
    ON subquery.memID = memDetTab.ID
Chris Shaffer