4

So I have two columns from two different databases that I would like to link.

Problem is that my first column outputs the numbers with this format "1 789 987" and my second column outputs the data "0000000001789987"

How can I write my WHERE sql forumla to idententify these as matching?

Ok so I pulled out the qrys to excel to provide you with more information. Here are the different tables.

tbl1

tbl2

Looks like Tbl2 has NUM column set to text. And even though the QRY in the program gave spaces to the numbers in Tbl1 it looks like the qry removed them shrug

SELECT *
FROM "Tbl1","Tbl2"
WHERE "Tbl1"."num" = "Tbl2"."num" 
AND "Tbl1"."Date" BETWEEN '2019-01-21' AND '2019-01-25' 

I hope the information became abit clearer. I'm new to SQL and Stackoverflow, i'll try and improve my questions information in the future.

Kalween
  • 101
  • 7

2 Answers2

2

Well, to transform format 1 to format 2 you can try something like this :

set @format1 = "1 789 987";
set @format2 = "0000000001789987";

select LPAD(REPLACE(@format1, ' ', ''), 16, "0") as format1, @format2 as format2

Output is :

====================================
     format1     |     format2
====================================
0000000001789987 | 0000000001789987

This way format1 looks like format2 if you test it. The REPLACE remove the ' ' and the LPAD will fill the string with 0 untill the string is 16 char length like the format2.

So you can use this in you WHERE condition :

...WHERE LPAD(REPLACE(your_first_column, ' ', ''), 16, "0") = your_other_column

Now you can try to transform both column in int too, you didn't provide lot of information about those format so hard to find the best solution !

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
1

This cast may fit for you:

NOTE: tbl1 contains ids like: 1 789 987

select *
from tbl1 join tbl2 on (
                        cast( -- casts to integer
                               replace(tbl1.text_id, ' ', '') -- removes whitespaces
                                 as int) = 
                        tbl2.numeric_id -- join on second table
                        )

In any case please provide sample data and a testable example what you did and the results you need

DDS
  • 2,340
  • 16
  • 34