Help with field search data - android

0

Good afternoon I have a table in the database with a date field, I need to put the values of the field difference between a specific date, for example: the last 10 minutes, the last 60 minutes, the last 7 days, but I can not how can you help me, follow the search code

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
String currentDateandTime = sdf.format(new Date());


public String nomeTabela() {
    String Pesquisa = "";
    ConexaoDao conexao = new ConexaoDao();
    ObjetoConexao objConexao = new ObjetoConexao();
    objConexao.db_connect_string = "flexvale.hopto.org:1433";
    objConexao.db_name = "FlexPortaCom";
    objConexao.db_userid = "sa";
    objConexao.db_password = "flextelecom";
    Connection conn = conexao.dbConnect(objConexao);



    if (conn == null) {

        Pesquisa = "Não foi possivel se conectar ao banco de dados";

    } else



    if (conn != null) try {


        Statement statement = conn.createStatement();
        String queryString = "select SUM(DIFERENÇA) as somaMes from TOTALIZADOR WHERE NID = 252 AND DATAHORA >=' "+  currentDateandTime  +  " -30 '   AND DATAHORA <= '"+ currentDateandTime +" '";
         ResultSet rs;

        rs = statement.executeQuery(queryString);

        if (rs.next()) {

            Pesquisa = rs.getString("somaMes");

        }
    } catch (SQLException e) {
        Pesquisa = e.getMessage();


    }
    return Pesquisa ;
    
asked by anonymous 29.11.2016 / 16:25

1 answer

0

The answer depends on your DBMS. I'll give an example to SQL Server for the 30 day break:

SELECT
   SUM(DIFERENÇA) as somaMes 
FROM TOTALIZADOR 
WHERE (NID = 252)
AND (DATAHORA BETWEEN DATEADD(day, -30, GETDATE()) AND GETDATE())

To return the current date in MySQL, the function is called NOW ().

It's worth looking at the documentation and getting other functions like TIMEADD ().

Good Luck.

    
22.07.2017 / 04:32