March 2, 2011 at 8:07 am
I am doing a select query, and trying to join in another table where the common field in one table looks like "1234" and the other table has a dash and extra numbers like "1234-01". My join statement is this -
left outer join orders o on o.orderno = d.jobno
What I want to do is grab the value of d.jobno to the left of the dash ("-") character. Can I do this with charindex or something?
Thanks!
March 2, 2011 at 8:12 am
Yeap.
declare @OddBallKey varchar(10) = '1234-01'
select SUBSTRING(@OddBallKey, 0, charindex('-', @OddBallKey, 0))
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 2, 2011 at 8:19 am
Thanks for the quick response. The problem is, I am doing a join on a table that has numbers like
1000-01
1001-01
1002-01
etc.
and I want to join to a table where the values are correspondingly
1000
1001
1002
So, I hoped I could just grab in my left outer join statement something to grab the value of the field prior to the dash...if that makes sense. If there were all the same length, I could do use the left and a number, but that isn't the case.
March 2, 2011 at 8:22 am
That is what i showed you. 😀
Try this to see if it makes it more clear.
select *
from table A
join table b on a.ID = SUBSTRING(b.IDFieldWithExtra, 0, charindex('-', b.IDFieldWithExtra, 0))
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 2, 2011 at 8:44 am
Another option is to use the LIKE operator. I don't know which will perform better.
SELECT *
FROM TableA
INNER JOIN TableB
ON TableA.ID LIKE Cast(TableB.ID as varchar(11)) + '%'
This assumes that the '1001-01' from TableA is stored as a string and the 1001 from TableB is stored as an int. If they are both strings, you don't need the CAST.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
March 2, 2011 at 9:04 am
I've got - thanks for the awesome assistance!
March 2, 2011 at 12:14 pm
drew.allen (3/2/2011)
Another option is to use the LIKE operator. I don't know which will perform better.
SELECT *
FROM TableA
INNER JOIN TableB
ON TableA.ID LIKE Cast(TableB.ID as varchar(11)) + '%'
This assumes that the '1001-01' from TableA is stored as a string and the 1001 from TableB is stored as an int. If they are both strings, you don't need the CAST.
Drew
Oh... be careful, now. What's going to happen if one string is 1001-01 and another is 10011-01?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 2, 2011 at 12:33 pm
Jeff Moden (3/2/2011)
drew.allen (3/2/2011)
Another option is to use the LIKE operator. I don't know which will perform better.
SELECT *
FROM TableA
INNER JOIN TableB
ON TableA.ID LIKE Cast(TableB.ID as varchar(11)) + '%'
This assumes that the '1001-01' from TableA is stored as a string and the 1001 from TableB is stored as an int. If they are both strings, you don't need the CAST.
Drew
Oh... be careful, now. What's going to happen if one string is 1001-01 and another is 10011-01?
And of course performance was pretty much tossed out the window when the join forces a full scan anyway.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 2, 2011 at 3:17 pm
Jeff Moden (3/2/2011)
drew.allen (3/2/2011)
Another option is to use the LIKE operator. I don't know which will perform better.
SELECT *
FROM TableA
INNER JOIN TableB
ON TableA.ID LIKE Cast(TableB.ID as varchar(11)) + '%'
This assumes that the '1001-01' from TableA is stored as a string and the 1001 from TableB is stored as an int. If they are both strings, you don't need the CAST.
Drew
Oh... be careful, now. What's going to happen if one string is 1001-01 and another is 10011-01?
Yes, I thought about that after posting, but haven't had a chance to revise the post. You should concatenate '-%' instead of just '%'.
As for the join causing a full scan, I think that both the Cast and the Substring will have the same effect. If the IDs are both stored as char, the LIKE method may be able to use an index.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply