简体   繁体   中英

How to modify SQL-Statement?

I have a problem that i can´t solve :(

I execute this sql statement to get the max of a the CCID-String:

                "select MAX(CCID) as test123 from "
                        + tabelleName+ " where CCID like 'W%'");

After that I want to increase the string. I substring the W and convert the string to an int:

                String ccid = rs.getString("test123");
                String test = ccid.substring(1,6);
                int id = Integer.parseInt(test);
                int newid = id+1;

At the moment the max(ccid) looks like W01352. The result of newid is "1353" without the "W0".

I want to get a string like W01353 that I could add to the db2-database for the next W-Number.

Do someone have any idea?

Sorry for my not so perfect english ;)

Thank u.

Maybe you can take a look at the db2 substring function : http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z9.doc.sqlref%2Fsrc%2Ftpc%2Fdb2z_bif_substring.htm

You can try select substring(MAX(CCID), 2, x) as test123 from ..

where x is the type you want: CODEUNITS16 , CODEUNITS32 , ..

This should return the value without the W0

edit

to add leading zero's to the new integer, do the following:

String old = "W01234";
String sub = old.substring(1); // "01234"
Integer id = Integer.parse(sub); //1234
Integer newI = id + 1; // 1235
String newS = "W" + ("00000" + newI).substring(newI.toString().length()); // "W01235"

Use PreparedStatement rather that Statement, eaiser and efficient way to work the SQL queries.

String sql = "select MAX(CCID) as test123 from <tabelleName> where CCID like '?%'");
PreparedStatement preparedStatement = dbConnection.prepareStatement(sql);
preparedStatement.setString(1, "...");
ResultSet rs = preparedStatement.executeQuery(sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM