MySQL select bit field, select value with 0 in front - mysql

I have an row in my table from my database that contains bits, it is type bit with 4 chars long, bit(4). In this field all my value are stored like this:
0001
0010
0100
1111
I have tried to select and obtain the exact representation, with the 0 in front of 1 but without luck. I have tried to select the values from the bit field like this:
SELECT BIN(field+0) FROM `table` WHERE value=0001;
the result is: 1, how can i obtain 0001;
I need this value like so because i want to do bitwise operations in PHP. Thank you in advance.

Use LPAD() in mysql
SELECT LPAD(field,4,'0')
FROM `table` WHERE value=0001;

try this:
select right(concat(field,'0'),4) from yourtable where bcol='0001'

Related

Return strings which contains number on specific position

I am basic on SQL queries and I need some help.
I have to select all string values which contains number e.g. 7 only on specific position in that string.
For example:
I have string: 987654321 and if on position 3 I will have number 7, then it should be selected.
So in example this string will be selected, because on 3rd position I have number 7.
Is there any SQL function for that, or something which could help me?
EDIT:
Example table
TABLE
Numbers Value
987654321 1
123456789 2
789009871 3
654321092 4
847949372 5
Output:
TABLE
Numbers Value
987654321 1
847949372 5
Statement:
SELECT table.numbers
FROM TABLE
WHERE substr(table.numbers,3,1)='7' <--- what to do here? --->
Many thanks in advance.
For a regex option, you may use MySQL's REGEXP operator:
SELECT *
FROM yourTable
WHERE num REGEXP '^[0-9]{2}7';
On Oracle, you could use REGEXP_LIKE:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(num, '^[0-9]{2}7');
You should use case statement.
select case when substr(stringcol, 3,1) = '7' then stringcol else "not valid" end as stringcol from <Table Name>

Treat number as Zero in mysql query

If have rows table like this
id content
1 this five lenght is 12345
2 this five lenght is 23456
3 this six lenght is 234567
4 this six lenght is 238567
Then when I want to select and group by content, it will result
SELECT * FROM table GROUP BY content
1 this five lenght is 00000
3 this six lenght is 000000
Is there a way to achieve this, that can replace number with zero in mysql query?
Many thanks in advance.
Replace numbers with zero and then group them.
Example for replacement:
Select REGEXP_REPLACE('Stackoverflow 2456','[0-9]','0')
Stackoverflow 0000
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=db93de9c1c965090f46b4dbb1f48a63e
In your case:
SELECT REGEXP_REPLACE(CONTENT, '[0-9]','0') FROM TABLENAME GROUP BY REGEXP_REPLACE(CONTENT, '[0-9]','0')
Be careful tho this will probably do a full TABLE scan, so it will be really slow on large tables.
You can try with REGEXP_REPLACE() function - it'll work on mysql 8+ version
SELECT id, REGEXP_REPLACE (content,'[0-9]', '0') as content
FROM tablename GROUP BY REGEXP_REPLACE (content,'[0-9]', '0')

MySQL sorting with alphanumeric prefix

I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?

MySQL VARCHAR Type won't CONVERT to Integer

I have a column of data of type VARCHAR, that I want to CONVERT or CAST to an integer (my end goal is for all of my data points to be integers). However, all the queries I attempt return values of 0.
My data looks like this:
1
2
3
4
5
If I run either of the following queries:
SELECT CONVERT(data, BINARY) FROM table
SELECT CONVERT(data, CHAR) FROM table
My result is:
1
2
3
4
5
No surprises there. However, if I run either of these queries:
SELECT CONVERT(data, UNSIGNED) FROM table
SELECT CONVERT(data, SIGNED) FROM table
My result is:
0
0
0
0
0
I've searched SO and Google all over for an answer to this problem, with no luck, so I thought I would try the pros here.
EDIT/UPDATE
I ran some additional queries on the suggestions from the comments, and here are the results:
data LENGTH(data) LENGTH(TRIM(data)) ASCII(data)
1 3 3 0
2 3 3 0
3 3 3 0
4 3 3 0
5 3 3 0
It appears that I have an issue with the data itself. For anyone coming across this post: my solution at this point is to TRIM the excess from the data points and then CONVERT to UNSIGNED. Thanks for all of the help!
FURTHER EDIT/UPDATE
After a little research, turns out there were hidden NULL bytes in my data. The answer to this question helped out: How can I remove padded NULL bytes using SELECT in MySQL
What does SELECT data, LENGTH(data), LENGTH(TRIM(data)), ASCII(data) FROM table return? It's possible your numeric strings aren't just numeric strings.
Alternately, are you using multi-byte character encoding?
I believe the query you have is fine; as it worked for me: sqlfiddle.com/#!2/a15ec4/1/3.
Makes me think you have a data problem. Are you sure there's not a return or space in the data somewhere?
you can check the data by trying to do a length or a ascii on the data to see if you have more than expected:
select ascii(data) from foo where ascii(data) not between 48 and 57 or
select length(data) as mLEN from table having mlen>1 for length.
I believe this is the correct form:
SELECT CAST(data AS UNSIGNED) FROM test;
SELECT CAST(data AS SIGNED) FROM test;
Tested here: http://sqlfiddle.com/#!8/8c481/1
Try these syntax
SELECT CONVERT(data, UNSIGNED INTEGER) FROM table
or
SELECT CAST(data AS UNSIGNED) FROM table

How to: Select from MYSQL text field type only the numbers that start with 89 using REGEXP?

I have tried in many ways to select from text fields only the numbers that start with 89. I don't have a fix length after the first 2 numbers.
How can I do this to work properly and not get numbers like 389xxxxxx in results, for example. THE minimum length should be at least 8 characters.
Thank you!
If your column is integer, then you can probably do something like:
select * from my_table where cast(int_column) as char) like '89______%'
(that's 6 underscore characters before the percentage char)
If it's character value, then you can do this:
SELECT * FROM mytable WHERE char_column REGEXP "^89[[:digit:]]{6,}$"
If your column is numeric with decimal places and you want only integer values, then you need to do something like
SELECT * FROM mytable WHERE cast(numeric_column as char) REGEXP "^89[[:digit:]]{6,}$"
Edit: It seems Tim has edited his answer, which I referred to, so I edited my answer to include his code for column of character type.
SELECT * FROM foo WHERE bar REGEXP "([^0-9]89[0-9]*)|(^89[0-9]*)"
SELECT * FROM mytable WHERE mycolumn REGEXP "(^|[^[:digit:]])89[[:digit:]]{6,}";
will do this:
( # Either match
^ # the start of the string
| # or
[^[:digit:]] # any character except a digit
) # End of alternation.
89 # Match 89
[[:digit:]]{6,} # plus at least 6 more digits.