473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

date queries with portions of the date unknown

I'm trying to do some date queries on a table, and I want to know if there is some builtin variable within the date datatype which can represent that a day, month, or year could be any value.

For instance, if the variable were "*", I could write:

Expand|Select|Wrap|Line Numbers
  1. table.datefield >= '*-10-*'
and any date in table.datefield which had the month 10, 11 or 12 would pass the predicate.

I know there are functions which can take out just the day, month, or year, but I'm dealing with an application where any part of the date may be left variable, or none of the parts of the date could be variable, so parsing in this way doesn't exactly work without writing a lot of code to build the sql for each specific case (nothing variable, only the day variable, only the month, day and month, ect. I don't want to code all of that if I don't have to)

Is there a symbol like this which I can insert into a date query which has this functionality?

Thanks.
Feb 6 '07 #1
5 2562
michaelb
534 Expert 512MB
I am not sure I understand what you have to deal with... perhaps few examples of the actual values you have on this column would help.

If you can afford the real long-term solution you should consider changing the application in a way that all dates are stored in the database in the same format. The existing values should be converted to this format as well.
If you go this route avoid storing dates in a varchar type, use one of the native Postgres date/time datatypes.
Feb 7 '07 #2
sorry I wasn't clear, I do have this date field stored as a date datatype.

The problem isn't with how the dates are stored, the problem is with the queries predicates I use to select out the tuples.

Say I wanted any dates that were from october to december, meaning that a date could be on any day, or any year, but as long as it was in the months of october, november, or december it passes the predicate. If there were a variable like I described, you could say:

table.datefield >= '*-10-*' AND table.datefield <= '*-12-*'

In another example, if I wanted dates from october 2006 to febuary 2006, I could just say:

table.datefield >= '2005-10-*' AND table.datefield <= '2006-2-*'

The first example can be solved by just parsing the year, month, and day out of the date using the date_part() function and doing queries on those peices individually, but that strategy wouldn't work in the second case because a date like '2006-1-5' wouldn't pass date_part('month', table.datefield) >= 10, but I want that date to pass my predicates.

I'm still not sure if I phrased this correctly, but hopefully these examples explained what I am looking for better.
Feb 7 '07 #3
michaelb
534 Expert 512MB
Thanks for clarification!

As you already noted the first case can make use of the date_part function:

Expand|Select|Wrap|Line Numbers
  1. SELECT field-list FROM tabname WHERE date_part('month', column-name) BETWEEN 10 and 12;
  2.  
As far as your second case, with the date datatype you should not have any values like '2006-1-5'.
Even if this is what you use with INSERT, the actual value on the table should be '2006-01-05', so the date_part function should have no troubles parsing this value.

You can also try to modify the example you posted above:

Expand|Select|Wrap|Line Numbers
  1. table.datefield >= '2005-10-*' AND table.datefield <= '2006-2-*'
  2.  -- change it to 
  3. table.datefield >= '2005-10-01' AND table.datefield <= '2006-02-28'
  4.  
(BETWEEN may come handy here too)

One catch here is that every month begins with 01, but not all of them have the same end-date (think of 28, 29, 30, 31),
and if you give a non-existant end-date the query may error out, so this approach may require little extra thought.

There could be more tricks to employ here, but first let us know whether this info was helpful.
Feb 7 '07 #4
Your right, I could fill in the first day of the month and last day of the month when the days of a query aren't specified.

As you said though, it does require some more thought because of the different days that months end with. maybe I'll just make a map for that, but I was wondering if there was something like this "*" that I described to use as a taking any value. This would make my code creating the sql, and the sql, a lot more clean.

As this post has been up for a few days though with no response, I'm probably going to guess that this doesn't exist and go with the way you propose.

Thanks for your help.
Feb 14 '07 #5
michaelb
534 Expert 512MB
I did not mean to discourage you. Let me put it this way: I am sure, that there are other methods to accomplish what you need, but quite often I don't have time to explore every possible approach or technique.
So if by any reason what we had so far isn't helpful, there will be more to discuss and try out... meaning do come back to this thread if you don't like the last suggested approach.

By the way, I'm positive there are ways of finding the last day of the month without keeping a hardcoded matrix.
Feb 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: EnriqueM | last post by:
One question: I created an access database that has been extremely useful in my work. I dont' even know at what level of knowledge I am at. I read a lot about how to create a database, created many...
5
by: David B | last post by:
I have a number of queries, running one after the other, which do quite a complex calculation. A text box on a form provides the date for this routine. I have another routine I wish to do and it...
2
by: carl.barrett | last post by:
Hi, I'm back with the same question as I still can't get it to display my data the way I want it to. The table lists information about a perpetrator involved with an anti social behaviour...
6
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
1
by: JosAH | last post by:
Greetings, Introduction This week we start building Query objects. A query can retrieve portions of text from a Library. I don't want users to build queries by themselves, because users make...
10
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the...
2
by: johnmay1248 | last post by:
I am having a problem with queries running in this code When I use the query "SELECT * FROM sample" the code runs and the data grid binds and shows the contents of the sample table. If I change...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.