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

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.

Related

Select a specific part of a value from a column in mariadb

i´m new in SQL and want to solve following case.
I have a table in a MariaDB named inventories. In this table are 2 columns: identifier and data.
I want to select and output a specific part in the column data, if the identifier is a specific one.
For example:
identifier
data
steam:xxxxxxxxxxxxxxx
...some data...,"Serial":"ZF3CJ8L1rrKjwP7nKTzb",...some more data...
The only part, that i want in the output is this: "Serial":"ZF3CJ8L1rrKjwP7nKTzb" but the part behind "Serial":" is a random value.
How can i solve this?
(i put a screenshot in here, table does not work for me in editorscreenshot of table in stackoverflow editor.)
You can try using the SubString function, which only returns a certain amount of character and starts a whatever character you want.
I got it!
SELECT (SUBSTRING(data, LOCATE('"Serial":"', DATA) + 0, 31)) FROM inventories where identifier = 'steam:xxxxxxxxxxxx';

How do I convert binary data from MySQL to a string in ColdFusion 11?

I have a field in my MySQL database that is set to data type "BIT". The value in the field is 101101. I am trying to read this value using Coldfusion (version 11)
I simply use the following code:
<cfquery name=q1 datasource=#data_source#>
select * from mytable
</cfquery>
<cfoutput>
#q1.mybitfield#
</cfoutput>
I have tried using CAST and CONVERT on the MySQL side and I have tried CharSetEncodeing on the CF side along with every option of ToString, ToBase64, and ToBinary that I can think of.
I still can not get my output to look like it does in the database.
Actually, I was probably thinking of SQL Server's bit type. For MySQL, a simpler option might be to use the bin() function which:
Returns a string representation of the binary value of N, ...This is equivalent to CONV(N,10,2). Returns
NULL if N is NULL.
For example:
SELECT bin(YourBitColumn) AS YourBitColumn FROM YourTable
... or
SELECT bin(YourBitColumn+0) AS YourBitColumn FROM YourTable
NB: High-order 0 bits are not displayed in the converted value. That applies the CF function as well.
Thanks Leigh for your help. I was never able to get it to work with just one step so I used a two step solution (if you want to call it that). What I ended up doing was setting up my MySQL statement to convert the field to an Unsigned interger (although Decimal or a signed integer would have also worked), then once I read in the decimal value I was able to convert it to binary using your suggestion of FormatBaseN(q1.myfield,2). So thanks for the reminder of FormatBaseN. I had forgotten about that one.
My final code ended up looking like this:
The MySQL statement:
SELECT *, CONVERT(item , UNSIGNED) as di from mytable
And the Coldfusion looked like this:
<cfset d = FormatBaseN(q1.di, 2)>
Edit
After writing this, I decided to go with Leigh's answer above since it was a better solution.

SQL Where Clause with Cast or convert doesnt work

I've a table on ArcGis which contains nummbers and dates. I need to filter these via a sql-query. I just have the possibility to change the where clause.
See here: https://services3.arcgis.com/rKOPqLnqVBkPP9th/arcgis/rest/services/Arbeitsmappe1/FeatureServer/0//query
Just type in the where clause 1=1 and outfield * then you will get all results.
I have to filter installierte_leistung which contains numbers in the following formats:
1.050,20 ; 18; 0,1 ; 1.230
and dates of following format: 11.04.08
wished filters:
installierte_leistung: I want to execute a sql-statement like: where (installierte_leistung BETWEEN '1' AND '2'). In the result there is also the 18. Or if I ask for values greater 10 it shows me also the 1.050,20.
I tried to convert with cast and convert to decimal, signed, unsigned, integer and so on, but the query has been always invalid. I tried with 'number' and with number and with "number". lowercase and uppercase and almost all thinkable possibilities. I get no results with cast or convert.
Same issue with the Date. I want to filter monthly. so means between 01.2008 and 09.2009 for instance.
Could someone please help me? Thanks a lot!
Falk
I had a similar problem in the past with nested query. The more database specific queries (like cast and so) don't work because ArcGIS server is by default configured to work only with standardized queries. If you need to use more specific queries you have to change "standardizedQueries": "false" in server setting, check here how (bottom of the page): http://resources.arcgis.com/en/help/main/10.2/index.html#//015400000641000000. Should work for you. Good luck.

Selecting a Point datatype from MYSQL

Good Day,
I am trying to get a point from an SQL column. CUrrently it is encoded as a string of characters: Gþ`C#Ëóàî¬Ë]À
So is there any way to actually decode it to get the actual value?
Use MySQL's AsText() function:
SELECT AsText(myPoint) FROM myTable

extract data in mysql from the string

I am trying to run mysql query to get the part of string below is string stored in database
nb=5&pfid=2098&rssurl=http%3A%2F%2Ffeeds.feedburner.com%2Ffoxnews%2Flatest&nb=5
using mysql query i need to extract the pfid which is '2098' and insert it into other table field. i tried few queries but i am not able to achieve it.
anybody have idea how to do this may be mysql expert...or sql query expert.
thank you in advance
regards,
mona
You can do this with instr and substring:
select left(val, instr(val, '&')-1)
from (
select substring(col,instr(col, 'pfid=')+5,length(col)) val
from yourtable
) t
SQL Fiddle Demo
This does assume the string value will contain an & after the value. If not, you could include a little additional logic to check.
In regards to inserting that value into another field, should be fairly straight-forward now that you have the value. Depends on your exact needs.