Converting error when try to UNION ALL temporary tables in SQL - sql

I created a few temporary tables which I need to union using UNION ALL. This is how it looks in my code:
SELECT X, Y, Z
INTO #Tab1
FROM dbo.Cust
SELECT X, Y, Z
INTO #Tab2
FROM dbo.Cars
SELECT X, Y, Z
INTO #Tab3
FROM dbo.Colors
SELECT * FROM #Tab1
UNION ALL
SELECT * FROM #Tab2
UNION ALL
SELECT * FROM #Tab3
Unfortunately, I got an error:
Conversion failed when converting the nvarchar value .. to data type
int
However, when I try to union these tables but not as temporary ones, then no error arises. So, the solution below works fine and output is returned properly:
SELECT X, Y, Z
FROM dbo.Cust
UNION ALL
SELECT X, Y, Z
FROM dbo.Cars
UNION ALL
SELECT X, Y, Z
FROM dbo.Colors
Columns X, Y, Z are of the same type as in temp table. I guess there must be an issue with temp tables but don't know what exactly. Where is the problem and why it happens?

Related

Condition statement in a select

Here's what i wanna do
Select X, Y, if Z IS NULL THEN ( select something ) else Z
Basically I want to select the 'Z' if it's null I want to select another value, can someone please suggest a code example with a case or something that I can follow through it ?
Select X, Y, NVL(Z, showThis) as Z
will return showThis if Z is null in ORACLE
Select X, Y, ISNULL(Z, showThis) as Z
will return showThis if Z is null in SQL-SERVER
Do you want coalesce()?
Select X, Y,
coalesce(z, <something else>) as z
Choose with CASE:
select
X, Y,
case
when Z is null then (select something)
else (select something else)
end as col
from tablename

SQLite: Combining 'WHERE' conditions

A question for my general understanding:
If I have:
SELECT X, Y, Z FROM MyTable
and I want only entries where none of the values are null I have to use
SELECT X, Y, Z FROM MyTable
WHERE X IS NOT NULL AND Y IS NOT NULL AND Z IS NOT NULL
Is there a shorter option? Something like
SELECT X, Y, Z FROM MyTable
WHERE X, Y, Z IS NOT NULL
This is just a short example, but I guess in queries with many conditions and requests something like this could make the query string much shorter and more readable.
There is no such shorter form than below query.
SELECT X, Y, Z
FROM MyTable
WHERE X IS NOT NULL AND Y IS NOT NULL AND Z IS NOT NULL

Select into #TempTable

I have something like the following scenario
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
I would like to put the results into a #TempTable and then create a Pivot of the results into #TempTable2
I have tried
SELECT * INTO #TempTable FROM (
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
)
But it get Incorrect syntax near ')'
I've forgotten all the other variations I have made but none of them have worked
Add an alias to the derived table. Here I use X because I'm imaginative
SELECT *
INTO #TempTable
FROM
(
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
) AS X
SQL Server needs a reference for the objects in the FROM clause. No alias = no reference
You can see this if we rewrite the query using a CTE
WITH myUnion AS
(
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
)
SELECT *
INTO #TempTable
FROM myUnion

create implicit data without having to create temp/volatile/working table

This is possible:
SELECT 'Bla' AS X
why is this not possible in TeraData:
SELECT 'Bla' AS X
UNION
SELECT 'DiBla' AS X
Is there a way to achieve the above without having to create a temp/volatile/working table in TeraData?
PS:
The error is: A select for a union, intersect or minus must reference a table
If you want two columns on one row, then use:
SELECT 'Bla' AS X, 'DiBla' AS Y
If you want:
X
Bla
DiBla
Then you just do:
select 'Bla' as X
union all
select 'DiBlah' as X;
If you want:
X Y
Bla NULL
NULL DiBla
Then:
SELECT 'Bla' as X, NULL as Y
UNION ALL
SELECT NULL as X, 'DiBla' as Y
You have a attribute name mishmash in your UNION. You can not perform UNION between relations having different structure. Therefore, use
SELECT cast('Bla' as varchar(6)) AS X FROM (SELECT 1 a) t
UNION
SELECT cast('DiBla' as varchar(6)) AS X FROM (SELECT 1 a) t
the explicit casting make sure that the data types are equivalent as well as the attribute names. Another solution could be
SELECT * FROM
(
SELECT cast('Bla' as varchar(6)) AS X
) t
UNION
SELECT * FROM
(
SELECT cast('DiBla' as varchar(6)) AS X
) t

How do I write a query in google bigquery to infer the data type of a column?

I have a table with all string columns, but I know certain columns are numbers (or dates). Is there a built in function in BigQuery to infer the data type of individual columns? Something like select is_string(column_name) from table_name?
One idea that comes to mind is using SAFE_CAST in combination with LOGICAL_AND, e.g.:
#standardSQL
WITH T AS (
SELECT '2017-05-01' AS x, '3.14' AS y, '5' AS z UNION ALL
SELECT '2017-03-02' AS x, '1.59' AS y, '-1' AS z UNION ALL
SELECT NULL AS x, NULL AS y, NULL AS z
)
SELECT
LOGICAL_AND(x IS NULL OR SAFE_CAST(x AS DATE) IS NOT NULL) AS x_is_date,
LOGICAL_AND(y IS NULL OR SAFE_CAST(y AS FLOAT64) IS NOT NULL) AS y_is_float64,
LOGICAL_AND(z IS NULL OR SAFE_CAST(z AS TIMESTAMP) IS NOT NULL) AS z_is_timestamp
FROM T;
This results in true, true, and false (the z values are not timestamps). If you want to reuse the same expression multiple times, you can make this a little less verbose with a SQL UDF:
#standardSQL
CREATE TEMP FUNCTION IsDate(x STRING) AS (
x IS NULL OR SAFE_CAST(x AS DATE) IS NOT NULL
);
WITH T AS (
SELECT '2017-05-01' AS x, '3.14' AS y, '5' AS z UNION ALL
SELECT '2017-03-02' AS x, '1.59' AS y, '-1' AS z UNION ALL
SELECT NULL AS x, NULL AS y, NULL AS z
)
SELECT
LOGICAL_AND(IsDate(x)) AS x_is_date,
LOGICAL_AND(IsDate(y)) AS y_is_date,
LOGICAL_AND(IsDate(z)) AS z_is_date
FROM T;
This results in true, false, false, since only x has values in date format.