Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String length mismatch not giving data in another column

I’ve a column location in my table. I’ve used it as a common column to join two tables
1.Factory
2.Inventory
Like
Factory.location = inventory.location
But I’m having a problem..
For example Location in factory table just has 09 where inventory table has 009 doesn’t match but for three digit numbers it’s matching eg: 115 = 115, 999=999. But in the output, I still get 009 but I’m not getting data in another column for the ones which doesn’t have three digits.
Please tell me how to make it 3 digit and join.
I tried putting it as
(Case when
length(factory.location)<3
Then concat(‘0’,factory.location)
else inventory.location
End) as location
in select statement.

Please help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming you’re storing them as varchar for a reason, you could pad them to equalize length before matching. I chose 3 because it sounded like all the locations are 3 characters long. Change as required

lpad(f.location, 3, '0') = lpad(i.location, 3, '0')

If you the values in location columns are all numbers that can be treated as integers, you could also do

f.location::int = i.location::int
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading