Select data from table based on current date

Hello EE.

I'm trying to run a sql statement is DB2 to use as part of a stored procedure but it doesn't work.  

select * from table1 where created_date = date(char(current timestamp)) - 1 days

I'm getting this error:
SQL0401N  The data types of the operands for the operation "="
are not compatible.  SQLSTATE=42818

I need to select data based on the prior day(which I was trying to do above), last 7 days and last full month.  All of the date fields in my tables are of datetime format and I only need to evaluate the date. Any suggestions???

Thanks!

mikmik74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sachinwadhwaCommented:
Try this:


select * from table1 where created_date = date(current timestamp - 1 day)

0
sachinwadhwaCommented:
sorry, if created_date is datetime/timestamp, try this:

select * from table1 where created_date = (current timestamp - 1 day)
0
mikmik74Author Commented:
That works, but I don't get any records back because it's looking for that exact timestamp.  I don't want it to consider the time (hours, seconds)...I only want it to consider the actual date 3/22/06.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

sachinwadhwaCommented:
there you go...

select * from table1 where date(created_date) = date(current timestamp - 1 day)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave FordSoftware Developer / Database AdministratorCommented:
If created_date has a data-type of DATE (as its name suggests), I'd use current date instead of current timestamp.

e.g.
select * from table1 where created_date = current date - 1 day

HTH,
DaveSlash
0
mikmik74Author Commented:
DaveSlash,
The created_date date type is datetime.

sachinwadhwa,
I don't know why I didn't think of that???  But, yes that worked.  So I'd use the following:

Prior Day
select * from table1 where date(created_date) = date(current timestamp - 1 day)

Last 7 days
select * from table1 where date(created_date) = date(current timestamp - 7 day)

Last Month
select * from table1 where month(created_date) = Month(current timestamp - 1 month) and Year(created_date) = Year(current timestamp - 1 month)


Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.