Home > Software design >  Union Different Field type as String
Union Different Field type as String

Time:02-27

I have 2 tables in GCP BigQuery Table.

Table 1 has 4 Attributes

Name (STRING)
ID (INTEGER)
Org Num (STRING)
Country Code (INTEGER)

Table 2 also have 4 Attributes

Name (STRING)
ID (STRING)
Org Num (INTEGER)
Country Code (STRING)

Now I am trying to merge or UNION these 2 tables' values, so I am trying like this -

SELECT * FROM 'project1.Dataset1.Table1' 
UNION ALL
SELECT * FROM 'project1.Dataset1.Table2' 

But it returns be error cause in these 2 tables the fields are different types. Do anyone knows if there is any way I can query these 2 tables and save all values as STRING type?

CodePudding user response:

SELECT name,
       CAST(id AS STRING) AS id,
       [Org Num],
       CAST([Country Code] AS STRING) AS [Country Code]
FROM 'project1.Dataset1.Table1' 
UNION ALL
SELECT name,
       id,
       CAST([Org Num] AS STRING) AS [Org Num],
       [Country Code] 
FROM 'project1.Dataset1.Table2' 

CodePudding user response:

Just cast the integer columns to strings in your query

  • Related