473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Join zero padded char to varchar

Having a brain fart and can't figure this out...
I have 2 databases I need to join:

db1.customer.customer_no char(15) right justified, zero padded
sample customer numbers:
000000000000001
000000000000010
000000000000234
000000000012345

db2.customer.customer_no varchar(20) left justified, no padding
sample customer numbers:
1
10
234
12345

How do I join tables on customer_no? Use cast, convert? Strip zeroes from
db1 table?

Thanks.
May 31 '08 #1
2 3867
You can do:

1). db1.customer.customer_no = RIGHT('000000000000000' +
db2.customer.customer_no, 15)

The above will no be able to utilize index on db2.customer.customer_no.

2). CAST(CAST(db1.customer.customer_no AS INT) AS VARCHAR(20)) =
db2.customer.customer_no

This one will not use index on db1.customer.customer_no.

You can create a view (or add computed column) to perform the above on one
of the tables, and then index the transformed column.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

May 31 '08 #2
Thank you. Both options work.
"Plamen Ratchev" <Pl****@SQLStudio.comwrote in message
news:DN******************************@speakeasy.ne t...
You can do:

1). db1.customer.customer_no = RIGHT('000000000000000' +
db2.customer.customer_no, 15)

The above will no be able to utilize index on db2.customer.customer_no.

2). CAST(CAST(db1.customer.customer_no AS INT) AS VARCHAR(20)) =
db2.customer.customer_no

This one will not use index on db1.customer.customer_no.

You can create a view (or add computed column) to perform the above on one
of the tables, and then index the transformed column.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 1 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pete | last post by:
hello i am a novice at database design and i'm having trouble doing a multi join in mysql, i was able to do a inner join with just the venues and productions tables below like so SELECT...
3
by: kj | last post by:
When I run the attached query, I get duplicates when there is one to many relationship between tableA and tableB. The query, tested schema and the result is attached. Sorry for the long post. ...
6
by: Rowan | last post by:
Hello, I am having a problem with a join. Either I am just not seeing the obvious, it isn't possible, or I need to use a different approach. I have an application with a vsflexgrid that needs...
6
by: Rowland | last post by:
Hi, I've got a field that stores numeric values, representing a tracking number. I've also got a stored procedure that will extract this field and return it to a client. However, I would like to...
4
by: Andrew S | last post by:
Hello Mr. Expert: - I have 3 tables in mysql in MyISAM table format, I am using mysql4.0 on freebsd5.3 - producttbl, productdetailentbl, pricetblN - they all have "productid" as the Primary KEY....
18
by: Dave | last post by:
Guys I am really stuck on this one. Any help or suggestions would be appreciated. We have a large table which seemed to just hit some kind of threshold. They query is somewhat responsive when...
5
by: ltansey | last post by:
I have a problem concerning a join statement query, the two table are called PublicHouse & PublicHouseBeer, below are the following tables are there attributes; PublicHouse Table create table...
2
by: ltansey | last post by:
Simple Join Statement Help -------------------------------------------------------------------------------- I have a problem concerning a join statement query, the two table are called...
2
by: Artie | last post by:
Having a brain fart and can't figure this out... I have 2 databases I need to join: db1.customer.customer_no char(15) right justified, zero padded sample customer numbers:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.