3

I have this query

select name || ' ' || sure_name from users

which has this result

'test test'
'test1 test1'
...

I need to create a filter for this query. But I'm wondering what would be best way of creating it. I came up with this:

select name || ' ' || sure_name from users
where name || ' ' || sure_name = 'test test'

But I'm wondering how efficient will be this query as the concating happens twice (in the select statement and also in the where statement)

EDIT

the filter could look like

like '%test t'
1
  • 2
    You should rename that column to sur_name. A sure name is something different than a surname ;)
    – user330315
    Jan 29, 2013 at 14:11

5 Answers 5

4

The concatenation itself is not the problem, but the access to the rows.

For example, if you have an index on sure_name or on name, it is better to query with separate columns.

select name || ' ' || sure_name from users
where name ='test' and sure_name = 'test';

But if you don't have any indexes, don't bother. The performance will be roughly the same with your query.

However, if you add an index

create index fbi_full_name on users(name || ' ' || sure_name)

your query will perform better.

1
  • 1
    I've seen the edit. Such queries(starting with '%') can't use the index. The index can be used with queryes with like 'Jo%' (knowing the starting pattern) Jan 29, 2013 at 14:14
3

In such a case, I prefer to express this using a subquery:

select *
from (select (name || ' ' || sure_name) as newname
      from users
     ) t
where newname = 'test test'

That way, my where logic is a little bit more isolated from the variable creation logic. I find it easier to read and maintain.

3

Test each field separately so that if you have indexes on name or sure_name then these indexes can be used more efficiently:

SELECT name || ' ' || sure_name
FROM users
WHERE name = 'test'
AND sure_name = 'test'

Note also that if the name or the suname could contain spaces then the two queries can give different results.

1
  • @user49126 Mark Byers told you the best solution. Please explain the correction of the question.
    – knagaev
    Jan 29, 2013 at 14:52
1
select *
from (
   select name || ' ' || sure_name as fullname
   from users
) 
where fullname like '%test t'
0
0

Try this Query :

select name || ' ' || sure_name 
from users
where (name || ' ' || sure_name) like '%test t'
4
  • 1
    I tried it on SQL SERVER R2 it worked. Here is my SQL FIDDLE for SQL SERVER R2. UPDATE It also worked on Oracle 11g R2, here is the SQL FIDDLE for ORACLE 11g R2 Jan 29, 2013 at 14:29
  • 1
    Sure it works. but: 1. is the same query as OP. 2.Does not add any explanation, nothing new to user. 3. you have an upvote. I downvoted. Improve your response with something useful. And, 4: Don't be furious. :) I got downvotes also. Some users don't feel useful some answers :) Jan 29, 2013 at 14:40
  • @FlorinGhita AGREED, But actually i didn't saw the OP. Jan 29, 2013 at 14:41
  • That's your fault, his query was there at the beginning :) Jan 29, 2013 at 14:42

Your Answer

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

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