Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search on a varchar column using a number?

Tags:

sql

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.

like image 291
Miguel Ángel Sánchez Avatar asked Dec 19 '25 11:12

Miguel Ángel Sánchez


1 Answers

you can try:

MYSQL:

SELECT * FROM tablename WHERE value = INSERT(95341,4,0,'-')

SQL Server:

SELECT * FROM tablename WHERE value = STUFF(95341,4,0,'-')
like image 120
Nolan Shang Avatar answered Dec 21 '25 04:12

Nolan Shang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!