How to search on a varchar column using a number? - mysql

I have a varchar(30) column that looks like this:
953-41
975-12
952-13
934-34
All numbers of the column share the structure of: 3 numbers and a dash followed by more numbers.
I want to make a query that works like SELECT * FROM tablename WHERE value = 95341
And get '953-41' only using numbers in the WHERE clause.
I can't change my database and remove the dash, I need to search with a numeric value on rows that mix the numbers I want with a dash in between.

you can try:
MYSQL:
SELECT * FROM tablename WHERE value = INSERT(95341,4,0,'-')
SQL Server:
SELECT * FROM tablename WHERE value = STUFF(95341,4,0,'-')

You can use this:
SELECT *
FROM yourTable
WHERE CAST(REPLACE(colName, '-', '') AS UNSIGNED) = 95341

Related

How to SELECT w/ LIKE and a '_' delimiter

I have a table with a name field that can have values like:
CHECK_5_20170909
CHECK_1_20170809
CHECK_11_20170809
CHECK_11_20170909
I would now like to query all fields that have a _1_ in the name, but ONLY them.
I tried this: SELECT * FROM tblName WHERE name LIKE '%_1_%';
but that shows me _11_ AND _1_ in my results.
When I try it with CHECKWHATEVER1WHATEVER20170909 and LIKE %WHATEVER1WHATEVER% it works, are there any special rules for _ in a MySQL Query?
Changing it to another delimiter in the MySQL DB would create a hell of work, is there any "workaround"?
You need to add a '\' before each underscore, otherwise its interpreted as a random "wildcard" character.
select * from
(
select 'CHECK_5_20170909' col
union
select 'CHECK_1_20170809'
union
select 'CHECK_11_20170809'
union
select 'CHECK_11_20170909'
) t
where col like '%\_1\_%'
try this using REGEXP
SELECT * FROM tblName WHERE name regexp '_1_';
it will return exact matches record from column for more reference read here

Query data using first 3 and last 3 characters from a ten character word

I have data set of about 10K alphanumeric words with 10 characters length each. I need to match these using the first 3 characters and the last 3 characters.
Example: BGP12BR2010
In this case, I should use only BGP and 010 and see if there are any entries in my database. I have used
LEFT(replace(term_id,' ',''),3)||RIGHT(replace(term_id,' ',''),3)
Is there any other way to get this done.
You can also use LIKE:
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP%210';
this matches on all string, not only 10 CHAR. to specify the lenght you can
use underscore
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP____210';
A better way for this is to add 2 virtual persitent fields, where Mysql calculate the values and you also can set a index on it for a better performance and not using a full table scan
add persistent virtual fields
ALTER TABLE yourtable
ADD COLUMN first3 VARCHAR(5) AS (SUBSTRING('hallo',1,3)) PERSISTENT,
ADD COLUMN last3 VARCHAR(5) AS (SUBSTRING('hallo',-3,3)) PERSISTENT;
Now you can select it
SELECT * FROM yourTable where first in('BGP','YXZ','XXX) and last3 = '210';
I'll do so:
SELECT * FROM yourtable
WHERE LENGTH(yourcolumn) = 10
AND yourcolumn LIKE 'BPG%010';
To get all the values starting with 3 alphabets and ending with 3 numeric characters, use
select *
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
To extract them, if they follow the above pattern,
select val, substring(val,1,3) as first3, substring(val,-3,3) last3,
--concatenate them if required
concat(substring(val,1,3), substring(val,-3,3)) concatenated_string
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
Add a condition for length of the column if it has to be exactly 10 characters. In that case, change the regexp to '^[a-z]{3}.{numcharactersrequired}[0-9]{3}$' , which would be '^[a-z]{3}.{4}[0-9]{3}$'
SQL Fiddle

select data mysql

i have in my table places named field. there are space separated values(there are problem to store csv value in one field). now i want to fire query like below. how i can do ??
select * from tablename where variablename in places
i did try this way but it shows syntax error.
select * from tablename where variablename in replace(places,' ',',')
### places ###
bank finance point_of_interest establishment
Use FIND_IN_SET
For comma separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', variablename ) )
Refer : SQL Fiddle
For space separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', replace(variablename,' ',',') ) )
Refer : SQL Fiddle
The best solution would be to normalise your data structure and do not have a single field storing multiple values.
You can make a query work without normalisation, but any solutions would be lot less optimal from a performance point of view.
Use patter matching with like operator:
... where fieldname like '% searched_value %'
Use the replace() function and combine it with find_in_set():
... where find_in_set('searched_value',replace(fieldname,' ',','))>0
Hi I think your problem comes from the usage of IN
IN for MySql is used like this
SELECT *
FROM table_name
WHERE column_name IN (bank,finance,point_of_interest, establishment);
In case of you want to select places you need to specify each place into value like

MySQL REGEXP for numbers that begin with

I need to write a query using MYSQL REGEXP that will find me rows that have a certain column begin with 11 or 12 or etc. I know I can use LIKE or LEFT(####,2) but would like to use the REGEXP option. My data is stored as 110001, 122122, 130013a and etc.
EDIT 1:
To make it more clear, I would like to express
SELECT * FROM table WHERE column LIKE '11%' or column LIKE '12%' or column LIKE '30%'"
with REGEXP
Thanks!
To match rows that start with any two digits you can use:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^[0-9][0-9]';
If you only want to allow rows starting with 11, 12 or 30 then you could use this:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^(11|12|30)';
This will not be able to use an index on the column so it may be slower than using LIKE.

How do you select from mysql where last character in a string = x?

I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'