Orace SQL Case WHEN Statement "missing keyword" - sql

I am using BI Publisher with Oracle Fusion 19c
I am trying to look at a 1000 word VARCHAR field (POH.NOTE_TO_VENDOR), then if it contains the word Hearing and the character after it (first part of the substr) are not blank ie a date exist. Then I want to convert the whole substr to a date other wise leave blank.
I can get the concatenated substr to work and display it as a string either as a date or if its not there then the value is '//' but cannot convert directly because if the value return '//' it wont convert this to date.
, CASE WHEN SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+14),2)IS NOT NULL
THEN (TO_DATE((SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+17),2)||'/'||SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+14),2)||'/'||SUBSTR(POH.NOTE_TO_VENDOR, (INSTR(POH.NOTE_TO_VENDOR,'Hearing: ')+11),2)),'DD/MM/YY'))
ELSE
END
This code returns the error missing keyword. I want it to either convert the string to date or return a blank value.
Tried with an else condition without else and as it is
I have also tried Case when......then ‘1’ else ‘0’ which returns 1 and 0s correctly

The problem lies in this chaos of parentheses.
I removed all the unnecessary parentheses which make the code hard to read and hard to spot the syntactical errors:
, CASE WHEN SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 14, 2) IS NOT NULL
THEN TO_DATE(
SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 17, 2)
|| '/' ||
SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 14, 2)
|| '/' ||
SUBSTR(POH.NOTE_TO_VENDOR, INSTR(POH.NOTE_TO_VENDOR,'Hearing: ') + 11, 2),
'DD/MM/YY'
)
END
I also removed ELSE which is not needed if you only need NULL in that case.

Related

How to get the month of a date with two digits in SQL-92?

I have a date format cell in my table, of which I need to extract the month in two digits. This means that 6-4-2021 should return 04. The main problem is that this needs to be in the syntax of SQL-92. Any ideas on how to do this?
SQL-92
substring(
'0' || substring(<column> from position('-' in <column>) + 1 for 3)
from position(
'-' in
'0' || substring(<column> from position('-' in <column>) + 1 for 3)
) - 2
for 2
)
I had wondered if you're were really looking for ODBC functions. If so then locate() would be the equivalent for position(), noting that the argument syntax just uses comma separators. replace() would then also be available for an alternate approach.
Looking at some reference material for your tool I would translate it this way though it does appears that there's even a dayOfMonth() function that might make this all even simpler.
SAP BusinessObjects SQL for multisource-enabled universes
substring(
'0' || substring(<column>, pos('-', <column>) + 1, 3),
pos('-',
'0' || substring(<column>, pos('-', <column>) + 1, 3)
) - 2,
2
)
https://help.sap.com/doc/4667b9486e041014910aba7db0e91070/4.2.4/en-US/sbo42sp4_info_design_tool_en.pdf
See it in action with PostgreSQL: http://sqlfiddle.com/#!17/9eecb/72962
SAP BusinessObjects SQL for multisource-enabled universes
substring(concat('0', toString(month(<date>))), length(concat('0', toString(month(<date>))))-1, 2)
I put a 0 in front of my month number converted to a string, and took the last two digits of the string.

CAST(somenumber as VARCHAR(10)) + somestring returns Error converting data type varchar to numeric

I'm trying to combine a min and max salary with a hyphen for display in a report. I feel like I've done this a hundred times but I still can't figure out why I'm getting the "Error converting data type varchar to numeric" especially since I'm trying to convert a numeric to varchar not the other way around.
amt_exp_formatted = CASE
WHEN a.class_code IN ('9997','9998','9999') THEN 0
--ELSE CAST(e.min_sal as VARCHAR(10)) + '-' + CAST(e.max_sal as VARCHAR(10))
ELSE CONVERT(VARCHAR(1), 1) + CONVERT(VARCHAR(1), 'a')
END
In the above example if I use 'a' I get the error. If I change 'a' to '9' then it works AND it appends 9 to the 1 (i.e. 19) rather than adding them together which is exactly what I want but with a character string. I've tried both CAST and CONVERT with no luck. I know I shouldn't need the second CONVERT on the letter 'a' but I was just trying to force the data types to be the same just in case. I have no doubt at all I'm missing something simple but I can't seem to get it so I figured it wouldn't hurt to ask the community.
Thank you!
A case expression returns one value with a determinate type. If any returned fork returns a number, then number it is. And strings get converted.
So, be sure you are returning a string in all cases:
(CASE WHEN a.class_code IN ('9997', '9998', '9999')
THEN '0'
ELSE CONCAT(e.min_sal, '-', e.max_sal)
END)
CONCAT() automatically converts the arguments to strings.
This is becuase you are mixing the data type. in the first part of case you were returning 0 while in the else part you're converting it to varchar(). - you can try the below returning 0 as also varchar
amt_exp_formatted = CASE
WHEN a.class_code IN ('9997','9998','9999') THEN '0'
--ELSE CAST(e.min_sal as VARCHAR(10)) + '-' + CAST(e.max_sal as VARCHAR(10))
ELSE CONVERT(VARCHAR(1), 1) + CONVERT(VARCHAR(1), 'a')
END

Replacing `:` with `-` while doing an `INSERT` statement

I am trying to move some data into a program but the name field does not accept the : character. I would like to change the : to a - while doing the INSERT statement. The data I am moving is something like this "Revenue:A". I would like it to be "Revenue-A" when I move it.
I have tried the REPLACE statement in a nested SELECT statement with no luck. I have researched using a char index statement but do not understand how to make it change the code.
SELECT Account
, 'Accounts Receivable'
, (SELECT REPLACE([DESC], '%:%', '-'))
, (Select REPLACE(Account + ' - ' + [DESC], '%:%', '-'))
, ………
FROM ………
WHERE ………
The results that I get still include the :. When I try the code without the wildcard character it just returns - as the name, which will not work.
You don't need the select statements if the columns [DESC] and Account are columns from table(s) mentioned in your query:
,REPLACE([DESC], ':', '-')
,0,0
,REPLACE(Account + ' - ' + [DESC],':','-')
I removed % from the argument ':' (if you used it as a wildcard it does not work and it is not needed).

SQL I need to extract a stored procedure name from a string

I am a bit new to this site but I have looked an many possible answers to my question but none of them has answered my need. I have a feeling it's a good challenge. Here it goes.
In one of our tables we list what is used to run a report this can mean that we can have a short EXEC [svr1].[dbo].[stored_procedure] or "...From svr1.dbo.stored_procedure...".
My goal is to get the stored procedure name out of this string (column). I have tried to get the string between '[' and ']' but that breaks when there are no brackets. I have been at this for a few days and just can't seem to find a solution.
Any assistance you can provide is greatly appreciated.
Thank you in advance for entertaining this question.
almostanexpert
Considering the ending character of your sample sentences is space, or your sentences end without trailing ( whether space or any other character other than given samples ), and assuming you have no other dots before samples, the following would be a clean way which uses substring(), len(), charindex() and replace() together :
with t(str) as
(
select '[svr1].[dbo].[stored_procedure]' union all
select 'before svr1.dbo.stored_procedure someting more' union all
select 'abc before svr1.dbo.stored_procedure'
), t2(str) as
(
select replace(replace(str,'[',''),']','') from t
), t3(str) as
(
select substring(str,charindex('.',str)+1,len(str)) from t2
)
select
substring(
str,
charindex('.',str)+1,
case
when charindex(' ',str) > 0 then
charindex(' ',str)
else
len(str)
end - charindex('.',str)
) as "Result String"
from t3;
Result String
----------------
stored_procedure
stored_procedure
stored_procedure
Demo
With the variability of inputs you seem to have we will need to plan for a few scenarios. The below code assumes that there will be exactly two '.' characters before the stored_procedure, and that [stored_procedure] will either end the string or be followed by a space if the string continues.
SELECT TRIM('[' FROM TRIM(']' FROM --Trim brackets from final result if they exist
SUBSTR(column || ' ', --substr(string, start_pos, length), Space added in case proc name is end of str
INSTR(column || ' ', '.', 1, 2)+1, --start_pos: find second '.' and start 1 char after
INSTR(column || ' ', ' ', INSTR(column || ' ', '.', 1, 2), 1)-(INSTR(column || ' ', '.', 1, 2)+1))
-- Len: start after 2nd '.' and go until first space (subtract 2nd '.' index to get "Length")
))FROM TABLE;
Working from the middle out we'll start with using the SUBSTR function and concatenating a space to the end of the original string. This allows us to use a space to find the end of the stored_procedure even if it is the last piece of the string.
Next to find our starting position, we use INSTR to search for the second instance of the '.' and start 1 position after.
For the length argument, we find the index of the first space after that second '.' and then subtract that '.' index.
From here we have either [stored_procedure] or stored_procedure. Running the TRIM functions for each bracket will remove them if they exist, and if not will just return the name of the procedure.
Sample inputs based on above description:
'EXEC [svr1].[dbo].[stored_procedure]'
'EXEC [svr1].[dbo].[stored_procedure] FROM TEST'
'svr1.dbo.stored_procedure'
Note: This code is written for Oracle SQL but can be translated to mySQL using similar functions.

Snowflake Syntax - Unexpected space character in Syntax

Query
select sis.subject_code||'_'||LEFT(REPLACE(sis.SIS_TERM_ID,0,''),LENGTH(sis.SIS_TERM_ID) - 4)||''|| REPLACE(SUBSTR(sis.SIS_TERM_ID, 8, 8),'','')
from TableX;
Result looks like the following
XXXX888543_134 1 ---there is a space before the last value. I am not sure where this is being derived from. Any ideas on what I could modify in the string above please.
Assuming the space is really a space, how about doing the replace() across the whole string?
select replace(sis.subject_code || '_' || LEFT(REPLACE(sis.SIS_TERM_ID, 0, ''), LENGTH(sis.SIS_TERM_ID) - 4) || SUBSTR(sis.SIS_TERM_ID, 8, 8), '', '')
It is unclear whether the replace is coming from the last element or the one before it. But you don't seem to want any spaces in the string.