Solved

help to understand a SQL update statement with DIGITS and SIGN

Posted on 2010-11-17
3
364 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 45

Accepted Solution

by:
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

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
 

Author Closing Comment

by:JDCam
ID: 34163803
Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select record with the most recent date 14 57
T-SQL: Do I need CLUSTERED here? 13 45
SQL Query assistance 16 27
awk and Pythagoras? 5 8
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

803 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