Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

help to understand a SQL update statement with DIGITS and SIGN

Posted on 2010-11-17
3
Medium Priority
?
394 Views
Last Modified: 2012-05-10
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
Comment
Question by:JDCam
  • 2
3 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 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
 
LVL 1

Author Comment

by:JDCam
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
 
LVL 1

Author Closing Comment

by:JDCam
ID: 34163803
Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question