SQL DB2 sql error INCORRECT SYNTAX - sql-server

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.

Related

Find first instance of space in string

I am trying to return the first 'word' in a string by finding the first instance of a space ' ' in the string field Part_Comment. Examples of strings in the field Part_Comment are:
13088V21 () (FAB)
G16707 (FOLD) ()
16636U01.01
I have tried:
substring(Part_Comment from 1 for position(' ' in Part_Comment)-2) as "AssyNo",
which comes up with an error "Incorrect syntax near the keyword 'from'." But it works fine when I just use Part_Comment by itself.
substring(Part_Comment from 1) as "AssyNo",
Same error as above
left(Part_Comment,10) as "AssyNo",
This works, but I need to use the position function or something else to find the ' ' substring. But apparently the position function returns 0 when more than one instance occurs.
I imagine this is a pretty common thing that users want, so there must be an easy solution.
You can do it with LEFT and POSITION like so:
LEFT(Part_Comment, POSITION(' ' in Part_Comment)-1))
EDIT
as #Arioch 'The in comment suggested, safety need to be implemented
CASE POSITION(' ' in Part_Comment)
WHEN 0 THEN 'Part_Comment'
ELSE LEFT(Part_Comment, POSITION(' ' in Part_Comment)-1)
END

Msg 102, Level 15, State 1 Incorrect syntax near '>'

I have a SQL Server Procedure like this:
//Beginning of procedure
SELECT
CASE IDNumber
WHEN (DATALENGTH(IDNumber)>7)
THEN SUBSTRING(IDNumber,0,6)
WHEN (DATALENGTH(IDNumber) < 7)
THEN CONCAT((REPLICATE(0,7-LEN(IDNumber)),IDNumber)
END AS NID
//Rest of the procedure here
the code on execution throws the error Incorrect syntax near '>'. on line WHEN (DATALENGTH(IDNumber)>7) .
IDNumber is a nvarchar. I tried using LEN(IDNumber) but in vain.
I don't know what the error is!
Rewrite your query like that:
SELECT CASE
WHEN DATALENGTH(IDNumber) > 7 THEN SUBSTRING(IDNumber, 0, 6)
WHEN DATALENGTH(IDNumber) < 7 THEN CONCAT(REPLICATE(0, LEN(IDNumber)), IDNumber)
END AS NID;
When you write CASE Column, you have to compare it to direct values, not to an expression.
If you have case variable then you need to provide values to When part, not the condition

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;

having issues with single quotes using like 'string token' in execute immediate statement

Here is the section of code, I am using ' ' blah ' ' to escape single quotes but I guess its not working:
declare
my_func varchar2(20) :='test_func';
begin
execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func || ' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' ' ';
end;
I am getting the following error:
PLS-00103: Encountered the symbol "VALIDATION1_%" when expecting one of the following:
& = - + ; < / > at in is mod remainder not rem return
returning <an exponent (**)> <> or != or ~= >= <= <> and or
like like2 like4 likec between into using || bulk member
submultiset
The symbol "* was inserted before "VALIDATION1_%" to continue.
It looks like you are trying to escape the single quote with another single quote (which is good), but there is an extra space in between the two. That has to go.
Change
' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' ' '
to
' from dual where TABLE_TEST.FUNCTION_NAME like ''VALIDATION1_%'' '
In cases like this it's much simpler to use the new q syntax for literal strings, e.g.:
execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func ||
q[' from dual where TABLE_TEST.FUNCTION_NAME like 'VALIDATION1_%' ]';

Raiseerror and Concat for the Message

I'd like to do something like this
raiserror(concat('Error in case #isFishy =', #isFishy, ' #isSmarmy=', #isSmarmy, ' #isTasty = ', #isTasty), 10, 1)
--or
raiserror('Error in case #isFishy =' + #isFishy + ' #isSmarmy=' + #isSmarmy + ' #isTasty = ' + #isTasty, 10, 1)
But it just isn't working. How do I accomplish this? I'm in SQL Server 2005.
The error message in RAISERROR has actually similar syntax to printf function in C, so assuming your arguments are of the type of integer you would need to use:
raiserror(N'Error in case #isFishy = %d #isSmarmy = %d #isTasty = %d',10,1,#isFishy,#isSmarmy,#isTasty)
check out BOL for details and other options
I use raiserror a lot. We have some stored procedures that are called from a .Net app each night for batch processing, and the .Net app wants to log the procedure output this way. I don't know why, but I generally have to build the string before calling raiserror.

Resources