Skip to content
Advertisement

How to compare trimmed variable in SQL statement?

I have a SQL statement which includes three database tables. I am comparing some IDs and DESCRIPTIONs and now I need to compare results FIRSTNAME, MIDDLENAME and SURNAME.

Variable UGOVARATELJ_OSIGURANJA is actually name of the client in form NAME MIDDNAME SURNAME, with spaces between each part of the name. My idea was to remove those spaces with REMOVE() but when I compare after that with second table there is no correct result.

I tried to test my statement with this: THIS IS ONLY TEST STATEMENT

Notice that I had to set space between name and middname LIKE %IVAN F% and correct way would be %IVANF%.

Any idea how to solve this? Thanks in advance

EDIT: I am doing this replacing (trimming) because some of the peops on my lists have not MIDDLENAME

EDIT: This is working statement for names which has MIDDLENAME. The statement is not working properly when someone has no MIDDLENAME because of one space too much.

Advertisement

Answer

This is working statement for names which has MIDDLENAME. The statement is not working properly when someone has no MIDDLENAME because of one space too much.

You can make the extra space conditional:

This will remove the potential for false matches from removing the spaces.

You could simplify this slightly by removing one unnecessary level of subquery:

or change it to use joins instead – I think this is equivalent:

Also check the data type of DESCRIPTION1 – if that is actually a number then you should use ACD.DESCRIPTION1 IN (1, 2) rather than forcing an unnecessary conversion between number and string.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement