Java String Between between(String value, String low, String high)

Here you can find the source of between(String value, String low, String high)

Description

Creates a BETWEEN SQL fragment for Integer value.

License

Open Source License

Parameter

Parameter Description
value a value or a column name
low a value or a column name
high a value or a column name

Declaration

public static String between(String value, String low, String high) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  ww  w. j  a va2s  . c om*/
     * Creates a BETWEEN SQL fragment for Integer value.
     * The function takes String to allow cases like (8 BETWEEN cola AND colb) and
     * (cola BETWEEN 1 AND 8)
     * 
     * @param value
     *            a value or a column name
     * @param low
     *            a value or a column name
     * @param high
     *            a value or a column name
     * @return
     */
    public static String between(String value, String low, String high) {
        return value + " BETWEEN " + low + " AND " + high;
    }

    /**
     * Creates a BETWEEN SQL fragment like 18 BETWEEN collow AND colhigh
     * 
     * @param value
     * @param colLow
     * @param colHigh
     * @return
     */
    public static String between(Object value, String colLow, String colHigh) {
        if (Number.class.isAssignableFrom(value.getClass())) {
            return value + " BETWEEN " + colLow + " AND " + colHigh;
        }
        return "'" + escapeSQLString(value.toString()) + "' BETWEEN " + colLow + " AND " + colHigh;
    }

    /**
     * Very simple escape function for SQL string.
     * All single quotes will be doubled
     * 
     * @param command
     * @return
     */
    public static String escapeSQLString(String command) {
        return command.replaceAll("'", "''");
    }
}

Related

  1. between(final String string, final Integer min, final Integer max)
  2. between(String str, String left, String right)
  3. between(String str, String minstr, String maxstr)
  4. between(String text, String after, String before)
  5. between(String text, String begin, String end)
  6. betweenNonGreedy(String string, String start, String end)