I have names in my table for which I need to replace certain characters with others.
I have the following code, but I get an error
REPLACE(REPLACE(TRIM(name),NCHAR(0x2019),NCHAR(0x0027)),NCHAR(0x200B),'')
However I get the following error message. Can somebody help me rewrite the code not using the Nchar function?
Undefined function: 'NCHAR'. This function is neither a registered temporary function nor a permanent function registered in the database 'default'
Instead of NCHAR function you can use unicode literals (\uXXXX) to represent a character as it's described in Spark documentation, in your case it will be:
REPLACE(REPLACE(TRIM(name),'\u2019','\u0027'),'\u200B','')
Related
I'm trying to convert varchar WKT format to geometry using presto function ST_GeometryFromText but I get this error
Error running query: Invalid WKT: 0101000020E6100000000000407BF43E40000000203CFA3D40
The point format in the database is stored in this format 0101000020E6100000000000407BF43E40000000203CFA3D40 as varchar, i just want to convert it to a geometry point, i used to use ST_X & ST_Y in postgis but after migrating to presto these two functions aren't supported.
If you run
SELECT ST_AsText('0101000020E6100000000000407BF43E40000000203CFA3D40')
...in postgis, you will get the point POINT(30.955005645752 29.9774799346924).
If you want to separate longitude and latitude, run:
SELECT ST_X(ST_AsText('0101000020E6100000000000407BF43E40000000203CFA3D40')), ST_Y(ST_AsText('0101000020E6100000000000407BF43E40000000203CFA3D40'))
I found the answer is simply by removing this part of the string '20E61000', once removed, the function works fine, I've used this function
ST_GEOMFROMBINARY(FROM_HEX(REPLACE('0101000020E6100000000000407BF43E40000000203CFA3D40', '20E61000')))
and it worked fine, also I've verified the answer using python Shapley wkb function.
I had the same issue...had to massage the heck out of this thing.
select ST_GeomFromBinary(from_hex(to_utf8(replace(geom,'20E61000')))) as geom from ...
I have installed PostgresSQL 10.6 installed on Windows and using DBeaver - I confirmed the version by "SELECT VERSION()".
For some reason, whenever I try to use function SUBSTRING, I receive the below error:
SQL Error [42883]: ERROR: function pg_catalog.substring(character
varying, integer, character varying, integer) does not exist
Similarly for REGEXP_MATCHES
SQL Error [42883]: ERROR: function regexp_matches(character varying,
text, integer) does not exist Hint: No function matches the given
name and argument types. You might need to add explicit type casts.
The syntax I'm using I believe matches the docs, for example REGEXP_MATCHES(source_string, pattern, 1)
Any ideas why Postgres would throw these errors?
I was attempting to use the functions incorrectly which threw the "function doesn't exist". I guess it means that the function does exist if I want to use it with the type of parameters I tried to use.
REGEXP_MATCHES(source_string, pattern, 1) I understood to be a regex match with case insensitivty (1). As Nick Barnes pointed out in the comment, REGEXP_MATCHES takes (text,text,text).
I was shorterning the length of one text field to check if it existed within another column - rather than use SUBSTRING against col2, I used LEFT(col2,10) and instead of regexp_matches, I used (regexp_match(col1,col2))[1]
I have a column called "Bakery Activity" whose values are all JSONs that look like this:
{"flavors": [
{"d4js95-1cc5-4asn-asb48-1a781aa83": "chocolate"},
{"dc45n-jnsa9i-83ysg-81d4d7fae": "peanutButter"}],
"degreesToCook": 375,
"ingredients": {
"d4js95-1cc5-4asn-asb48-1a781aa83": [
"1nemw49-b9s88e-4750-bty0-bei8smr1eb",
"98h9nd8-3mo3-baef-2fe682n48d29"]
},
"numOfPiesBaked": 1,
"numberOfSlicesCreated": 6
}
I'm trying to extract the number of pies baked with a regex function in Tableau. Specifically, this one:
REGEXP_EXTRACT([Bakery Activity], '"numOfPiesBaked":"?([^\n,}]*)')
However, when I try to throw this calculated field into my text table, I get an error saying:
ERROR: function regexp_matches(jsonb, unknown) does not exist;
Error while executing the query
Worth noting is that my data source is PostgreSQL, which Tableau regex functions support; not all of my entries have numOfPiesBaked in them; when I run this in a simulator I get the correct extraction (actually, I get "numOfPiesBaked": 1" but removing the field name is a problem for another time).
What might be causing this error?
In short: Wrong data type, wrong function, wrong approach.
REGEXP_EXTRACT is obviously an abstraction layer of your client (Tableau), which is translated to regexp_matches() for Postgres. But that function expects text input. Since there is no assignment cast for jsonb -> text (for good reasons) you have to add an explicit cast to make it work, like:
SELECT regexp_matches("Bakery Activity"::text, '"numOfPiesBaked":"?([^\n,}]*)')
(The second argument can be an untyped string literal, Postgres function type resolution can defer the suitable data type text.)
Modern versions of Postgres also have regexp_match() returning a single row (unlike regexp_matches), which would seem like the better translation.
But regular expressions are the wrong approach to begin with.
Use the simple json/jsonb operator ->>:
SELECT "Bakery Activity"->>'numOfPiesBaked';
Returns '1' in your example.
If you know the value to be a valid integer, you can cast it right away:
SELECT ("Bakery Activity"->>'numOfPiesBaked')::int;
I found an easier way to handle JSONB data in Tableau.
Firstly, make a calculated field from the JSONB field and convert the field to a string by using str([FIELD_name]) command.
Then, on the calculated field, make another calculated field and use function:
REGEXP_EXTRACT([String_Field_Name], '"Key_to_be_extracted":"?([^\n,}]*)')
The required key-value pair will form the second caluculated field.
I am trying to duplicate a detail object from Desktop to its Webi equivalent.
I am familiar with the syntax differences, i.e. using semicolons instead of commas and using [] rather than <> to enclose references to other dimensions/measures/detail objects.
Given the working formula from the Deski report:
=ToDate(7+"/1/"+<Current FY>-1 ,"mm/dd/yyyy")
I tried to convert this using the syntax I know from Webi:
=ToDate(7+"/1/"+[Current FY]-1 ; "mm/dd/yyyy")
I faced the error message
The expression or sub-expression at position 8 in the '-' function uses an invalid data type
I am guessing this has something to do with trying to convert a date datatype into an integer in order to subtract "1." However, I do not know what kind of function this requires.
Thanks in advance!
In one place I have
CREATE FUNCTION updateGeo2(text, float4, float4) RETURNS float AS $$
followed later by
SELECT updateGeo2('area', 40.88, -90.56);
and I get
error : ERROR: function updategeo2(unknown, numeric, numeric) does not exist
so it doesn't know that I tried to pass in a text variable, followed by a float variable and another float variable, it sees these as "unknown, numeric and numeric", lame. How do I let it know the types I am passing in?
try this way:
SELECT updateGeo2('area', (40.88)::float4, (-90.56)::float4);
Clarify misunderstanding
First of all, this should work as is, without type cast. I tested with PostgreSQL 9.1, 9.2 and also with 8.4.15. You must be running an earlier point-release or there is some other misunderstanding (like wrong search_path). Your information is misleading.
Except for ad-hoc calls, you should always add explicit type casts anyway to disambiguate. PostgreSQL allows function overloading. If another function should be created with the signature:
CREATE FUNCTION updateGeo2(text, numeric, numeric) RETURNS text AS $$ ..
... then it would take precedence over the other one due to the default type numeric for numeric literals. Existing code might break.
If, on the other hand, you add a function:
CREATE FUNCTION updateGeo2(char(5), numeric, numeric) RETURNS text AS $$ ..
Then Postgres does not know what to do any more and throws an exception:
ERROR: function updategeo2(unknown, numeric, numeric) is not unique
Proper syntax
SELECT updateGeo2('area', '40.88'::float4, '-90.56'::float4);
Or, more verbose in standard SQL:
SELECT updateGeo2('area', cast('40.88' AS float4), cast('-90.56' AS float4));
Or, if you really wanted to avoid single quotes (and colons):
SELECT updateGeo2('area', float4 '40.88', float4 '-90.56');
This way you cast a numeric literal to data type float4 (= real) directly.
More about type casting in the manual.
(40.88)::float4 works, too, but subtly less effective. First, 40.88 is taken to be of type numeric (the default type for this numeric literal containing a dot). Then the value is cast to float4. Makes two type casts.
More about numeric constants in the manual.