sql code not working in oraclesqldeveloper: Invalid Character error - sql

I wrote a code to find few items by a particular number but it keeps saying that is something is an "invalid character"."ORA-00911: invalid character
00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action: Error at Line: 14 Column: 23"
My brain is blown,I am not sure where the problem exists.
Need Help. Thanks.
select
a.app_num,
a.crs_pol_num,
kl.score,
kl.risk_level_desc
from
application a,
kyc_new_risk_level kl
where
a.app_num = kl.app_num
and a.app_num = (select max(to_number(a1.app_num)) from
application a1,
kyc_new_risk_level kl1 where kl1.app_num = a1.app_num and a1.crs_pol_num = a.crs_pol_num)
and a.crs_pol_num in (‘CG0147511’,
‘CG0133662’,
‘CG0138107’,
‘493186’,
‘CG0142230’,
‘CS0138382’,
‘CG0147509’,
‘CG0147545’,
‘921141048001’,
‘CG0347239’,
‘CG0142212’,
‘CG0147518’,
‘CG0134057’,
‘CG0143158’,
‘CG0147536’,
‘CG0244124’,
‘CG0134102’,
‘CG0241709’,
‘CG0147197’,
‘CG0137204’,
‘CG0347496’,
‘CG0147490’,
‘CG0143467’,
‘CG0135689’,
‘CG0146904’,
‘CS0132517’,
‘CG0145455’,
‘CG0147554’,
‘CG0133626’,
‘CG0147560’,
‘CG0135359’,
‘CG0133303’,
‘921165287001’,
‘CG0147546’,
‘CG0114581,
‘CG0122266’,
‘CG0236577’,
‘CG0345349’,
‘CG0132670’,
‘CG0147413’,
‘CG0241646’,
‘CG0143783’,
‘CG0245160’,
‘CG0124066’,
‘CG0124830’,
‘CG0145956’,
‘CG0232953’,
‘CG0144479’,
‘CG0147569’,
‘CG0147555’,
‘CG0244857’,
‘CG0147562’,
‘CG0347578’,
‘CG0346461’,
‘CS0133352’,
‘512097’,
‘CS0127026’,
‘CG0147583’,
‘CG0233314’,
‘CG0247096’,
‘CG0131282’,
‘CG0123462’,
‘CS0124502’,
‘CG0146034’,
‘CG0140236’,
‘CS0126420’,
‘CG0147557’,
‘CG0123182’,
‘CG0233300’,
‘CG0132782’,
‘CG0147501’,
‘CS0141693’,
‘CG0145237’,
‘CG0141763’,
‘CG0147591’,
‘CG0144107’,
‘CG0125208’,
‘CG0132306’);

SQL Developer is showing you the problem before you even run your query
That's not a quote, it's a smart quote. Which is nice for browsers and word processors - but not so much for the database engine.
If you click on that hint, it will replace it with a real single quote.
But with so many to fix, would be much faster, better to use search and replace.
But your cursor on the bad character, and hit Ctrl+R
And then hit 'replace all', then repeat for the closing smart quote.
But in case you missed all that, the error also back from the DB tells you where the issue is

Related

new lines are not getting eliminated

I'm trying to replace newline etc kind of values using regexp_replace. But when I open the result in query result window, I can still see the new lines in the text. Even when I copy the result, I can see new line characters. See output for example, I just copied from the result.
Below is my query
select regexp_replace('abc123
/n
CHAR(10)
头疼,'||CHR(10)||'allo','[^[:alpha:][:digit:][ \t]]','') from dual;
/ I just kept for testing characters.
Output:
abc123
/n
CHAR(10)
头疼,
allo
How to remove the new lines from the text?
Expected output:
abc123 /nCHAR(10)头疼,allo
There are two mistakes in your code. One of them causes the issue you noticed.
First, in a bracket expression, in Oracle regular expressions (which follow the POSIX standard), there are no escape sequences. You probably meant \t as escape sequence for tab - within the bracket expression. (Note also that in Oracle regular expressions, there are no escape sequences like \t and \n anyway. If you must preserve tabs, it can be done, but not like that.)
Second, regardless of this, you include two character classes, [:alpha:] and [:digit:], and also [ \t] in the (negated) bracket expression. The last one is not a character class, so the [ as well as the space, the backslash and the letter t are interpreted as literal characters - they stand in for themselves. The closing bracket, on the other hand, has special meaning. The first of your two closing brackets is interpreted as the end of the bracket expression; and the second closing bracket is interpreted as being an additional, literal character that must be matched! Since there is no such literal closing bracket anywhere in the string, nothing is replaced.
To fix both mistakes, replace [ \t] with the [:blank:] character class, which consists exactly of space and tab. (And, note that [:alpha:][:digit:] can be written more compactly as [:alnum:].)

How to include apostrophe in character set for REGEXP_SUBSTR()

The IBM i implementation of regex uses apostrophes (instead of e.g. slashes) to delimit a regex string, i.e.:
... where REGEXP_SUBSTR(MYFIELD,'myregex_expression')
If I try to use an apostrophe inside a [group] within the expression, it always errors - presumably thinking I am giving a closing quote. I have tried:
- escaping it: \'
- doubling it: '' (and tripling)
No joy. I cannot find anything relevant in the IBM SQL manual or by google search.
I really need this to, for instance, allow names like O'Leary.
Thanks to Wiktor Stribizew for the answer in his comment.
There are a couple of "gotchas" for anyone who might land on this question with the same problem. The first is that you have to give the (presumably Unicode) hex value rather than the EBCDIC value that you would use, e.g. in ordinary interactive SQL on the IBM i. So in this case it really is \x27 and not \x7D for an apostrophe. Presumably this is because the REGEXP_ ... functions are working through Unicode even for EBCDIC data.
The second thing is that it would seem that the hex value cannot be the last one in the set. So this works:
^[A-Z0-9_\+\x27-]+ ... etc.
But this doesn't
^[A-Z0-9_\+-\x27]+ ... etc.
I don't know how to highlight text within a code sample, so I draw your attention to the fact that the hyphen is last in the first sample and second-to-last in the second sample.
If anyone knows why it has to not be last, I'd be interested to know. [edit: see Wiktor's answer for the reason]
btw, using double quotes as the string delimiter with an apostrophe in the set didn't work in this context.
A single quote can be defined with the \x27 notation:
^[A-Z0-9_+\x27-]+
^^^^
Note that when you use a hyphen in the character class/bracket expression, when used in between some chars it forms a range between those symbols. When you used ^[A-Z0-9_\+-\x27]+ you defined a range between + and ', which is an invalid range as the + comes after ' in the Unicode table.

REGEXP_REPLACE explanation

Hi may i know what does the below query means?
REGEXP_REPLACE(number,'[^'' ''-/0-9:-#A-Z''[''-`a-z{-~]', 'xy') ext_number
part 1
In terms of explaining what the function function call is doing:
It is a function call to analyse an input string 'number' with a regex (2nd argument) and replace any parts of the string which match a specific string. As for the name after the parenthesis I am not sure, but the documentation for the function is here
part 2
Sorry to be writing a question within an answer here but I cannot respond in comments yet (not enough rep)
Does this regex work? Unless sql uses different syntax this would appear to be a non-functional regex. There are some red flags, e.g:
The entire regex is wrapped in square parenthesis, indicating a set of characters but seems to predominantly hold an expression
There is a range indicator between a single quote and a character (invalid range: if a dash was required in the match it should be escaped with a '\' (backslash))
One set of square brackets is never closed
After some minor tweaks this regex is valid syntax:
^'' ''\-\/0-9:-#A-Z''[''-a-z{-~]`, but does not match anything I can think of, it is important to know what string is being examined/what the context is for the program in order to identify what the regex might be attempting to do
It seems like it is meant to replaces all ASCII control characters in the column or variable number with xy.
[] encloses a class of characters. Any character in that class matches. [^] negates that, hence all characters match, that are not in the class.
- is a range operator, e.g. a-z means all characters from a to z, like abc...xyz.
It seams like characters enclosed in ' should be escaped (The second ' is to escape the ' in the string itself.) At least this would make some sense. (But for none of the DBMS I found having a regexp_replace() function (Postgres, Oracle, DB2, MariaDB, MySQL), I found something in the docs, that would indicate this escape mechanism. They all use \, but maybe I missed something? Unfortunately you didn't tag which DBMS you're actually using!)
Now if you take an ASCII table you'll see, that the ranges in the expression make up all printable characters (counting space as printable) in groups from space to /, 0 to 9, : to #, etc.. Actually it might have been shorter to express it as '' ''-~, space to ~.
Given the negation, all these don't match. The ones left are from NUL to US and DEL. These match and get replaced by xy one by one.

In bigquery sql are curly braces allowed as part of a column alias

I'm trying to do something like this
SELECT epi_week {week}, state
FROM
lookerdata:cdc.project_tycho_reports
LIMIT 10
Error: Encountered " "{" "{ "" at line 1, column 17. Was expecting: EOF>
It seems that curly braces are not legal syntax. I've tried escaping or using quotes without success.
Is there a way around this? We use the braces as an indication for post-processing string replacement to support multiple languages.
Is there a way around this?
No way unfortunatelly.
Field name must contain only letters, numbers, and underscores, start with a letter or underscore, and be at most 128 characters long.
As an option - you might want to come up with another name convention for post-processing

How can I write special character in VB code

I have a Sql statament using special character (ex: ('), (/), (&)) and I don't know how to write them in my VB.NET code. Please help me. Thanks.
Find out the Unicode code point for the character (from http://www.unicode.org) and then use ChrW to convert from the code point to the character. (To put this in another string, use concatenation. I'm somewhat surprised that VB doesn't have an escape sequence, but there we go.)
For example, for the Euro sign (U+20AC) you'd write:
Dim euro as Char = ChrW(&H20AC)
The advantage of this over putting the character directly into source code is that your source code stays "just pure ASCII" - which means you won't have any strange issues with any other program trying to read it, diff it, etc. The disadvantage is that it's harder to see the symbol in the code, of course.
The most common way seems to be to append a character of the form Chr(34)... 34 represents a double quote character. The character codes can be found from the windows program "charmap"... just windows/Run... and type charmap
If you are passing strings to be processed as SQL statement try doubling the characters for example.
"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "
The '' double works with the other special characters as well.
The ' character can be doubled up to allow it into a string e.g
lSQLSTatement = "Select * from temp where name = 'fred''s'"
Will search for all records where name = fred's
Three points:
1) The example characters you've given are not special characters. They're directly available on your keyboard. Just press the corresponding key.
2) To type characters that don't have a corresponding key on the keyboard, use this:
Alt + (the ASCII code number of the special character)
For example, to type ¿, press Alt and key in 168, which is the ASCII code for that special character.
You can use this method to type a special character in practically any program not just a VB.Net text editor.
3) What you probably looking for is what is called 'escaping' characters in a string. In your SQL query string, just place a \ before each of those characters. That should do.
Chr() is probably the most popular.
ChrW() can be used if you want to generate unicode characters
The ControlChars class contains some special and 'invisible' characters, plus the quote - for example, ControlChars.Quote