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

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

Related

Query value from XML data element

I have a SQL query which returns data from a column. However the data in the column is in XML. I need value inside value element. How can I do so?
Please see my below SQL query.
SELECT
A.[business_line]
FROM
[EU_OTH_REG].[dbo].[TBL_EU_OTH_TXN_REG_RSDS] A
INNER JOIN
[EU_OTH_REG].[dbo].[TBL_EU_OTH_REG_MST_LOOKUP] B ON B.code = A.product_substance_type
The output to this query is
<collection><object parentid="ce57cc75-3966-478f-bf25-5e3abf716f96" parenttype="Object"><fields><field name="code"><value>BL2</value></field></fields></object></collection>
I want the value BL2 from this. The data is inside the value element. Can anyone help how to do so? I tried using stuff but I wasn't able to get the value.
XML.value() does the trick as follows:
declare #Test xml = '<collection><object parentid="ce57cc75-3966-478f-bf25-5e3abf716f96" parenttype="Object"><fields><field name="code"><value>BL2</value></field></fields></object></collection>';
select #Test.value('(/collection/object/fields/field/value)[1]', 'varchar(3)' )
Returns:
BL2
So add that to your query:
select A.[business_line].value('(/collection/object/fields/field/value)[1]', 'varchar(3)'
from [EU_OTH_REG].[dbo].[TBL_EU_OTH_TXN_REG_RSDS] A
inner join [EU_OTH_REG].[dbo].[TBL_EU_OTH_REG_MST_LOOKUP] B
on B.code = A.product_substance_type
As you have indicated that A.[business_line] is not of XML datatype, just convert it before calling value:
convert(xml, A.[business_line]).value
Note: I have assumed that its only ever a 3 character result i.e. varchar(3) but if not adjust to suit.

Pentaho CDE SQL Query with numerical param

I would like to know if it is possible to perform an SQL query and pass it a numerical parameter. Let's suppose that I have the following query:
SELECT
CONCAT(cuatrimestre,' Cuatrimestre') As quarter
, ROUND(SUM(fact_Ventas.cantidad * precioUnitario),0) as amount
FROM fact_Ventas
INNER JOIN dim_Tiempos ON
fact_Ventas.idAnio = dim_Tiempos.idAnio AND
fact_Ventas.idMes = dim_Tiempos.idMes AND
fact_Ventas.idDia = dim_Tiempos.idDia
INNER JOIN dim_Clientes ON dim_Clientes.idCliente = fact_Ventas.idCliente
INNER JOIN dim_Productos ON dim_Productos.idProducto = fact_Ventas.idProducto
WHERE
CAST(fact_Ventas.idAnio As Char) LIKE ${paramAnio} AND
CAST(fact_Ventas.idMes As Char) LIKE ${paramMes} AND
CAST(fact_Ventas.idVendedor As Char) LIKE ${paramVendedores} AND
CAST(fact_Ventas.origen As Char) LIKE ${paramOrigen} AND
dim_Productos.marca LIKE ${paramMarca} AND
dim_Clientes.segmentoCliente LIKE ${paramSegmento}
GROUP BY 1
ORDER BY 1
I want to divide the column amount, by a numerical value extracted from a simple parameter. I managed to use filters in the where clause, but I can not make a division in a column.
Try the following query:
SELECT
CONCAT(cuatrimestre,' Cuatrimestre') As cuatrimestre
, ROUND( (SUM(fact_Ventas.cantidad * precioUnitario)/${paramValue}),0) as Importe
FROM fact_Ventas
INNER JOIN dim_Tiempos ON
fact_Ventas.idAnio = dim_Tiempos.idAnio AND
fact_Ventas.idMes = dim_Tiempos.idMes AND
fact_Ventas.idDia = dim_Tiempos.idDia
INNER JOIN dim_Clientes ON dim_Clientes.idCliente = fact_Ventas.idCliente
INNER JOIN dim_Productos ON dim_Productos.idProducto = fact_Ventas.idProducto
WHERE
CAST(fact_Ventas.idAnio As Char) LIKE ${paramAnio} AND
CAST(fact_Ventas.idMes As Char) LIKE ${paramMes} AND
CAST(fact_Ventas.idVendedor As Char) LIKE ${paramVendedores} AND
CAST(fact_Ventas.origen As Char) LIKE ${paramOrigen} AND
dim_Productos.marca LIKE ${paramMarca} AND
dim_Clientes.segmentoCliente LIKE ${paramSegmento}
GROUP BY 1
ORDER BY 1
But it gives an error and the data is not loaded. The syntax of the query was tested in the database and is correct.
Did you set the parameter type to Numeric? It defaults to String which was probably fine for your other parameters.
The problem was that I forgot to assign the parameter to the component, I had associated it with SQL Query but not with the component.

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.

SQL Joining from 2 fields to a concatenated field

I have a quote ID field in a table (PING) that has the following format: "XX" (which is just a lead off to the quote number) followed by the USERID "YYYY" and then the Cart_ID "ZZZZ". Both of the second fields come from another table (Cart). I need to join Cart to the first table. These 2 fields are the only way to join them, but I can't figure out how to join 2 fields to 1 field from another table. Obviously code below won't work, but is for illustration.
join PROD..CART on Cart.CART_ID and Cart.USERID = CW_PING.QuoteID
Try this:
join PROD..CART on CONCAT(Cart.CART_ID,Cart.USERID) = CW_PING.QuoteID
This was the final solution:
join PROD_QQ..CART on 'QQ' + (right('0000000000' + CAST (Cart.USERID as varchar),4) + (right( '0000000000' + CAST (Cart.CART_ID as varchar),6)))
try:
join PROD..CART on [Cart.CART_ID + Cart.USERID] = CW_PING.QuoteID

Upper to Lowercase conversion in SQL Server

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.