1

I have a variable which is in this format 2011-05-13. I want to make a query that adds one day to this variable and searches for days like this in the database.

I made this query but it doesnt work.

select phone from employee where date like (select date '2011-05-13' + 1) %

Can anyone help?

1
  • What type is the "date" field? What does it mean that the query "doesn't work?" Does it fail with a syntax error, because you meant to quote and concatenate that bare '%'? Does it fail with operator does not exist error because there's no overloaded LIKE operator that accepts a timestamp field and a string, and your query above isn't precisely what you're executing? Commented Jun 2, 2011 at 3:20

2 Answers 2

5

You need an INTERVAL:

SELECT phone FROM employee WHERE datefield = (date '2011-05-13' + INTERVAL '1 DAY') ;

Edit: Don't use LIKE when you're working with dates, there is no LIKE for a date.

Sign up to request clarification or add additional context in comments.

7 Comments

My date row is like this 2011-05-13 22:13:09.969 so i need % symbol to match 2011-05-13. I dont know how to do this. Adding 1 day to 2011-05-13 works for me with this query select date '2011-05-13' + 1.
@tipotas: use between "where date between '2011-05-13' and '2011-05-14'" will get all the values on 5-13 as it will treat them as midnight to midnight.
Just cast your TIMESTAMP to a DATE: CAST(field AS DATE) = '2011-05-13'
Seems like (according to OP) the column in question is of type text. I kinda doubt that but who knows. @tipotas: what type is the "date" column mentioned in your example query?
@Milen A. Radev: That would be a bug, he should fix that.
|
0

Try the following:

SELECT phone FROM employee WHERE to_char(date, 'yyyy-mm-dd')::timestamp = ('2011-05-13'::timestamp + '1 day'::interval)

3 Comments

YOu could also use date_trunc('day', date) instead of to_char().
Just CAST it to a DATE and you're fine. You don't need a string function to_char() to extract just the date part.
use of to_char() in this manner probably makes it impossible to use an index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.