3

I need to filter based on a timestamp and would like to get everything within a certain day. The timestamps are like this: 02/06/2014 7:45:59 AM or translated 2014-02-06 07:45:59

select * 
from P_FAR_SBXD.T_CLAIM_SERVICE_TYP_DIM 
where service_type_id = 134469888 and valid_from_tsp not like '2014-02-06 %'

When I run this query, I am returned the error: Partial String matching requires character operands

Upon searching this error, I was given The user used the partial string matching operator (LIKE) with an argument that was not a character string.

So what can I use to match a date? edit: or in this case, not a date?

staples
  • 424
  • 2
  • 8
  • 24

4 Answers4

3

The following is a better way to express the date condition:

select * 
from P_FAR_SBXD.T_CLAIM_SERVICE_TYP_DIM 
where service_type_id = 134469888 and
      valid_from_tsp >= DATE '2014-02-06' and
      valid_from_tsp < DATE '2014-02-07';

or:

select * 
from P_FAR_SBXD.T_CLAIM_SERVICE_TYP_DIM 
where service_type_id = 134469888 and
      valid_from_tsp >= DATE '2014-02-06' and
      valid_from_tsp < DATE '2014-02-06' + interval '1' day;

The difference is important. In general, when you have a function on a column, the database does not use indexes. So these forms will use an index on the column. The best index for this query is T_CLAIM_SERVICE_TYP_DIM(service_type_id, valid_from_tsp).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

You should be able to just cast to a date and compare to your desired date, something like;

SELECT * 
FROM P_FAR_SBXD.T_CLAIM_SERVICE_TYP_DIM 
WHERE service_type_id = 134469888 
 AND CAST(valid_from_tsp AS DATE) = '2014-02-06'

EDIT: If you have a large table, this query will not use indexes well. If that is important, just do a range check between midnight and next midnight instead;

SELECT * 
FROM P_FAR_SBXD.T_CLAIM_SERVICE_TYP_DIM 
WHERE service_type_id = 134469888 
 AND valid_from_tsp >= '2014-02-06' AND valid_from_tsp < '2014-02-07'
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

use

select * from P_FAR_SBXD.T_CLAIM_SERVICE_TYP_DIM 
where service_type_id = 134469888 and 
not DATEADD(dd, 0, DATEDIFF(dd, 0, valid_from_tsp)) = DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))
0

LIKE operator is for text.

Just a guess (you did not provide table structure), but probably valid_from_tsp is not a varchar column, but a timestamp or date column.

So , instead of

valid_from_tsp not like '2014-02-06 %'

you should use this:

cast(valid_from_tsp as varchar(20)) not like '2014-02-06 %'`
filiprem
  • 6,721
  • 1
  • 29
  • 42