MS SQL 2005, count from substring.

I have 2 tables like this:
table 1:
Number1        Cost
09                   10
08                   20
06                   10
09                    5


and Table2:
Number2
1009
2009
0008
0009

and I want to get some information like this:

Number1        Cost                MathNumber (count the 2 right numbers of Number2 from the Table2)
    09               10                        3
    08               20                        1
    06               10                        0
    09                5                         3

How can I get above infomation with select statement?
JameMeckAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbprogrammerCommented:
What's the relation between 2 tables?
0
JameMeckAuthor Commented:
Just 2 tables, no relation.
Thanks!
0
mbprogrammerCommented:
With same record count?
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Kakhaber SiradzeCommented:
you can create function and use it in select.
CREATE FUNCTION [dbo].[count_number1]
(@number1 varchar(20)
)
RETURNS numeric
AS
BEGIN
	RETURN
     (select count(*) from dbo.table2
     where CHARINDEX(@number1, number2)>0)
END

-------------------------------

select 
  Number1,
  Cost,
  dbo.count_number1(Number1) as MathNumber
from
  dbo.table1

Open in new window

0
cyberkiwiCommented:
Hi there,

Looks like you need this

select t1.Number1, t1.Cost, (select count(*) from table2 t2 where t1.number1 = right(t2.number2,2))
from table1 t1
0
cyberkiwiCommented:
If you find that either "number" field contains non-numeric values, try this conversion

select t1.Number1, t1.Cost, (
    select count(*) from table2 t2
    where convert(varchar,t1.number1) = right(t2.number2,2))
from table1 t1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AmitGKCommented:
Select Number1, Cost,
(Select count(Number2) from Table2 Where SUBSTRING(Number2, 3, 4) = Number1) as 'MathNumber'
From Table1
0
JameMeckAuthor Commented:
Thanks!
It is perfect solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.