OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Solved] Query using "today" as a value

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base
View previous topic :: View next topic  
Author Message
Mountainview
Newbie
Newbie


Joined: 11 Sep 2012
Posts: 3

PostPosted: Tue Sep 11, 2012 9:04 am    Post subject: [Solved] Query using "today" as a value Reply with quote

Hello Forum Very Happy

My first post so be gentle with me! I'm using version 3.4.1 and the HSQL database engine

I am trying to create a query to collect data for a report that uses the current date, minus a number of days to define the data set.

For example, all records entered in the last 30 days.

I can get todays records without any problem, I'm just having a problem understanding the syntax of subtracting a number of days to create a date "range"

Any help would be appreciated.

Paul


Last edited by Mountainview on Tue Sep 11, 2012 11:34 am; edited 1 time in total
Back to top
View user's profile Send private message
Sliderule
Super User
Super User


Joined: 29 May 2004
Posts: 2473
Location: 3rd Rock From The Sun

PostPosted: Tue Sep 11, 2012 9:31 am    Post subject: Reply with quote

Paul:

Welcome to the OpenOffice Base forum.

Per your description, I am assuming you are creating your Query, from the Queries icon on the left, and, under Tasks, Create Query in Design View...

I suggest you use two HSQL database functions:
  1. CURRENT_DATE
  2. DATEDIFF
According to HSQL documentation, found at:

http://www.hsqldb.org/doc/1.8/guide/ch09.html#N1251E

HSQL Version 1.8 Doumentationhttp://www.hsqldb.org/doc/1.8/guide/ch09.html#N1251E wrote:

CURRENT_DATE

returns the current date


DATEDIFF(string, datetime1, datetime2)

returns the count of units of time elapsed from datetime1 to datetime2. The string indicates the unit of time and can have the following values 'ms'='millisecond', 'ss'='second','mi'='minute','hh'='hour', 'dd'='day', 'mm'='month', 'yy' = 'year'. Both the long and short form of the strings can be used.

Now, when creating your Query, add another Field to be used, you can determine whether you want to include this calculated field in the display - using the Visible line Smile .

On the Field line, enter ( but, change "MyDateField" to the name of YOUR database field ( column ), and, make sure the name you type is exact - including CASE ( UPPER / Mixed / lower )):
Code:
DATEDIFF( 'dd', "MyDateField", CURRENT_DATE )


On the Criterion line, enter:
Code:
<= 30

If you want to assign a new name for this calculated column, you may enter it on the Alias line.

Explanation: The above will 'calculate' the the number of days difference between the first field ( "MyDateField" ) and CURRENT_DATE. Because you have entered something on the Criterion line, it will only return the records in your table that meet the criteria . . . <= 30 ( just as you described in your post above ) Smile .

If you want to see an example of the above, WITH A GRAPHIC ( sometimes, a picture is worth 1000 words Smile ) . . . click on the link below:

http://www.oooforum.org/forum/viewtopic.phtml?t=82993

I hope this helps, please be sure to let me / us know.

Sliderule

Thanks to add [Solved] in your first post Title ( edit button ) if your issue has been fixed / resolved.
Back to top
View user's profile Send private message
Mountainview
Newbie
Newbie


Joined: 11 Sep 2012
Posts: 3

PostPosted: Tue Sep 11, 2012 11:37 am    Post subject: [Solved] Many Thank Sliderule Reply with quote

Very Happy Worked first time around, the graphic option was also very good.

Have some future dates (> today) that were simple to remove by changing the:

<=30

To <=30 AND >0

Many thanks

Paul
Back to top
View user's profile Send private message
Sliderule
Super User
Super User


Joined: 29 May 2004
Posts: 2473
Location: 3rd Rock From The Sun

PostPosted: Tue Sep 11, 2012 11:46 am    Post subject: Reply with quote

Paul:

Very good.

Just as an additional FYI ( For Your Information ), if you want to make this a DYNAMIC Query . . . that is . . . where the user is Prompted to input the number of days . . . on the Criterion line you enter:

Code:
BETWEEN 0 and :Enter_Number_of_Days


I appreciate that you marked this as [Solved] . . . and . . . best of success with OpenOffice / LibreOffice Base.

Sliderule
Back to top
View user's profile Send private message
Mountainview
Newbie
Newbie


Joined: 11 Sep 2012
Posts: 3

PostPosted: Tue Sep 11, 2012 10:24 pm    Post subject: Thanks again Reply with quote

Many thanks for the additional information Very Happy that covers another part of my database that I will be working on soon.

Paul
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group