#Teradata error expected something like ')' between ';' and the end of the request - request

REPLACE MACRO GEO_MATCHED (REGION INTEGER) AS (
INSERT INTO DLA_v.GEO_FULL_2019_MATCHED
SELECT
Poly.Project_ID AS "Project_ID",
Poly.WIKIMAPIA_ID AS "POLY_ID_WIKIMAPIA",
Poly.NAME AS "POLY_NAME",
Fast.REVENUE_YTD,
Fast.JOB_VERTICAL,
Poly.geometry.ST_Contains(Fast.geometry) AS "INSIDE"
FROM
(SELECT * FROM DLA_A_19143.geo_jobsite_input_data
WHERE region__c = :REGION)Fast, DLA_D_SLSMKT.GEO_POLYGONS Poly
WHERE
(Poly.geometry.ST_Contains(Fast.geometry) = 1;);
----region with null values
getting this error for this code :
[42000] [Teradata][ODBC Teradata Driver][Teradata Database] Syntax error, expected something like ')' between ';' and the end of the request.
Added the brackets as it has asked to add the bracted between semicolumns and end of request its not working, if any one could help .

Related

Error converting data type nvarchar to numeric. Tried convert and replace

I am trying to add a sum of my forecast using a code
select
sum(cast(replace(replace(ZFREEGOOD, ',', ''), ' ', '') as decimal(22,8)))
from
TEMP_GBR_History_1611
I keep getting an error
Error converting data type nvarchar to numeric.
I used a case statement to figure out which rows failed. These are some of them
Would be of great help to get a solution
You're not handling the blank case, this works with test data:
select sum(cast(case when replace(replace(ZFREEGOOD,',',''),' ','') = '' then '0' else replace(replace(ZFREEGOOD,',',''),' ','') end as decimal(22,8))) from TEMP_GBR_History_1611
One option is to use try_convert(money,...) It tends to be a little more forgiving.
Example
Select AsMoney = try_convert(money,' 27,300') -- works!
,AsInt = try_convert(int,' 27,300')
,AsDec = try_convert(decimal(10,2),' 27,300')
,AsFloat = try_convert(float,' 27,300')
Returns
AsMoney AsInt AsDec AsFloat
27300.00 NULL NULL NULL

What are all values ISNUMERIC function finds as TRUE in sql server?

Below part of my query was failing initially:
WHEN ISNUMERIC(npx.nvcAnswer) = 1
THEN CASE
WHEN ABS(CONVERT(DECIMAL(38,2),npx.nvcAnswer)) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),npx.nvcAnswer))
ELSE npx.nvcAnswer
END
Below was the error we were getting:
com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type nvarchar to numeric.
rootCause=SQLException #1: errorCode: 8114 sqlState: S0005 message: Error converting data type nvarchar to numeric.
I realized that it considers '.' & '-' as NUMERIC too.
So I added "NOT npx.nvcAnswer in( '.' , '-')" to it and it worked.
WHEN ISNUMERIC(npx.nvcAnswer) = 1 AND NOT npx.nvcAnswer in( '.' , '-')
THEN CASE
WHEN ABS(CONVERT(DECIMAL(38,2),npx.nvcAnswer)) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),npx.nvcAnswer))
ELSE npx.nvcAnswer
END
After some day it started failing again with below error.
com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type nvarchar to numeric.
rootCause=SQLException #1: errorCode: 8114 sqlState: S0005 message: Error converting data type nvarchar to numeric.
Then I changed below block and it worked: (But I failed to find what values were causing this issue)
OLD:
WHEN ABS(CONVERT(DECIMAL(38,2),npx.nvcAnswer)) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),npx.nvcAnswer))
NEW:
WHEN ABS(CONVERT(DECIMAL(38,2),PATINDEX('%[^0-9]%', npx.nvcAnswer))) < 1
THEN CONVERT(VARCHAR,CONVERT(DECIMAL(38,2),PATINDEX('%[^0-9]%', npx.nvcAnswer)))
Ask :
What are all values ISNUMERIC function finds as TRUE in sql server?
I will suggest you should your Try_Cast() function instead of using complex logic to check numeric values.
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). Please refer below links for further detail -
https://learn.microsoft.com/en-us/sql/t-sql/functions/isnumeric-transact-sql

dashDB: An unexpected token "IF" was found following " ". Expected tokens may include: "<call>". SQLSTATE=42601

I am trying to pass following query to dashdb:
IF (NOT EXISTS (SELECT * FROM TEST_CONFIGURATION WHERE DEVICEID = 'OZ_POLLUDRON_010'))
BEGIN
INSERT INTO TEST_CONFIGURATION (DEVICEID, DEVICETYPE, SERIAL, TYPE, MACID, CONFIG, INIT) VALUES('OZ_POLLUDRON_010', 'POLLUDRON_PRO', '9428424248', 0, '200050000b51343334363138', '[object Object]', '[object Object]')
END
ELSE
BEGIN
UPDATE TEST_CONFIGURATION SET DEVICEID = 'OZ_POLLUDRON_010', DEVICETYPE = 'POLLUDRON_PRO', SERIAL = '9428424248', TYPE = 0, MACID = '200050000b51343334363138', CONFIG = '[object Object]', INIT = '[object Object]' WHERE DEVICEID = 'OZ_POLLUDRON_010' END
But it is giving me following error:
database/sql/driver: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "IF" was found following " ". Expected tokens may include: "<call>". SQLSTATE=42601
Can anyone please help me out with it?
Consider using the MERGE statement to achieve an upsert action (insert if not exist else update). Examples are in the DB2 samples directory and in the DB2 Knowledge Center.

Query fails on "converting character string to smalldatetime data type"

I've been tasked with fixing some SQL code that doesn't work. The query reads from a view against a predicate. The query right now looks like so.
SELECT TOP (100) Beginn
FROM V_LLAMA_Seminare
//Removal of the following line makes the query successful, keeping it breaks it
where Beginn > (select cast (getdate() as smalldatetime))
order by Beginn desc
When I run the above query, I am greeted with the following error.
Msg 295, Level 16, State 3, Line 1
Conversion failed when converting character string to smalldatetime data type.
I decided to remove the WHERE clause, and now it runs returning 100 rows.
At first, I thought that behind the scenes, SQL Server was somehow including my predicate when bringing back the View . But then I investigated how the View was being created, especially the Beginn field, and at no point does it return a String.
Long story short, the column that becomes the Beginn field is a BIGINT timestamp like 201604201369.... The original user transforms this BIGINT to a smalldatetime using the following magic.
....
CASE WHEN ma.datum_dt = 0
THEN null
ELSE CONVERT(smalldatetime, SUBSTRING(CAST(ma.datum_dt AS varchar(max)),0,5) + '-' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),5,2) + '-' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),7,2) + ' ' +
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),9,2) +':'+
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),11,2) +':' +
RIGHT(CAST(ma.datum_dt AS varchar(max)),2)) END AS Beginn
...
My last attempt at finding the problem was to query the view and run the function ISDATE over the Beginn column and see if it returned a 0 which it never did.
So my question is two fold, "Why does a predicate break something" and two "Where on earth is this string error coming from when the Beginn value is being formed from a BIGINT".
Any help is greatly appreciated.
This problem is culture related...
Try this and then change the first SET LANGUAGE to GERMAN
SET LANGUAGE ENGLISH;
DECLARE #bi BIGINT=20160428001600;
SELECT CASE WHEN #bi = 0
THEN null
ELSE CONVERT(datetime, SUBSTRING(CAST(#bi AS varchar(max)),0,5) + '-' +
SUBSTRING(CAST(#bi AS varchar(max)),5,2) + '-' +
SUBSTRING(CAST(#bi AS varchar(max)),7,2) + ' ' +
SUBSTRING(CAST(#bi AS varchar(max)),9,2) +':'+
SUBSTRING(CAST(#bi AS varchar(max)),11,2) +':' +
RIGHT(CAST(#bi AS varchar(max)),2)) END AS Beginn
It is a very bad habit to think, that date values look the same everywhere (Oh no, my small application will never go international ...)
Try to stick to culture independent formats like ODBC or ISO
EDIT
A very easy solution for you actually was to replace the blank with a "T"
SUBSTRING(CAST(ma.datum_dt AS varchar(max)),7,2) + 'T' +
Then it's ISO 8601 and will convert...
The solution was found after looking through #Shnugo's comment. When I took my query which contained the Bigint->Datetime conversion logic, and put it into a CTE with "TOP 100000000" to avoid any implicit conversion actions, my query worked. Here is what my view looks like now with some unimportant parts omitted.
---Important part---
CREATE VIEW [dbo].[V_SomeView] AS
WITH CTE AS (
SELECT TOP 1000000000 ma.id AS MA_ID,
---Important part---
vko.extkey AS ID_VKO,
vko.text AS Verkaufsorganisation,
fi.f7000 AS MDM_Nr,
vf.f7105 AS SAPKdnr,
CASE WHEN ma.datum_dt = 0 --Conversion logic
CASE WHEN ma.endedatum_dt = 0 --Conversion logic
CONVERT(NVARCHAR(MAX),art.text) AS Art,
.....
FROM [ucrm].[dbo].[CRM_MA] ma,
[ucrm].[dbo].[CRM_fi] fi,
[ucrm].[dbo].[CRM_vf] vf,
[ucrm].[dbo].[CRM_ka] vko,
[ucrm].[dbo].[CRM_ka] art,
[ucrm].[dbo].[CRM_ka] kat
where ma.loskz = 0
and fi.loskz = 0
and vf.loskz = 0
and fi.F7029 = 0
and vf.F7023 = 0
...
GROUP BY ma.id,
vko.extkey,
vko.text,
fi.f7000 ,
vf.f7105,
ma.datum_dt,
ma.endedatum_dt,
....
)
select * FROM CTE;

SQL DB2 sql error INCORRECT SYNTAX

What am I missing in this code below that will cause the error:
Msg 170, level 15, line 113 line 113:
Incorrect syntax near
'actual_completion_date'.
update #Loss_mit_step
set
[STEP924_COMPL_DATE] = Case when step_code ='924' then ls_actual_completion_date else ' ' end,
[STEP926_COMPL_DATE] = Case when step_code ='926' then ls_actual_completion_date else ' ' end,
[STEP927_COMPL_DATE] = Case when step_code ='927' then ls_actual_completion_date else ' ' end,
[STEP928_COMPL_DATE] = Case when step_code ='928' then ls_actual_completion_date else ' ' end,
[APPROVAL_DATE] = Case when step_code ='Q28' then ls_actual_completion_date else ' ' end
What you posted is syntactically correct, so you cannot get a syntax error. It actually parses fine. Your error message mentions actual_completion_date but you have no such token in your post. So obviously you posted an error from a different T-SQL.
You appear to have an extra comma in the statement, at the very end.
I suggest you comment out each of the "case" lines, one by one, until you find out what the problem is, or until you only have one line left.
I found the cause of the error: a space in front of actual_completion_date on the first line.

Resources