I have a table that is a single column made up of a JSON string. The JSON has multiple key pairs, and is a string because it is the raw table.
One of the keys is "Ticket" and has dollar amount values. I am not certain if prices are in __.__ format, or just ____. I want to query the column to return me the entire string if this "Ticket" ends in a 6, as in 96 cents, or 66 cents, etc.
This is my query:
SELECT json FROM tablename
WHERE json RLIKE '%"TICKET": "___6",%'
OR json RLIKE '%"TICKET": "__._6",%'
This currently returns as blank.
How can I get the entire string if the dollar amount ends in a 6 (as in 6 cents)?
The search strings you are using are what you would use for LIKE
So you could use LIKE :
select * from tablename
where (json LIKE '%"TICKET": "___6"%' or json LIKE '%"TICKET": "__._6"%')
Or a RLIKE with a regex:
select * from tablename
where json RLIKE '"TICKET":[ ]*"[0-9.]+6"'
Related
I want to create two columns from a column of values containing JSON in Snowflake using SQL.
Say this table is called keywords_bids
then there is a column called keywords that has JSON in it
example json in a cell in the keywords column:
row1: {"apple":0.1, "peach":0.2, "banana":0.1} row2: similar JSON, etc....
input image
I want to create a columns called keyword and it is bid price from the JSON
output would be:
keyword | Bid
'apple' | 0.1
'peach' | 0.2
'banana'| 0.3
First for JSON you'll need to change the single quotes to double quotes.
Then you just need to flatten the json to get keys and values:
with data as (
select parse_json('{"apple":0.1, "peach":0.2, "banana":0.1}') j
)
select k.key, k.value
from data, table(flatten(j)) k
;
https://community.snowflake.com/s/article/Dynamically-extracting-JSON-using-LATERAL-FLATTEN
This article is to demonstrate various examples of using LATERAL FLATTEN to extract information from a JSON Document. Examples are provided for its utilization together with GET_PATH, UNPIVOT, and SEQ functions.
I have a JSON in my MYSQL like [1,2,3].
And how can i get where 1 is.
I tried to use this:
SELECT JSON_SEARCH('[1,2,3]','one',1);
But the result of this is NULL
I expect the output to be $[0]
Your JSON array elements should be in double quotes:
WITH yourTable AS (
SELECT '["1","2","3"]' AS json
)
SELECT
json,
JSON_SEARCH(json, 'one', '1')
FROM yourTable;
This returns "$[0]" for the match of '1' against the JSON text.
Demo
As to why it doesn't work with number literals (which should in fact be valid literal JSON values), it seems that the third parameter to JSON_SEARCH is a search string, and it only works against actual text, not numbers.
I have two fields in my MySQL database that are arrays. The datatype is shown as LONGTEXT with a Comment of (DC2Type:array).
For example, the integer values stored in this field would look like this:
a:4:{i:0;i:9;i:1;i:10;i:2;i:11;i:3;i:12;}
And the String values would look like this:
a:2:{i:0;s:6:"Value1";i:1;s:6:"Value2";}
I need these fields this way so I can store columns that are filterable. E.g. the first one may be age groups so ages 9,10,11,12 are represented.
My query must then get all records that say are relevant for age 10 or in some cases say I want to find those that are 10 and 11.
I've tried the IN and FIND_IN_SET syntaxes but neither is returning any results.
Using IN
SELECT *
FROM table_name
WHERE MyField IN (10)
Using FIND_IN_SET
SELECT *
FROM table_name
WHERE FIND_IN_SET(MyField,'Value1') > 0;
I know arrays are probably not the best field to store values in but I didn't want to have separate fields for each AgeGroup e.g. Age1, Age2, etc. or each category e.g Value1, Value2, etc.
Any thoughts on how I can find a value or values from a database array field, please?
Thanks!
You can use a pattern match.
Integer:
WHERE MyField LIKE '%i:10;%'
String:
WHERE MyField LIKE '%s:6:"Value1";%'
6 has to be replaced with the length of the string you're searching for.
If you want to search for multiple numbers or strings, you can use a regular expression with alternation:
WHERE MyField RLIKE 'i:(10|11);'
WHERE MyField RLIKE 's:(6:"Value1"|10:"LongValue2");'
Note that none of these methods can make use of an index on the table. It's generally a bad idea to store arrays in database columns, you should store them as separate rows in a many-to-many table.
I have a table with fields containing html code. I would like to select only those rows where htmlfield contain string "<p>[[{"fid":" but only in first 10 characters of the html field (this field contains more of such strings and I want to find only fields that contain the string in the beginning).
Is it possible to do such select?
You can use a SUBSTRING() to grab the fields with that string in them.
SELECT field1
FROM TableName
WHERE SUBSTRING(field1, 1, 12) = '<p>[[{"fid":'
Example
You can also try using the LIKE function. Where you can use the % wildcard at the end of your string to get fields that start with that string.
SELECT field1
FROM TableName
WHERE field1 LIKE '<p>[[{"fid":%'
Example
Do it like this:
select SUBSTRING(field_name,1, 10) from table_name;
It will display 10 characters only for that specific field.
Hope it helps
It is not my code, its something that I need to get it done without modifying the structure of table. I know it would be very easy to just store date as MySQL date format but I cant do that.
There is a column in table which stores serialized array as a string. Now I need to select all rows whose 'date' is less than today.
This date is inside serialized array string.
Is there a way to compare it on mysql query? An example string is:
a:3:{s:4:"test";b:1;s:2:"se";i:1;s:4:"date";s:10:"2013-05-23";}
I need to compare the "date" from this string to mysql date using the following query:
"date" BETWEEN 2013-01-01 AND 2013-05-23
You can extract the date value (assuming it's always set off by "date";s:10) using nested SUBSTRING_INDEX calls. The inner one returns everything after "date";s:10" and the outer one cuts off the closing quote and whatever follows:
SUBSTRING_INDEX(SUBSTRING_INDEX(val, '"date";s:10:"', -1), '"', 1)
If val is a:3:{s:4:"test";b:1;s:2:"se";i:1;s:4:"date";s:10:"2013-05-23";} as in your example, this will return 2013-05-23. Then your query can be:
...
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(val, '"date";s:10:"', -1), '"', 1) BETWEEN 2013-01-01 AND 2013-05-23
Not pretty, but we can't expect pretty here :)
I think you should get date with substring with starting character -13 (13 from right side) and length of 10.
Something like this:
SUBSTR(field_name, -13, 10)
select * from postmeta where meta_key = 'your_meta_key' and meta_value REGEXP ('6')