0

I have a city column field in my table with city names starting with @,&,*,',$...(example- %york,$pradesh,@lorida and also having values delhi,New york...)

My requirement is to only getthe city names(like delhi,New york) in the field starting only with Alphabets from A-Z.

Please help me out.

2
  • 4
    Which DBMS are you using ?
    – A Hocevar
    Commented Aug 24, 2015 at 6:22
  • Added mysql tag based on the accepted answer.
    – user330315
    Commented Aug 24, 2015 at 6:58

4 Answers 4

1

Depending on your DBMS system you can use regular expressions in WHERE clause. For example, PostgreSQL provides them like this.

SELECT * FROM cities WHERE name ~* '^[A-Z]';

MySQL/MariaDB provides them as well (see here).

1

Following should work in MSSQL:

SELECT * FROM <TABLENAME>
WHERE LEFT(<COLUMNNAME>,1) IN ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')
0

You can do this with the REGEXP (Regular Expression):

SELECT * FROM cities where name REGEXP '^[A-Za-z]';

It now gets you the lines only Starting with A-Z and a-z:

Before:

enter image description here

After:

enter image description here

2
  • Hi All, Thanks for the quick replies.....I have many special characters in my city field name starting with like !@#$%^*&+.., so i cant search for each special character so I need to get only city names starting with Alphabet A-Z ..and special mention- i have other conditions in my where clause with my current condition. and my table contains 5billion records
    – Ramya
    Commented Aug 24, 2015 at 7:03
  • I am using Hadoop Hive
    – Ramya
    Commented Aug 24, 2015 at 7:14
0

If you don't want the City name to contain any symbols, you can proceed as below, using the CONTAINS function;

SELECT City
FROM   MyTable 
WHERE NOT CONTAINS(City, '$')
AND   NOT CONTAINS(City, '%')

And in case, you only want to verify for the first character, you may proceed as below;

SELECT City
FROM   MyTable 
WHERE SUBSTRING(City, 1, 1) NOT IN ('$', '%', '@')
2
  • Hi All, Thanks for the quick replies.....I have many special characters in my city field name starting with like !@#$%^*&+.., and soo on.
    – Ramya
    Commented Aug 24, 2015 at 6:54
  • Hi.. You can still add them in the second query above. In any way, you'll need to specify the characters you'd wish, except if using RegEx.
    – Nadeem_MK
    Commented Aug 24, 2015 at 7:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.