Upper to Lowercase conversion in SQL Server - sql

I have the below columns in from 2 different tables -
DimTeamProject.ProjectNodeGUID DimIteration.ProjectGUID
------------------------------ ------------------------
FAE8B08E-286E-487D-B1C1-011853028CDB fae8b08e-286e-487d-b1c1-011853028cdb
I was trying a join operation while matching the case. It gave me an error like
Conversion failed when converting from a character string to uniqueidentifier.
The query I was trying was -
select
p.ProjectNodeName, i.IterationName
from
DimTeamProject p, DimIteration i
where
(p.ProjectNodeGUID) = UPPER(i.ProjectGUID)
I tried the "char" and "cast" function too but without success. Please help.
DimIteration.ProjectGUID is an "nvarchar" & DimTeamProject.ProjectNodeGUID is an "uniqueidentifer"

Just cast the appropriate side as a uniqueidentifier:
select p.ProjectNodeName, i.IterationName
from DimTeamProject p
inner join DimIteration i on p.ProjectNodeGUID =
CAST(i.ProjectGUID as uniqueidentifier)
See the demo with a SQL Fiddle.

Related

SQL INNER JOIN syntax Error BigQuery // New to SQL

I'm getting this error "No matching signature for operator = for argument types: INT64, STRING. Supported signature: ANY = ANY at [9:6]
" while trying to JOIN two table in BQ, Login ID column is listed in the two tables but BQ shows that there's an error on line 9enter image description here
SELECT
performance.name,
performance.ahtdn,
tnps.tnps,
FROM
`data-exploration-2023.jan_scorecard_2023.performance-jan-2023` AS performance
LEFT JOIN
`data-exploration-2023.jan_scorecard_2023.tnps-jan-2023` AS tnps
ON performance.login_id = tnps.login_id
I've checked the syntax for INNER JOIN online and on BQ documentation but I couldn't find the reason why I get this error.
The error should be due to trying to join INT64 and STRING column. It should be resolved casting the numeric column to text one like this:
CAST(performance.login_id AS STRING) = tnps.login_id
or cast the second one if it is the numeric.

How to Join (equal) two data columns that belongs to the String and Double types respectively in Alibaba MaxCompute?

I am not authorized to share the table details.
For instance, let me consider an Example:
I am trying to join two columns of String and Double data types respectively in Alibaba MaxCompute.
In the earlier version of MaxCompute, the String and Double data types are converted to the bigint data type at the cost of precision. 1.1 = “1” in a Join condition.
Whereas the same code does not work in the new version of the MaxCompute. The code syntax is like follows:
SELECT * FROM t1 JOIN t2 ON t1.double_value = t2.string_value;
Error:
WARNING:[1,48] implicit conversion from STRING to DOUBLE, potential data loss, use CAST function to suppress
What is the correct syntax to do the join operation in Alibaba MaxCompute V2?
I did a bit of digging and it seems like this SQL command is the recommended way of getting around this issue.
select * from t1 join t2 on t.double_value = cast(t2.string_value as double);
As the error message suggests:
SELECT *
FROM t1 JOIN
t2
ON CAST(t1.double_value as string) = t2.string_value;

Teradata - Comparing Varchar to decimal

I am very new to Teradata and SQL in general. I need to create a table by combining data from three tables. I was able to successfully join two of them. I am not able to write the joining condition for the third table properly. Here is the code:
select s.cola, s.colb,
t.colc, t.cold,
u.cole, u.colf, u.colg, u.colh, u.coli, u.colj, u.colk, u.coll
from table1 s
inner join table2 t
on s.colb = t.colc
inner join table3 u
on t.cold = cast(u.colm as decimal)
order by 3
where substr(cast(s.cola as varchar(10)),6,2) = 11 and substr(cast(s.cola as varchar(10)),1,4) = 2017 and substr(cast(s.cola as varchar(10)),9,2) between 06 and 10
The error I am getting is:
[Teradata Database] [2620] The format or data contains a bad character.
I think the problem is with the line: on t.cold = cast(u.colm as decimal). The u.colm is of type VARCHAR(50) while t.cold is of type DECIMAL(10, 0). I believe I have casted it properly. Please help.Thanks in advance.
There's some bad data in u.colm.
Depending on your Teradata release you can check it using
WHERE u.colm > '' AND TRYCAST(u.colm as decimal(10,0)) ISNULL
or
WHERE u.colm > '' AND TO_NUMBER(u.colm) IS NULL
You can also use those in the join-condition, e.g.
on t.cold = trycast(u.colm as decimal(10,0))
Don't forget to add the precision of the decimal, as it defaults to (5,0).
Your WHERE_condition is strange, what's the datatype of s.cola?
Seems it's a string with a date yyyy-mm-dd in it. Try
WHERE trycast(s.cola as date) between date '2017-11-06' and date '2017-11-10'
Finally the ORDER BY should be placed after WHERE.

Modifying column datatypes for select view

I have a relatively simple query:
SELECT ATTR.*
FROM NDC_ATTR ATTR
INNER JOIN CONTRACT_NDC_BRG BRG
ON ATTR.ITEM_ID = BRG.CONTRACT_NUM_VAL AND BRG.CONTRACT_NUM_VAL LIKE '%'+#CONTRACT_NUMBER+'%'
The JOIN is causing data type issues:
Conversion failed when converting the varchar value 'Testing' to data type int.
How can I change the incoming datatypes during the query so I can fix this issue?
You can use CAST or CONVERT
Using CAST:
SELECT ATTR.*
FROM NDC_ATTR ATTR
INNER JOIN CONTRACT_NDC_BRG BRG
ON Cast(ATTR.ITEM_ID as varchar(500)) = BRG.CONTRACT_NUM_VAL AND BRG.CONTRACT_NUM_VAL LIKE '%'+#CONTRACT_NUMBER+'%'

SQL Server: Conversion failed when converting the varchar value '1,2'

I have a query like this
select
t.tiid, t.employeeid, t.remarks,
dd.DocID, dd.Document, dd.DocuName
from
ti t
inner join
History cth on cth.tiid = t.tiid
inner join
Downloads dd on dd.DocID = cth.DocID
My data in table is like this
History:
DocID DocuName
1,2 abc.dox,def.docx
Downloads
DocID DocuName document
1 abc.docx x3400000efg..
2 def.docx xc445560000...
but when I execute this query, it shows an error:
Conversion failed when converting the varchar value '1,2' to data type int.
The DocID of history is multiple DocID had been combined with comma, So you can not compare the value directly( One value vs Multiple values).
You can check whether the multiple values contain the specify value use CHARINDEX.
To make sure complete matched of sub string,need a delimiter to indicate a single value, otherwise can get wrong result.
For Eample:
CHARINDEX('1,','12,2,3') will be 1, but in fact, there is no 1 in the string.
select
t.tiid,
t.employeeid,
t.remarks,
dd.DocID,
dd.Document,
dd.DocuName
from ti t
inner join History cth on cth.tiid=t.tiid
inner join Downloads dd on CHARINDEX(','+LTRIM(dd.DocID)+',',','+cth.DocID+',')>0
As the error says, you are trying to equate a string with int.You need to convert the int DocID as string and check if it's present in the comma-separated DocID .Something like
SELECT t.tiid,
t.employeeid,
t.remarks,
dd.DocID,
dd.Document,
dd.DocuName
FROM ti t
INNER JOIN History cth ON cth.tiid=t.tiid
INNER JOIN Downloads dd ON CHARINDEX(',' + CAST(dd.DocID AS VARCHAR(10)) + ',',',' + cth.DocID + ',')>0