REGEXP_REPLACE Punctuation in Redshift - sql

I'm trying to use REGEXP_REPLACE to remove all punctuation from a varchar. I'm using the following:
regexp_replace(d.NAME, [.,\/#!$%\^&\*;:{}=\-_`~()])
But it gives me an error, saying:
Statement 1 is not valid. ERROR: syntax error at or near "."
How can I fix this to remove all punctuation?

Firstly, the dash in a character class means a range, except when it's first or last... so put it there:
[.,\/#!$%\^&\*;:{}=\_`~()-]
And, you have to put it in quotes, and most characters don't need escaping:
regexp_replace(d.NAME, '[.,/#!$%^&*;:{}=_`~()-]')

Related

Syntax error in my SQL when inserting a JSON field

Why this query is not working:
UPDATE country SET timezones="[{"zoneName":'Asia\/Kabul',"gmtOffset":16200,"gmtOffsetName":'UTC+04:30',"abbreviation":'AFT',"tzName":'Afghanistan Time'}] " where name='Afghanistan'
Error I get:
ERROR: syntax error at or near "zoneName"
LINE 1: UPDATE country SET timezones="[{"zoneName":'Asia/Kabul',"gm...
^
SQL state: 42601
Character: 34
the issue with your SQL statement is that the literal string you are trying to set timezones to contains improperly formatted escape characters. if you wanted to avoid that first error you can double up on quotes like timezones="[{""zoneName"": ...
you can go to the link to see more about string formating in SQL. good luck!
You're trying to update the value wrapping the string in quotes. You need to wrap the string in single quotes timezones='[{"zoneName":'Asia...}]'
However, to TitledTeapot's point, you will also have to escape the existing single quotes in your string, so you'd end up with something like this:
'[{"zoneName":''Asia\/Kabul'',"gmtOffset":16200,"gmtOffsetName":''UTC+04:30'',"abbreviation":''AFT'',"tzName":''Afghanistan Time''}]'

Adding a colon to language in K

How do I add colon as a syntactic construct in my language? I've added it to the syntax, but when I try to match a colon inside a rule it gives me a parse error.
K is getting confused when you surround it with parenthesis. Delete the surrounding parenthesis and the parse error should be removed.

Postgres syntax error when using 'like' in single quote

I am getting a syntax error in a PostgreSQL query. I am working on a project developed in YII1, I am getting an error
CDbCommand failed to execute the SQL statement: SQLSTATE[42601]:
Syntax error: 7 ERROR: syntax error at or near "s" LINE 1: ...OT NULL
AND sub_heading like '%Women and Children's Voices%'.
As you can see above, I am using the like operator in single quotes, and in the string there is another single quote (Children's). So PostgreSQL is throwing me an error. Please provide me a solution to escape the string.
You can escape a single quote in a string by using another single quote (i.e., '' instead of '. Note that these are two ' characters, not a single " character):
sub_heading LIKE '%Women and Children''s Voices%'
-- Here -----------------------------^
You should use the format function to construct the SQL statement, using the %L placeholder for the pattern.
I solved this problem by replacing the single quote with double quotes using PHP. Here is the code
There is a variable $var with value Women and Children's Voices. I replace that single quote using the str_replace() function.
$var = str_replace("'", "''", $var);

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

Regular Expression to return when invalid character found

I have the following regex that checks for a list of valid characters:
^([a-zA-Z0-9+?/:().,' -]){1,35}$
What I now need to do now is search for any existing columns in our DB that invalidates the above regex. I'm using the oracle SQL REGEXP_LIKE command.
The problem I have is I can't seem to negate the above expression and return a value when it finds a character not in the expression e.g.
"a-valid-filename.xml" => this shouldn't be returned as it's valid.
"an_invalid-filename.xml" => I need to find these i.e. anything with an invalid character.
The obvious answer to me is to define a list of invalid characters... but that could be a long list.
You can match it against the following regex which uses the [^...] negation character class:
([^a-zA-Z0-9+?/:().,' -])
This will match any single character that is not part of the list of characters that are allowed.
You can negate a character class by inserting a caret as the first character.
Example:
[^y]
The above will match anything that is not y
Try this:
where not regexp_like(col, '^([a-zA-Z0-9+?/:().,'' -]){1,35}$')
or
where regexp_like(col, '[^a-zA-Z0-9+?/:().,'' -]')