We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Select data from table based on current date

mikmik74
mikmik74 asked
on
Medium Priority
649 Views
Last Modified: 2008-02-01
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!

Comment
Watch Question

Try this:


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

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

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

Author

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.
there you go...

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Dave FordSoftware Developer / Database Administrator
CERTIFIED EXPERT

Commented:
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

Author

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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.