Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

help to understand a SQL update statement with DIGITS and SIGN

Posted on 2010-11-17
3
369 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:
Kent Olsen 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 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