Teradata - Comparing Varchar to decimal - sql

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.

Related

Issue With SQL Pivot Function

I have a SQL query where I am trying to replace null results with zero. My code is producing an error
[1]: ORA-00923: FROM keyword not found where expected
I am using an Oracle Database.
Select service_sub_type_descr,
nvl('Single-occupancy',0) as 'Single-occupancy',
nvl('Multi-occupancy',0) as 'Multi-occupancy'
From
(select s.service_sub_type_descr as service_sub_type_descr, ch.claim_id,nvl(ci.item_paid_amt,0) as item_paid_amt
from table_1 ch, table_" ci, table_3 s, table_4 ppd
where ch.claim_id = ci.claim_id and ci.service_type_id = s.service_type_id
and ci.service_sub_type_id = s.service_sub_type_id and ch.policy_no = ppd.policy_no)
Pivot (
count(distinct claim_id), sum(item_paid_amt) as paid_amount For service_sub_type_descr IN ('Single-occupancy', 'Multi-occupancy')
)
This expression:
nvl('Single-occupancy',0) as 'Single-occupancy',
is using an Oracle bespoke function to say: If the value of the string Single-occupancy' is not null then return the number 0.
That logic doesn't really make sense. The string value is never null. And, the return value is sometimes a string and sometimes a number. This should generate a type-conversion error, because the first value cannot be converted to a number.
I think you intend:
coalesce("Single-occupancy", 0) as "Single-occupancy",
The double quotes are used to quote identifiers, so this refers to the column called Single-occupancy.
All that said, fix your data model. Don't have identifiers that need to be quoted. You might not have control in the source data but you definitely have control within your query:
coalesce("Single-occupancy", 0) as Single_occupancy,
EDIT:
Just write the query using conditional aggregation and proper JOINs:
select s.service_sub_type_descr, ch.claim_id,
sum(case when service_sub_type_descr = 'Single-occupancy' then item_paid_amt else 0 end) as single_occupancy,
sum(case when service_sub_type_descr = 'Multi-occupancy' then item_paid_amt else 0 end) as multi_occupancy
from table_1 ch join
table_" ci
on ch.claim_id = ci.claim_id join
table_3 s
on ci.service_type_id = s.service_type_id join
table_4 ppd
on ch.policy_no = ppd.policy_no
group by s.service_sub_type_descr, ch.claim_id;
Much simpler in my opinion.
for column aliases, you have to use double quotes !
don't use
as 'Single-occupancy'
but :
as "Single-occupancy",

SQL - Error converting data type varchar to numeric

I have data in a table that has decimals and stored as a varchar type data. I need to place that data into another table with column that is data type of decimal(18,5)
I get error:
Error converting data type varchar to numeric
The problem is with this Share column
Query I have is
SELECT IFS.Bin,CFL.CUSIP,IFS.[FROM],
--IFS.Shares
--SUM(isnull(cast(IFS.Shares as money),0))
--CAST(isnull(IFS.Shares,0) AS VARCHAR(30))
Shares =
Convert(
DECIMAL(18,5),
CASE
WHEN IFS.Shares LIKE '%[^0-9]%' THEN IFS.Shares --NULL
ELSE IFS.Shares
END)
FROM
mfclearing.ICE.ImportFileStaging IFS INNER JOIN
mfclearing.Production.ClearingFundList CFL ON
IFS.[From] = CFL.NasdaqSymbol
So if I try to only use IFS.Shares that doesn't work, I was trying some other SUM and CAST , but the last thing is that I'm trying to do is that Shares = Convert .....
Should not be too relevant to this, but I do want to do an insert into and so I do have an insert statement right above that Select statement
INSERT INTO [InterclassExchangeBatchDetails](Bin,FromCusip,FromSymbol,Shares)
The data that I'm working with for reference that is causing the problem, here is a sample of it
521.92100000000005
9906.8510000000006
542.529
1043.8409999999999
3129.0839999999998
5285.4120000000003
104.367
126.98
332.02499999999998
530.12300000000005
575.57799999999997
895.56899999999996
1052.9349999999999
1167.0619999999999
1180.9939999999999
1630.8030000000001
247.232
2136.2040000000002
667.95500000000004
947.78599999999994
148.36000000000001
223.994
238.42699999999999
255.25700000000001
257.56999999999999
259.70600000000002
317.90199999999999
317.90199999999999
317.90199999999999
360.59199999999998
366.84399999999999
374.35000000000002
376.90199999999999
393.11500000000001
397.37200000000001
399.47699999999998
449.72699999999998
463.60899999999998
474.68599999999998
488.11599999999999
491.245
504.67399999999998
509.97899999999998
530.47199999999998
535.93299999999999
537.69799999999998
549.40599999999995
552.41700000000003
581.32600000000002
608.05100000000004
Just use TRY_CONVERT():
Shares = TRY_CONVERT(DECIMAL(18,5), IFS.Shares)

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

ORA-00933 SQL command not properly ended

I need to convert MSSQL query to Oracle but end up with SQL command not properly ended.
Here is MSSQL query
SELECT * FROM [dbo].[trade] AS [Extent1]
WHERE EXISTS (
SELECT 1 AS [C1] FROM
[dbo].[findetail] AS [Extent2]
INNER JOIN [dbo].[transact] AS [Extent3] ON [Extent2].[transact] = [Extent3].[transact]
WHERE [Extent1].[trade] = [Extent2].[trade]
AND 'ACCR' = [Extent3].[subledger]
AND [Extent3].[date] = '2016-03-18T00:00:00'
)
Converting it to Oracle SQL I end with this.
SELECT * FROM trade Extent1
WHERE EXISTS
(SELECT 1 C1 FROM findetail Extent2
JOIN transact Extent3
ON Extent2.transact=Extent3.transact
WHERE Extent1.trade=Extent2.trade
AND 'ACCR'=Extent3.subledger
AND Extent3.date='2016-03-18T00:00:00'
);
and receive error above.
Date formats are different in Oracle. Perhaps something like this:
SELECT *
FROM trade Extent1
WHERE EXISTS (SELECT 1
FROM findetail Extent2 JOIN
transact Extent3
ON Extent2.transact = Extent3.transact
WHERE Extent1.trade = Extent2.trade AND
Extent3.subledger = 'ACCR' AND
Extent3."date" = DATE '2016-03-18'
);
DATE is a reserved word so needs to the surrounded in double quotes and, I am assuming that it is of DATE data type so you will probably need to convert the string:
SELECT *
FROM trade t
WHERE EXISTS (
SELECT 1
FROM findetail f
JOIN transact r
ON f.transact = r.transact
WHERE t.trade = f.trade
AND 'ACCR' = r.subledger
AND r."DATE" = TO_DATE( '2016-03-18T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS' )
);
If you just use the string in r."DATE" = '2016-03-18T00:00:00' then Oracle will implicitly try to convert the string literal using the TO_DATE() function with the NLS_DATE_FORMAT session parameter as the format mask. If they match then it will work but this is a client variable so can be changed and then the query will break without the code having changed (and be a pain to debug). The simple answer is to ensure that you compare date value by either using TO_DATE() and specifying the format mask (as per the query above) or to use an ANSI date literal DATE '2016-03-18' (which is independent of the NLS settings).

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.