0
ID  MOBILE 
1   9869600733 
2   9869600793 
3   9869600799 

all id whose mobile number containing 9 three times(using string functions like replace, substr, etc)... ? (without like , % , etc)

4
  • 1
    Which DBMS you are using ? Commented Jul 23, 2017 at 13:31
  • 1
    exactly three times? or at least three times? Commented Jul 23, 2017 at 13:32
  • both exactly 3 & more than 3 as well... Commented Jul 23, 2017 at 13:33
  • And why not like? That is the appropriate way to do this. Commented Jul 23, 2017 at 13:40

2 Answers 2

1

You can use LEN and Replace

Where len(MOBILE)-len(replace(MOBILE ,'9',''))>=3

Note : Some DBMS uses LENGTH instead of LEN

Where length(MOBILE)-length(replace(MOBILE ,'9',''))>=3

  • replace(MOBILE ,'9','') will replace all the 9's with empty string
  • length(MOBILE) will count the number of characters in Mobile column
  • length(replace(MOBILE ,'9','')) will count the number of characters in Mobile column as replacing 9's with empty string
  • length(MOBILE)-length(replace(MOBILE ,'9','')) here the difference will tell the number of missing characters that is our 9, you can use this difference to count the 9
Sign up to request clarification or add additional context in comments.

16 Comments

can u pls tell me logic
i need another help from you
@PrashantTendulkar - yes
want to write a check constraint(while creating a table) which accepts value between 2 dates like ('25-oct-94' to '10-may-16')
create table dob5 ( birthdate date, CONSTRAINT check_dates CHECK (birthdate BETWEEN '25-oct-94' AND '10-may-16') ) this is how i tried @Pரதீப்
|
1

exactly three '9's:

Select * from mytable
Where len(mobile) - len(replace(mobile, '9', '')) = 3

at least three '9's:

Select * from mytable
Where len(mobile) - len(replace(mobile, '9', '')) >= 3

2 Comments

can you tell me logic of this ?
Yes, if you create a string that has all the 9s removed, then it's length will be the length of the unmodified string minus the number of 9s that were removed.

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.