Substring Query SQL

20 pts.
Tags:
SQL
SQL Server Query
Hi All, I'm trying to build a query to join names in one table to abbreviated names in another table. So for example I have - Harry Harold Harrison on one table, but in the other table I have just Har, this can vary in length so does anyone know how I can sum the amount of Har names with the total from the other table bearing in mind the data could be Fred for frederick, freddy etc, Kind Regards, Stuart Mills

Answer Wiki

Thanks. We'll let you know when a new response is added.

I figured this out in the end, something like –

SELECT SUBSTRING(a.name, 1, LEN(b.NamePrefix)) AS Expr1, COUNT(a.name) AS Expr2, b.NamePrefix
FROM Customers a INNER JOIN
NamePrefixes b ON b.NamePrefix = SUBSTRING(a.name, 1, LEN(b.NamePrefix))

Gives me a count of names beginning with the matches in the nameprefixes table.

So if nameprefixes table has fred in it and names contains Fred, Frederick, Freddy, I get Fred and 3 as the count.

Thanks for showing an interest anyway

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    Could you provide some example data and the results you would want to get ?
    69,835 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following