I have a MySQL table setup where one column's values are a string of comma-separated True/False values (1s or 0s). For example, in the column, one field's value may be "0,1,0,0,0,0,1,1,0" and another may be "1,0,0,1,1,1,0,0,0" (note: these are NOT 9 separate columns, but a string in one column). I need to QUERY the MySQL table for elements that are "true"(1) for the "nth element" of that column's value/string.
So, if I was looking for rows, with a specific column, where the 3rd element of the column's value was 1, it would produce a list of results. So, in this case, I would only be searching for "1" in the fth place (12345 = X,X,X...) of the string (X,X,1,X,X,X,X,X,X,X). How can I query this?
This is a crude example of what I am trying to do ...
"SELECT tfcolumn FROM mytable WHERE substr({tfcolumn}, 0, 5)=1"
{tfcolumn} represents the column value
5 represents the 5th position of the string
=1 represents what I need that position to equal to.
Please help. Thanks
You can't. Once you put a serialized data type into a column in SQL (like comma separated lists, or JSON objects) you are preventing yourself from performing any query on the data in those columns. You have to pull the data in a different way and then use a program like python, VB, etc to get the comma separated values you are looking for.
Unless you want to deal with trying to make this mess of a query work...
I would recommend changing your table structure before it's too late. Although it is possible, it is not optimized in a format that a DBMS recognizes. Because of that the DBMS will spend a significant amount of time going through every record to parse the csv values which is something that it was not meant to be doing. Doing the query in SQL will take as much time (if not more time) than just pulling all the records and searching with a tool that can do it properly.
If the column contains values exactly like the ones you posted, then the Nth element is at the 2 * N - 1 position in the comma separated list.
So do this:
SELECT tfcolumn
FROM tablename
WHERE substr(tfcolumn, 2 * 5 - 1, 1) = '1'
Replace 5 with the index that you search for.
See the demo.
Or remove all commas and get the Nth char:
SELECT tfcolumn
FROM tablename
WHERE substr(replace(tfcolumn, ',', ''), 5, 1) = '1'
See the demo.
Try this
if substring_index(substring_index('0,1,0,0,0,0,1,1,0',',',3),',',-1)='1'
The first argument can be your column name. The second argument (',') tells the function that the string is comma-separated. The third argument takes the first 3 elements of the string. So, the output of inner substring_index is '0,1,0'.
The outer substring_index has -1 as the last argment. So, it starts counting in reverse direction & takes only 1 element starting from right.
For example, if the value in a particular row is '2,682,7003,14,185', then the value of substring_index(substring_index('2,682,7003,14,185',',',3),',',-1) is '7003'.
I am having a hell of a time trying to add two calculated fields in a query together. My first record has field1= 1, and field2= 5, and the field that is trying to add them as 15!
So it’s treating them as a string.
When I try to use the function of SUM() I get an error of some of the other fields not being used in expressions, which I don’t understand.
Subtracting the two fields works as does multiplication.
I am unable to change the format of either fields in the properties as the drop down menu is blank.
Please help!
Aggregate functions act on rows not fields. Sum(field1) adds the values of field1 for group of records. Use aggregate functions in an aggregate (GROUP BY) query.
Plus (+) character will concatenate text values but add numeric. Apparently, your two fields are providing text values. Either correct the field data type or use function to convert values to number. Convert at least one field and Access will perform arithmetic instead of concatenation on all terms of expression.
Val(field1) + field2
This assumes no fields are Null. Number conversion functions will error with Null. Also, arithmetic with Null returns Null. If Null is possibility, handle with Nz().
Val(Nz(field1,0)) + Nz(field2,0)
Hi I have been trying to select the records whose column has no integer I have this piece of code and tried it different ways but still get back rows with P992142
P992142
P301716
P301716
P307162
P306522
which I don't want
select practitioner_id
from claimsprofinload
WHERE practitioner_id not like '%[0-9]%';
You're using a regular expression in conjunction with LIKE, which is not valid. What you want is the REGEXP or RLIKE comparison.
Since that expression is evaluated as a more literal string, and since none of your rows have [0-9] literally in them, it matches all rows.
I need to search for a value like 1234-abc. The database doesn't have this particular value, but has another value 1234. Now the problem is when I write my query like
SELECT * FROM words WHERE tval='1234-abc'
instead of fetching an empty recordset, it fetches the 1234 value, it seems to ignore anything after the -, any idea what's going on?
http://sqlfiddle.com/#!2/9de62/3
You can use the BINARY keyword for the exact match
SELECT tval FROM words WHERE BINARY tval='1223-abc';
Binary is a built-in keyword that after your WHERE clause that forces a comparison for an exact case-sensitive match
Fiddle
The existing expression is implicitly converting the string expression to a number - you need to explicitly convert the number to a character strng, like so:
SELECT tval FROM words WHERE convert(tval,char(20))='1223-1ABCDE';
SQLFiddle here.
I have a table with a field value which is a varchar(255). The contents of the field can be quite varied:
$1.20
$2994
$56 + tax (This one can be ignored or truncated to $56 if necessary)
I have a query constructed:
SELECT value FROM unnamed_table ORDER BY value
However, this of course uses ASCII string comparison to order the results and does not use any numerical type of comparison.
Is there a way to truly order by value without changing the field type to DECIMAL or something else? In other words, can the value field be modified ('$' removed, value converted to decimal) on the fly before the results are sorted?
You could sort on an expression made to "parse" the text into decimal
SELECT value FROM unnamed_table ORDER BY expression_returning_decimal(value)
Where your expression uses MySQL functions to extract the number from the string depending on what form you expect it to take (something like CAST(value AS DECIMAL(10,2)), but you'll probably need to deal with the extraneous non-numeric characters somehow as I'm not sure what the CAST will give if you don't strip them).
Create a second column without the $-sign, sort on that one and use the data of the original column in your application.
In order to create the helper column and sort on it you would need something like this:
SELECT value, CAST(SUBSTR(value, 2) AS UNSIGNED) sort_col
FROM unnamed_table ORDER BY sort_col