In ARRAY_LITERAL, an Array Literal was missing values for one or more rows - google-query-language

Tried all the solutions did not work
=QUERY({IMPORTRANGE("19_HAo-q9PuVtMxBWhXDQ_ftmikKzU-eNlSFxl3KwUvE","August19 Tracker!A2:O");IMPORTRANGE("1O_GIuel6WYpYx7cqdhAdLLzHF_wctjJ3aWmAR3I0oIo","August 19!A2:O")},"Select * where (Col1<> '' and Col6<> '')")

Related

MySQL - Replace string when it appears

The column 'PrizeMoneyBreakDown' includes a number of strings seperated by a semi-colon. I am trying to remove the strings 'total value', 'trophy total value', and 'welfare fund' from the data. These strings only appear sometimes in the data so it is not as simple as just removing the last three strings. I need to write a query that removes the strings IF they appear.
Example of data:
1st,5285;2nd,1680;3rd,885;4th,550;5th,350;6th,350;7th,350;8th,350;total_value,10000;welfare_fund,200;trophy_total_value,150;
Desired output of data:
1st,5285;2nd,1680;3rd,885;4th,550;5th,350;6th,350;7th,350;8th,350
Current code (only removes the words 'total value' etc - does not remove prize money associated with string):
SELECT PrizeMoneyBreakDown,
REPLACE(REPLACE(REPLACE(PrizeMoneyBreakDown,'total_value',""),'welfare_fund',""),'trophy_total_value',"") as new
FROM race2;
On MySQL 8+, we can use REGEXP_REPLACE:
SELECT PrizeMoneyBreakDown,
REGEXP_REPLACE(PrizeMoneyBreakDown,
'(total_value|welfare_fund|trophy_total_value),\\d+;',
'') AS NewPrizeMoneyBreakDown
FROM race2;
If you want to update the actual column then use:
UPDATE race2
SET PrizeMoneyBreakDown = REGEXP_REPLACE(
PrizeMoneyBreakDown,
'(total_value|welfare_fund|trophy_total_value),\\d+;',
'')
WHERE PrizeMoneyBreakDown REGEXP '(total_value|welfare_fund|trophy_total_value),\\d+;';

MySQL Search strings

I'm looking for a way to get a row from a tabla which have a column data type of string. This column could have values as follows:
1. "1,2,3,4,5"
2. "X,3,4,5,8"
3. "X,X,3,4,5"
4. "1,2,3,4,X"
5. "1,3,4,X,X"
and so on, ...
I want to accomplish a search for a String like
"1,2,3,4,5"
I tried with a
SELECT *
FROM *table_name*
WHERE *column* LIKE '%1,2,3,4,5%';
hoping this query could retrieve at least three results (in the example, first, third and forth strings) but it returns only the first string, because of course it's the only string that matches with the specified criteria. Anyone knows a way for me to accomplish this achievement?
I assume the X listed is literally the X character - if so, try
SELECT * FROM table WHERE '1,2,3,4,5' REGEXP REPLACE(column, 'X', '.')

How to remove a number from MySQL's JSON array?

If I have a MySQL table with a JSON column called numbers and a record that has [1, 2, 3] in that column (array of integers), how do I update that record to remove the 2 (so it becomes [1, 3])?
I was searching for an answer my self and came to this question, not wanting to use objects I continued the search. But I found a solution, you need to use a combination of json_remove and json_search
The following removes the value 1 from the table tbl and the column numbers
UPDATE tbl
SET numbers = JSON_REMOVE(
numbers, replace(json_search(numbers, 'one', 1), '"', '')
)
WHERE json_search(numbers, 'one', 1) IS NOT NULL
json_search returns the path of where the value is, ie. "$[0]"
replace remove the " otherwise an error will occur with json_remove
json_remove will remove the path from the json_search result
Et voila, your value is removed.
Note: this assumes no duplicate values
Until someone finds a better solution, I just converted it to an object instead: {"1": 1, "2": 2, "3": 3}. Yes, this is uglier and occupies more disk space, but you get the benefit of not having to worry about duplicates.
To add a number:
update tbl set numbers = json_insert(`numbers`, '$."4"', 4);
To remove a number:
update tbl set numbers = json_remove(`numbers`, '$."4"');
To get the row with a certain number:
select * from tbl where json_contains_path(`numbers`, 'one', '$."4"');
By the way,
JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path]
...])
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-search
just search_str

How do I get the type of a variable in MySQL?

I'm trying to change a table field that contains decimal numbers from varchar(255) to decimal(12,2). And before I do that, I'd like to find out if there is information that would get deleted in the process: are there any rows where this field contains something other than a decimal(12,2).
I'm stumped how to do this. Apparently there isn't a string function like is_numeric() in PHP. I already tried casting the field to decimal and then comparing it with the original string, but this returns TRUE even for obvious cases where it should not:
select ('abc' = convert('abc', decimal(12,2)));
returns 1
Any help? How do I find out if a string contains something other than a decimal in MySQL? Thanks.
Stupid me, I have to cast twice (to decimal and back to char), which makes it work:
select ('abc' = convert(convert('abc', decimal(12,2)), char(255)));
returns 0
Thanks.
If you want to examine if the strings are actually floating points numbers, you could also use a regular expression. The following regex can help :)
SELECT '31.23' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 1
SELECT '31' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 1
SELECT 'hey' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 0

MySQL: can I do a for-each on a comma separated field?

I just ran into a problem.
I know these integers, $integers: 3,5,15,20.
I only want to select the rows from this following table where all comma separated INT's from the field NUMBERS are found.
TABLE: number_table
Uid Numbers
------------------------
1 3,5,15 OK, since all of NUMBERS are in $integers
2 5,15,20 OK, since all of NUMBERS are in $integers
3 3,4,5,15 NOT OK, since 4 is not found in $integers
4 2,15,20,25 NOT OK, since 2 and 25 is not found in $integers
Is it possible to do a "for-each" on a comma separated string or another way to do this SELECT?
UPDATE: It sounds like this is not possible. I will leave it here for little while. Just a hint. When searching for something in a comma separated string then MySQL provides the WHEERE something IN (comma separated string). What I What I look for is someway to traverse a comma separated string using MySQL but that might not be possible.
Something like this would do it (pseudocode):
SELECT * FROM number_table WHERE each_commaseparated_substring(Numbers , 'IN (3,5,15,20)')
It should NOT be comma separated fields.
It must be rows in the related table.
I haven't tried this, and it's a bit ugly and quite possibly slow but you can try the following.
3,5,15,20
SELECT * FROM number_table
WHERE Numbers (LIKE '%,3,%' OR LIKE '%3,%') AND Numbers LIKE '%,5,%' AND Numbers LIKE '%,15,%' AND Numbers (LIKE '%,20,%' OR LIKE '%,20%')
You may be able to do something with REGEX. But at the very least you could use a stored procedure.
Updated for correctness
Maybe try with concate code using PHP and the implode() function.
Correct the short answer is no, but despite being non-normal data there are solutions that are ugly so not recommended. Specifically make a split string function and loop through each value with a stored procedure.
Can MySQL split a column?
Mysql string split
You could check that the number of commas is one less than the number of search terms found:
SELECT * FROM number_table
WHERE CHAR_LENGTH(Numbers) - CHAR_LENGTH(REPLACE(Numbers, ',', '')) = -1
+ (FIND_IN_SET( 3, Numbers) > 0)
+ (FIND_IN_SET( 5, Numbers) > 0)
+ (FIND_IN_SET(15, Numbers) > 0)
+ (FIND_IN_SET(20, Numbers) > 0)
To create this from $integers using PHP:
$sql = "SELECT * FROM number_table
WHERE CHAR_LENGTH(Numbers) - CHAR_LENGTH(REPLACE(Numbers, ',', '')) = -1";
foreach ($integers as $i) $sql .= " + (FIND_IN_SET($i, Numbers) > 0)";