Help with data field search - android

Asked

Viewed 50 times

0

Good afternoon I have a table in the database with a date field, I need to add the values of the difference field between a specific date, for example: the last 10 minutes, the last 60 minutes, the last 7 days, but I’m not able to 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 ;

1 answer

0

The answer depends on your DBMS. I’ll give an example for SQL Server for the 30-day range:

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().

Worth looking at the documentation and getting other functions like TIMEADD().

Good Luck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.