I have to search a DB2 database for a string that is a concatination of two columns. Is there a way to CONCAT the columns then use the combined string in the WHERE clause? I've searched but can't seem to find an answer to this.
Thanks
I have to search a DB2 database for a string that is a concatination of two columns. Is there a way to CONCAT the columns then use the combined string in the WHERE clause? I've searched but can't seem to find an answer to this.
Thanks
Something like
SELECT *, CONCAT(column1, column2) AS s FROM table WHERE column3 = s
After a bit more searching in the IBM manuals I came up with this solution that works for me;
Select
Col1 || Col2 as MyCol1
Where CONCAT(RTRIM(Col1),(RTRIM(Col2)) = 'searchvalue'
Thanks for the help
The simplest solution works, no need to use CONCAT()
.
SELECT Col1 || Col2 as MyCol1
FROM MY_TABLE
WHERE Col1 || Col2 = 'searchvalue'
Just beware that the search will not be indexed. If you want it to be indexed, you have to use an index based on a function. Then 2 possibilities :