Frage

I have to get the records from a table field where Length of record/data/string is greater then 8 characters. I cannot use any string function as the query has to be used on (MySQL, MSSQL, Oracle).

I don't want to do the below EXAMPLE:

List<String> names = new ArrayList<String>();
String st = select 'name' from table;

rs = executeSQL(st);
if ( rs != null )
{
    rs.next();
    names.add(rs.getString(1));
}

for(String name : names)
{
    if(name.length() > 8)
    result.add(name);
}

Any idea other then the one coded above? A query that can get the required result instead of processing on the retrieved data and then getting the required result.

Thank you for any help / clue.

War es hilfreich?

Lösung

JDBC Drivers may implement a JDBC escapes for the functions listed in appendix D (Scalar Functions) of the JDBC specification. A driver should convert the scalar functions it supports to the appropriate function on the database side. A list of the supported functions can be queried using 'DatabaseMetaData.getStringFunctions()'

To use this in a query you would then either use CHAR_LENGTH(string) or LENGTH(string) like :

SELECT * FROM table WHERE {fn CHAR_LENGTH(field)} > 8

You can replace CHAR_LENGTH with LENGTH. The driver (if it supports this function) will then convert it to the appropriate function in the underlying database.

From section 13.4.1 Scalar Functions of the JDBC 4.1 specification:

Appendix D “Scalar Functions" provides a list of the scalar functions a driver is expected to support. A driver is required to implement these functions only if the data source supports them, however.

The escape syntax for scalar functions must only be used to invoke the scalar functions defined in Appendix D “Scalar Functions". The escape syntax is not intended to be used to invoke user-defined or vendor specific scalar functions.

Andere Tipps

I think you may be better off leveraging the power of the database and implementing a factory for your SQL statements (or perhaps for objects encapsulating your SQL functionality).

That way you can configure your factory with the name/type of the database, and it'll give you the appropriate SQL statements for that database. It gives you a clean means of parameterising this info, whilst allowing you to leverage the functionality of your databases and not having to replicate the database functionality in a suboptimal fashion in your code.

e.g.

   DabaseStatementFactory fac = DatabaseStatementFactory.for(NAME_OF_DATABASE);
   String statement = fac.getLongNames();
   // then use this statement. It'll be configured for each db type

It's probably wise to encapsulate further and use something like:

   DabaseStatementFactory fac = DatabaseStatementFactory.for(NAME_OF_DATABASE);
   List<String> names = fac.getLongNames();

such that you're not making assumptions re. common schema and means of queries etc.

Another solution that I found is:

Select name from table where name like '________';

SQL counts the underscore (_) characters and return a name of length equal to number of underscore characters.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top