Tuesday, May 5, 2009

How to use string functions to make an SQL join

I am trying to make an inner join on columns in which a value is stored differently, for example, in one column it is application:username and in another it is username (not starting with application). Can you explain how to do this with an example?

This is accomplished by using string functions to extract the username from the application:username values.

Consider these sample tables:

Table1      Table2
username username
Tom asdf:Tom
Dick asdf:Dick
Harry asdf:Harry
qwerty:Tom
qwerty:Dick
qwerty:Harry
asdfTom
Tom
oops:

The SUBSTRING function, which extracts a substring from a string value, will be needed. But what if there are application values of different lengths? Then we need to take the substring starting at a different point in the username column, and this will vary depending on where the colon is.

This is a job for the POSITION function.

SELECT Table1.username
, Table2.username
FROM Table1
INNER
JOIN Table2
ON SUBSTRING(Table2.username
FROM POSITION(':' IN Table2.username) + 1
) = Table1.username

Here, the POSITION function finds the position of the colon in Table2.username. For asdf:username it's in position 5, and for qwerty:username it's in position 7. By adding 1, we begin extracting the substring at the next character. Since there is no FOR length parameter specified, the substring goes all the way to the end. The extracted substring is then compared to Table1.username to match rows.

If the Table2.username value does not contain a colon, however, then the POSITION function returns 0 as the position. By adding 1, we begin extracting the substring at the first character. Thus the entire value will be compared to Table1.username to match rows. This may or may not result in a match, depending on the data, but at least the query will run. We need to add 1, simply because a FROM 0 value for the SUBSTRING function will usually fail.

Another problem is if the Table2.username value contains a colon but nothing after it. Then the POSITION value will be equal to the length of the string, and the FROM value will be 1 greater than that, so the SUBSTRING function might fail again. To get around this, just tack an extra space onto the Table2.username value:

    ON SUBSTRING(Table2.username || ' '
FROM POSITION(':' IN Table2.username) + 1
) = Table1.username

If the colon is in the last position, then the SUBSTRING function will return the space. Of course, this probably won't match any Table1.username, so this is fairly safe. And luckily, trailing spaces do not make a difference when it comes to matching values, so the other rows will continue to join properly.

Tuesday, November 18, 2008

SAP ABAP Development Quiz

Que 1. As a beginner with the ABAP Workbench, you will want to...
  • a) Read data from tables
  • b) Write data to tables
  • c) Create or change tables
  • d) All of the above
Que 2. The settings in the Data Browser/Table View Maintenance field...
  • a) Regulate the transport of the table's data records during installs, upgrades, copies
  • b) Regulate the scope that you may use to display and maintain data records
  • c) Answers A & B
  • d) None of the above
Que 3. Before activating a data element you should?
  • a) Preform a consistency check
  • b) Select "buffering not allowed"
  • c) Click "execute"
  • d) None of the above
Que 4. What are the two elementary data types provided for character strings in ABAP?
  • a) a and b
  • b) x and y
  • c) p and f
  • d) c and n
Que 5. The number of your fields has reached unmanageable levels and you can no longer be sure that the fields have the correct content at runtime. What tool will help you with this problem?
  • a) Transformation Editor
  • b) Performance Trace
  • c) ABAP Debugger
  • d) Runtime Analysis
Que 6. Set more breakpoints in the ABAP Debugger by...
  • a) Double clicking a line
  • b) Utilities - Breakpoints - Display
  • c) F8
  • d) None of the above
Que 7. A "branch" corresponds to...
  • a) A statement that contains "ELSE"
  • b) An "IF" statement
  • c) The "either-or" statement in normal language
  • d) All of the above
Que 8. In the SAP system you can have internal tables in...
  • a) The database and memory
  • b) Memory and the formatting lists
  • c) The database and toolbars
  • d) All of the above
Que 9. Modularization in ABAP is important because...
  • a) You are debugging more often than necessary
  • b) You are manually writing all code by yourself
  • c) You are splitting complicated solutions into small components
  • d) None of the above
Que 10. What does ABAP stand for?
  • a) Advanced Business Application Programming
  • b) All Banthas are Portentous
  • c) Allgemeiner Berichts-Aufbereitungs-Prozessor
  • d) Answers B & D