Página 1 de 1

timestamp without time zone ~~ unknown

NotaPublicado: 2017-02-21 11:57 @540
por seafree
Hola, me encuentro ejecutando la sentencia:
Sintáxis: [ Descargar ] [ Ocultar ]
Using sql Syntax Highlighting
  1. SELECT sum((sub*tir)) AS total FROM compras WHERE fecha_sale LIKE '2013-01-%';
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

y me manda el siguiente error:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
ERROR:  operator does not exist: timestamp without time zone ~~ unknown
LINE 1: ...r)) as total from compras te where fecha_sale like '2013...

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Les agradeceré su apoyo ya que no me queda claro qué es lo que está sucediendo.

La versión de PostgreSQL es 9.2.18.

Gracias.

Re: timestamp without time zone ~~ unknown

NotaPublicado: 2017-02-24 04:06 @212
por explorer
No se puede comparar con un LIKE un campo de una fecha, como si fuera una cadena de texto.

Lo que puedes hacer es, primero, convertir la fecha en una cadena de texto:
Sintáxis: [ Descargar ] [ Ocultar ]
Using sql Syntax Highlighting
  1. SELECT sum((sub*tir)) AS total FROM compras WHERE fecha_sale::text LIKE '2013-01-%';
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Con versiones 8.3 y anteriores, PostgreSQL hacía la conversión automática a texto, pero luego lo quitaron.

También puedes hacer truncamientos:
Sintáxis: [ Descargar ] [ Ocultar ]
Using sql Syntax Highlighting
  1. SELECT sum((sub*tir)) AS total FROM compras WHERE date_trunc('month', fecha_sale) = '2013-01-01';
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Lo que hace es truncar la fecha a múltiplo de mes, por lo que luego podemos ver si corresponde al primer día de ese mes. La ventaja de esta última solución es que es indexable (puedes crear un índice en una columna con esta función).

Otra opción, usando extracciones:
Sintáxis: [ Descargar ] [ Ocultar ]
Using sql Syntax Highlighting
  1. SELECT sum((sub*tir)) AS total FROM compras WHERE EXTRACT(YEAR FROM fecha_sale) = 2013 AND EXTRACT(MONTH FROM fecha_sale) = 1;
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Otra opción, usando periodos:
Sintáxis: [ Descargar ] [ Ocultar ]
Using sql Syntax Highlighting
  1. SELECT sum((sub*tir)) AS total FROM compras WHERE fecha_sale BETWEEN '2013-01-01' AND '2013-02-01';
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Re: timestamp without time zone ~~ unknown

NotaPublicado: 2017-02-28 12:20 @556
por seafree
Gracias, explorer.