Convert output number format with a single query - mysql

I am running this query through a unix command line:
SELECT X FROM TABLEX WHERE NAME LIKE 'Blablabla';
The output result should be 304768764529, however a get as return 3.047E11.
How can I, with a single query, convert the result to be the exact number, not in the scientific mode?

If using Oracle RDBMS, you can do it this way:
SELECT to_char(X)
FROM TABLEX
WHERE NAME LIKE 'Blablabla';
I'm curious why you're using LIKE without wildcards though.

Related

Match Numeric value after comma separated, concatenated by underscore values using MYSQL/MariaDB & REGEXP_SUBSTR

I have field column values stored like:
texta_123,textb_456
My SQL:
SELECT *
FROM mytable
WHERE 456 = REGEXP_SUBSTR(mytable.concatenated_csv_values, 'textb_(?<number>[0-9]+)')
NOTE: I'm aware there are multiple ways of doing this, but for the purposes of example I simplified my query substantially; the part I need to work is REGEXP_SUBSTR()
Effectively, I want to: "query results where an id equals the numeric value extracted after an underscore in a column with comma-separated values"
When I test my Regex, it seems to work fine.
However, in MySQL (technically, I'm using MariaDB 10.4.19), when I run the query I get a warning: "Warning: #1292 Truncated incorrect INTEGER value:textb_456"
You should seriously consider fixing your database design to not store unnormalized CSV data like this. As a temporary workaround, we can use REGEXP_REPLACE along with FIND_IN_SET:
SELECT *
FROM mytable
WHERE FIND_IN_SET(
'456',
REGEXP_REPLACE(concatenated_csv_values, '^.*_', '')) > 0;
The regex trick used here would convert a CSV input of texta_123,textb_456 to just 123,456. Then, we can easily search for a given ID using FIND_IN_SET.

Capture a number in a string

I store a serialized table in a Mysql column. The data looks like this:
a:2:{i:116;s:1:"4";i:113;s:1:"6";}
I'm trying to extract the number that's always placed before ;s:1:"6";
In the example above, the number would be 113.
I'm running Mysql 5.7, so I don't have access to new regex functions. It would have been nice to access PREG_CAPTURE and just write
PREG_CAPTURE(\'i:([0-9]+);s:1:"6";\', mycolumn, 1)
But I can't use PREG_CAPTURE and I'm stuck. Any idea?
Use substring_index.
Try this
SELECT SUBSTRING_INDEX( field1, ';s:1:"6";',1 )

MySQL underscore with LIKE Operator

I have an SQL query which matches results using a LIKE :
_column_name_%
This will return results where the column name is:
_column_name_1
_column_name_2
The end number could be a really high number e.g. 32523, or even 523624366234.
If I use _column_name_%%%%% this would match 32523, but not 523624366234.
How would I get the LIKE to match without typing % repeatedly?
A Simple select query with the LIKE operator should look like this
You have to escape the underscore using "\" if you are having any.
instead of pretext_% use pretext\_%
Select * from mytable where field_1 like 'pretext\_%'
This will return pretext_1 as well as pretext_11

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 separate the data from selected data

I have a column in my table name as URL and it contains Multiple value like "https://www.google.com/#q=how+to+make+a+android+app"
and
http://www.bing.com/search?q=how+to+make+a+android+app&go=&qs=n&form=QBLH&pq=how+to+make+a+android+app&sc=8-15&sp=-1&sk=
I want to get data separately in output like
website = https://www.google.com
Keyword = how to make a android app.
Any Idea Plz, How can i get this in MySql.
you can use substr() function in php to cut string. In your case, you can get the characters up to the end of 'https://www.google.com'.
read more at http://ro1.php.net/substr
in case you want to do it directly in your query, you can use substr() as well. Sql syntax goes like this:
SELECT SUBSTR(column, start_position, desired_length) FROM table_name
*SELECT SUBSTR(column_name, 1, 20) FROM table_name;*