extract data in mysql from the string - mysql

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.

Related

In mysql where clause bigint showing unexpected result

Hope you all are doing well.
Today morning I was writing some sql query and I found a situation where I need your suggestion on that so here is the situation:
I've a table in mysql called users it has id column which is bigint
now when I'm trying to extract data with a query like :
select * from users where id = 123
in this case it'll show the result for user 123
now the situation here is if I run the same query like:
select * from users where id = 123b
now the issue here is it is still giving me the data for user 123
need your suggestion guys on the same, I did some R&D on the same but didn't found much usefull.
Thank you
Your question is tagged with mysqli which means you are using PHP to build your query. If you pass the string 123b as an integer, PHP will keep the leading digits:
var_dump((int)'123b');
# output: int(123)
MySQL then execute your query with this well formed integer 123.
If you execute a raw SQL query using any tool of your choice you should get an error like the following:
Unknown column '123b' in 'where clause'
This is because 123b cannot be an integer as it contains a letter, and isn't a string as it has no string delimiters.
Note that MySQL can still interpret strings without delimiters like hexadecimal values. For example, 0x31323362 is a valid string value for 123b and does not need quotes around it.

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.

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.

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

Mysql filter json data value range

This one is bit complicated than sinmple range in mysql ,
my field data is {"id":"15","value":"1200"} and I can find it by ,
SELECT *
FROM `my_table`
WHERE `my_field`
REGEXP '{"id":"15","value":"1200"}'
but what I need is the range for the value
so I need to look for range between 1000 > 2000 and field with json value 1200 should match.
I cant redo the output with php because it will eat up the resource , so please if it is possible within Mysql without me processing the data trough php on return
any help is appreciated!
SOLUTION TO ANYONE !
LC gave us very nice solution
REGEXP '{"id":"15","value":"(1[0-9][0-9][0-9]|2000)"}'
My knee-jerk reaction is to say "stop storing more than one value in a single column", but if you have to do it that way and you have regex power, use it (untested):
SELECT *
FROM `my_table`
WHERE `my_field` REGEXP '{"id":"15","value":"(1[0-9][0-9][0-9]|2000)"}'
Keep in mind, there's no way to optimize this query using indexes so you are stuck with examining every row in my_table.