Solved

# help to understand a SQL update statement with DIGITS and SIGN

Posted on 2010-11-17
352 Views
I am trying to understand a SQL update statement with an application we use. Can someone break this down and explain what it is doing? It is used on an DB2 database

The output value is always either a 4 digit string (eg '0003') or null.

Update Table1 set fieldA= SUBSTR(DIGITS(N.INPUT)||'####',7+SIGN(SIGN(1-10000)+1)*4,4) Where record = ......

0
Question by:JDCam
• 2

LVL 45

Accepted Solution

Kdo earned 500 total points
ID: 34160567
Hi JD,

Easy enough.

The DIGITS function returns an integer value as a string.  The length of the returned string is dependent upon the datatype and is leading zero filled if necessary to ensure that the string is as long as the longest possible string for that type.,

The SIGN function returns -1, 0, or 1, depending on the sign of the argument.

SUBSTR(DIGITS(N.INPUT)||'####',7+SIGN(SIGN(1-10000)+1)*4,4)

N.INPUT is the only variable in the equation.

If N.INPUT is a short integer (5 digits) with a value of 12, the first parameter to SUBSTR becomes '00012####'.

7+SIGN(SIGN(1-10000)+1)*4
7+SIGN(SIGN(-9999)+1)*4
7+SIGN(-1+1)*4
7+0*4
7

So if N.INPUT is 12, the call to SUBSTR becomes

SUBSTR ('000012####', 7, 4);

It looks like someone deliberately obfuscated some code here.

Good Luck,
Kent
0

Author Comment

ID: 34160702
Wow !! I don't feel so bad for not understanding this one. And if nothing else, I learned A new word today

If we said the the longest possible string is only 4 char. Then would it make sense that the trailing ',4' is intended to output at 4 characters.

Thus input 3 would output 0003 which matches the behavior today.

0

Author Closing Comment

ID: 34163803
Thanks!
0

## Featured Post

### Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…