how to convert text to number in a SOQL query? - socrata

Can somebody please help me figure out what is wrong with this query?
https://data.cityofnewyork.us/resource/i4gi-tjb9.json?$where=speed<10
It failed with error code: query.soql.type-mismatch
the "speed" column of the table is declared as text. I am having trouble in converting speed to a real (decimal) number for the comparison. Thanks

Try casting the column with ::number, like this:
GET https://data.cityofnewyork.us/resource/i4gi-tjb9.json?$where=speed::number > 31.69

Put the number in parenthesis, like this:
https://data.cityofnewyork.us/resource/i4gi-tjb9.json?$where=speed<"10"
or if you need it url encoded, use this:
https://data.cityofnewyork.us/resource/i4gi-tjb9.json?$where=speed%3C%2210%22

Related

query json in postgresql with "\" inside

I have a json which looks like this:
"{\"chat:title\":\"Random name Comunidad\",\"chat:type\":\"supergroup\",\"command:start:count\":4,\"command:start:ts\":1648146227630,\"command:help:count\":1,\"command:help:ts\":1648145742922,\"command:price:count\":3,\"command:price:ts\":1648146698585}"
And I'd like to query it and get the name out of it. I tried classically:
SELECT metadata->>'chat'
SELECT metadata->>'chat:title'
but it's not working. I think it's because of the backslash in the string... Any ideas how to query it?
Just remove backslash from the string:
with t as (
select '{\"chat:title\":\"Random name Comunidad\",\"chat:type\":\"supergroup\",\"command:start:count\":4,\"command:start:ts\":1648146227630,\"command:help:count\":1,\"command:help:ts\":1648145742922,\"command:price:count\":3,\"command:price:ts\":1648146698585}'
as metadata)
select replace(t.metadata, '\','')::json ->> 'chat:title'
from t
Is it of JSON type and the content is as is you showed? If so then:
select btrim(replace(metadata::text,'\',''),'"')::json->>'chat:title';
Manually removing the backslashes might work, but will eventually fail on some inputs and just make things worse.
The right way is to use the existing JSON operators to fix it (unless of course someone has already made it worse).
SELECT (metadata->>0)::jsonb ->> 'chat:title'
But really you should fix the source that is causing the problem, and update the existing data to store the fix.
update whatever set metadata=(metadata->>0)::jsonb;
If only some of the data has been crappified, you might need to where clause to distinguish the good from the bad and so only fix the bad.

CakePHP 3.2, date output

I´ve several dates wich are stored in my database like this: 2016-02-17.
I want them to be displayed like this: 17.02.2016.
For this I use this code in my AppController:
I18n::locale('de-DE');
Time::$defaultLocale = 'de-DE';
Time::setToStringFormat('dd.MM.YYYY');
Type::build('datetime')->useLocaleParser()->setLocaleFormat('dd.MM.YYYY');
But my output always looks like this: 17.02.16.
How do I force the output of the year with 4 characters?
Many thanks in advance. :)
Try using date("d.m.Y", strtotime($yourDateAsStringFromDb)). This will return the date formatted as you describe you want it.
I just tryed using this and it works to display the date as you asked.
bare in mind in this capital letters matter

How can I order a mySQL column by a number imbedded in a string?

I am parsing genomic positions from a MySQL field. The field is called "change" and the entries are of the form:
g.100214985T>C
g.100249769C>A
g.10185G>T
I am trying to order the field by the numerical portion of the string. I am trying to figure out what mySQL query I can use to accomplish this. I have tried using REGEXPs and SUBSTRING_INDEX but am still running into issues. Any help would be much appreciated!
Assuming you have always 2 characters in front of and 3 at the end you need to have removed:
SELECT CAST(SUBSTR(col from 3) AS UNSIGNED) AS value
FROM `my_table`
ORDER BY value
Watch this sql fiddle also: http://sqlfiddle.com/#!2/7bc0e/67
Thank you #MarcusAdams and #amoudhgz! The following code works:
CAST(SUBSTR(field, 3) AS UNSIGNED).
MySQL already stops the conversion at the first non-numerical character.

Looking to extract data between parentheses in a string via MYSQL

Can someone please help. I have been searching and encountered/modified this code I am getting a 1 or 0 as a result. 1 if there is something between () and 0 if there is not. I am looking to find exactly what is between them not if there is something. So if I have a string in afield that looks like this: "ABC (989) Hello" currently I get 1 as my result I would like to get "989". Any help would be greatly appreciated.
select , OUTCNTCTNOTE regexp '[(]|\\[)]' as test
from trcalls.callcoding;
To complete the first answer, because the third parameter passed to substr is the length of the substring, we need to subtract the index of the opening parantheses, so:
substr(columnname,instr(columnname,"(") + 1, instr(columnname,")") - instr(columnname,"(") - 1)
should do the trick
select substr(columnname,instr(columnname,"(") + 1, instr(columnname,")")) as temp from mytable
something close, I tested this. Please see if this helps!
Mysql's regexes don't support capturing or replacing. They're purely for matching. You'd need to use regular string operations to do the actual extraction:
SELECT ...string stuff here...
FROM yourtable
WHERE OUTCNTCTNOTE regexp ....
If your strings are fairly 'regular' and you don't have to worry about multiple sets of brackets in any field, then using LOCATE() and SUBSTR() would do the trick.

Query MySQL with unicode char code

I have been having trouble searching through a MySQL table, trying to find entries with the character (UTF-16 code 200E) in a particular column.
This particular code doesn't have a glyph, so it doesn't seem to work when I try to paste it into my search term. Is there a way to specify characters as their respective code point instead for a query?
Thanks,
-Ben
Not tested, but CHAR() could work for this:
CHAR(0x200E);
I can't set up a full test case right now, let us know whether it worked.