Select with date returning empty

Asked

Viewed 188 times

1

Good afternoon, What could be wrong in my query that is returning empty because of the date condition. Follows query:

   SELECT  
  imp_loja       AS LOJA,
  imp_item       AS ITEM,
  imp_desc       AS DESCRICAO,
  imp_dias_giro  AS DIAS_DE_GIRO,
  imp_nec        AS NECESSIDADE,
  imp_pedido     AS PEDIDO,
  imp_bancao     AS BANCAO,
  imp_romaneio   AS ROMANEIO, 
  imp_transito   AS TRANSITO,
  imp_gondola    AS GONDOLA,
  imp_fator      AS FATOR,
  imp_reposicao  AS REPOSICAO,
  imp_estoque    AS ESTOQUE,
  imp_estoque_dc AS EST_DC,
  (imp_nec - imp_fator) AS NEC_FAT,
  (imp_estoque / imp_fator) AS EXT_X_FATOR,
  (imp_estoque + imp_transito) AS ESTTRAN,
  (imp_nec - imp_fator) AS NECFAT,
  imp_data       AS DATA
  FROM importacao  WHERE imp_data =  NOW() 
       ORDER BY imp_desc

Follows the table:

# Host: localhost  (Version 5.5.5-10.1.13-MariaDB)
# Date: 2016-06-23 15:57:02
# Generator: MySQL-Front 5.3  (Build 5.33)

/*!40101 SET NAMES utf8 */;

#
# Structure for table "importacao"
#

CREATE TABLE `importacao` (
  `imp_loja` int(11) DEFAULT NULL,
  `imp_item` int(11) DEFAULT NULL,
  `imp_desc` varchar(254) DEFAULT NULL,
  `imp_obs` varchar(50) DEFAULT NULL,
  `imp_dias_venda` int(11) DEFAULT NULL,
  `imp_dias_giro` int(11) DEFAULT NULL,
  `imp_nec` int(11) DEFAULT NULL,
  `imp_pedido` int(11) DEFAULT NULL,
  `imp_bancao` int(11) DEFAULT NULL,
  `imp_romaneio` int(11) DEFAULT NULL,
  `imp_transito` int(11) DEFAULT NULL,
  `imp_gondola` int(11) DEFAULT NULL,
  `imp_fator` int(11) DEFAULT NULL,
  `imp_reposicao` int(11) DEFAULT NULL,
  `imp_estoque` int(11) DEFAULT NULL,
  `imp_estoque_dc` int(11) DEFAULT NULL,
  `imp_id` int(11) NOT NULL AUTO_INCREMENT,
  `imp_data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`imp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#
# Data for table "importacao"
#

Obs: I’ve tried to:

CURRENT_TIMESTAMP()
CURDATE()
NOW()
  • 1

    Probably the hour/minute of the now() should not hit the stored value, if comparing only the date, make a cast to date of both values. Only confirm if you need date or date and time.

  • rray, do not know the caste could give me an example , please ?

2 answers

4


If comparing only the date, cast in both values to make the comparison 'to the correct extent'.

The current comparison would be something like: WHERE '2016-06-26 12:00:01' = '2016-06-26 17:03:34', when it should be WHERE '2016-06-26' = '2016-06-26'

Change:

WHERE imp_data =  NOW() 

To:

WHERE cast(imp_data as date) =  cast(NOW() as date)
  • perfect, unaware of the cast. Thank you.

1

The problem must be in the fact that you compare your field imp_data with NOW, representing the current date/time.

If you want to list the records with imp_data equal to current date, use CURDATE in place of NOW. Thus:

SELECT  
  imp_loja       AS LOJA,
  imp_item       AS ITEM,
  imp_desc       AS DESCRICAO,
  imp_dias_giro  AS DIAS_DE_GIRO,
  imp_nec        AS NECESSIDADE,
  imp_pedido     AS PEDIDO,
  imp_bancao     AS BANCAO,
  imp_romaneio   AS ROMANEIO, 
  imp_transito   AS TRANSITO,
  imp_gondola    AS GONDOLA,
  imp_fator      AS FATOR,
  imp_reposicao  AS REPOSICAO,
  imp_estoque    AS ESTOQUE,
  imp_estoque_dc AS EST_DC,
  (imp_nec - imp_fator) AS NEC_FAT,
  (imp_estoque / imp_fator) AS EXT_X_FATOR,
  (imp_estoque + imp_transito) AS ESTTRAN,
  (imp_nec - imp_fator) AS NECFAT,
  imp_data       AS DATA
  FROM importacao  WHERE DATE(imp_data) =  CURDATE() 
       ORDER BY imp_desc

In addition, you must extract the date part of its field for comparison with the function DATE().

  • I put in the observation complement, also this returning empty.

  • I changed the answer, take a test...

  • Already scored, perfect also thanks.

Browser other questions tagged

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