Solved

help to understand a SQL update statement with DIGITS and SIGN

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now