SQL Zone is brought to you in partnership with:

I am the founder and CEO of Data Geekery GmbH, located in Zurich, Switzerland. With our company, we have been selling database products and services around Java and SQL since 2013. Ever since my Master's studies at EPFL in 2006, I have been fascinated by the interaction of Java and SQL. Most of this experience I have obtained in the Swiss E-Banking field through various variants (JDBC, Hibernate, mostly with Oracle). I am happy to share this knowledge at various conferences, JUGs, in-house presentations and on our blog. Lukas is a DZone MVB and is not an employee of DZone and has posted 233 posts at DZone. You can read more from them at their website. View Full User Profile

Simulation of TRUNC() in Derby

  • submit to reddit

Derby is missing out a lot of functions from the set of functions that other databases usually provide. One example is the TRUNC(value, decimals) function. According to the Wikipedia, truncation can be achieved as such:

-- trunc(x, n) 
CASE WHEN x > 0 
  floor(power(10, n) * x) / power(10, n) 
  ceil(power(10, n) * x) / power(10, n) 

Unfortunately, there is no POWER(base, exponent) function in Derby either. But no problem, we can simulate that as well. Let’s consider the Wikipedia again and we’ll find:

power(b, x) = exp(x * ln(b))

If we substitute that into the original simulation, we get for Derby:

-- trunc(x, n) 
CASE WHEN x > 0 
  floor(exp(n * ln(10)) * x) / exp(n * ln(10))
  ceil(exp(n * ln(10)) * x) / exp(n * ln(10)) 

Verbose, probably quite inefficient, but effective! Let’s run a short test, too:

create table test (x numeric(10, 5), n int);

insert into test values (11.111, 0);
insert into test values (11.111, 1);
insert into test values (11.111, 2);
insert into test values (11.111, -1);

  x, n, 
  case when x >= 0
    floor(exp(n * ln(10)) * x) / exp(n * ln(10))
    ceil(exp(n * ln(10)) * x) / exp(n * ln(10))
  end "trunc(x, n)"
from test;

The above yields

11.111 0 11
11.111 1 11.1
11.111 2 11.11
11.111 -1 10





Published at DZone with permission of Lukas Eder, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Herry Johnson replied on Tue, 2012/06/12 - 2:05pm

Assuming the combo box is initially empty, when the user types 'a' you have to fetch all the 'a's. When he types the next letter, you already have all the 'a's in the combo box model, so all you have to do is filter them, and so on for any subsequent letters. You only have to fetch from the database when he types, or retypes, the first letter.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.