0

Here I'm using postgres 9.6 And PFA is my table schema for postgres

id  username
----------
1   ashesh 
2   123456

While doing a query on a username with '123456' it will not gives the response but when doing a query with 'ashesh' it will give a response. My query is

select * from customer where username = '123456';

Gives me a blank response but while querying with 'ashesh'

select * from customer where username = 'ashesh';

it will give the response.

3
  • How can we reproduce this behavior? Commented Jun 20, 2018 at 13:08
  • 2
    Maybe some trailing spaces. Does where username like '123456%' work? Do you have an unique index on that column? What is your exact Postgres version (select version() will tell you). Commented Jun 20, 2018 at 13:09
  • You can also try SPLIT_TO_ARRAY to check the string for weird characters Commented Jun 20, 2018 at 13:45

2 Answers 2

1

Perhaps, sometimes you keep a blank in the username value(it seems for 123456),

trim may be used in the where condition for username column :

create table customer( ID int, username varchar(125));
insert into customer values(1,'ashesh');
insert into customer values(2,'123456 ');

create table customer( ID int, username varchar(125));
insert into customer values(1,'ashesh');
id  username
1   ashesh

select * from customer where trim(username) = 'ashesh';
1   ashesh

select * from customer where username = '123456';
--no results return

select * from customer where trim(username) = '123456';
id  username
2   123456

SQL Fiddle Demo

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

1 Comment

using trim is works, but why this type of issue is created, bcoz I had updated same value but gives no result. Any reason behind that?
0

Your problem would seem to be hidden characters -- either in the where constant or in the column itself. If the first, then simply re-typing the code should fix the problem.

The second requires more investigation. I would start with:

where username like '%123456%'

to see if any usernames contain that sequence of characters. If this finds the row, then start looking for hidden characters. If not, then try:

where username like '%1%2%3%4%5%6%'

This will look for hidden characters between the digits. Once you have determined what the issue is, you can figure out how to deal with the problem.

If you have a fixed set of characters that are allowed, you can also test:

where regexp_replace(username, '[^a-zA-Z0-9]', '', 'g') = '123456'

If this is the problem, you can remove the hidden characters with:

update customer
    set username = regexp_replace(username, '[^a-zA-Z0-9]', '', 'g')
    where username ~ '[^a-zA-Z0-9]';

2 Comments

this will work, but I want an exact match in the column value. This type of issue creates while the only numeric value is present in the row. otherwise, it will work fine.
@AsheshKhatri . . . You need to find the cause of the problem, if you want your queries to work.

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.