0

I have this table(students):

id | name  | address
1  | hello | test451.1
2  | hello | test461.1

I want to select the rows that contain 451 in the address field exact before decimal and after alphabet string. Basically I want to filter 451 from address values. I am using the following query to get integer out of string.

SELECT * FROM `students` WHERE CAST(`address` AS UNSIGNED) = '451'

I still have no idea how to filter for a float.

Also tried this but didn't work :

SELECT * FROM `students` WHERE CAST(`address` AS DECIMAL(4,1)) = '451'

I can't use like %451% because there are other rows with test4451.1, test 5441.1 etc. I don't want to select them.

3 Answers 3

1

use substring and position to figure out 451

SELECT * FROM `students` WHERE  
substring(address,position('4' in address),4)= '451.'

here is the link i try with your data and it returns

create table students (
id int,name varchar(50),address varchar(300)
);
insert into students values(1,'hello','test451.1'),(2,'hello','test461.1'),(3,'hell','test 5441.1');

id  name    address
1   hello   test451.1

https://www.db-fiddle.com/f/c6byWF6ahoBusWQqqNtsSf/0

Sign up to request clarification or add additional context in comments.

1 Comment

There is one problem. Its also picking up the rows which has values like test4511.1 . It shouldn't select those.
0
SELECT CAST(25.65 AS int);

That is what W3schools says. That should get the integer out of string.

Comments

0

I think you want like:

where address like '%451%'

You can use:

where address like 'test451.%'

4 Comments

I can't use like because there are other rows with address as test4451, test5451 . I must use = .
@JasminderPalSingh . . . It is unclear what you are asking. Why not just use =?
Thanks for responding. Actually, I can't use = because the user input coming as integer(451) but field has value in string format having float. So i must filter exact results.
Please check Zaynul Abadin Tuhin answer. It worked but there is one issue as written in answer comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.