PC Review


Reply
Thread Tools Rate Thread

Date compare

 
 
=?Utf-8?B?VGlzaGE=?=
Guest
Posts: n/a
 
      7th Jul 2007
Hello,
I'm trying to select a group of records with an effective date >= Jan 1 of
the prior year forward to date. So I created a compare date of date - 1
year. Next, I want to compare this date to the EFFCYMD so I created a ccyy
date field but the selection criteria fails. The date fields look
different. How do I compare these fields? Thanks!

SELECT tbl_ZCH.GRP, tbl_ZCH.IDNBR, tbl_ZCH.COMPCD, tbl_ZCH.EFFCYMD,
tbl_ZCH.CARCD, Year(Date())-1 AS Datecmp, (Mid([effcymd],1,4)) AS Effccyy,
Right([datecmp],4) AS [Year]
FROM tbl_ZCH;

 
Reply With Quote
 
 
 
 
pietlinden@hotmail.com
Guest
Posts: n/a
 
      7th Jul 2007
On Jul 6, 6:36 pm, Tisha <T...@discussions.microsoft.com> wrote:
> Hello,
> I'm trying to select a group of records with an effective date >= Jan 1 of
> the prior year forward to date. So I created a compare date of date - 1
> year. Next, I want to compare this date to the EFFCYMD so I created a ccyy
> date field but the selection criteria fails. The date fields look
> different. How do I compare these fields? Thanks!
>
> SELECT tbl_ZCH.GRP,

tbl_ZCH.IDNBR,
tbl_ZCH.COMPCD,
tbl_ZCH.EFFCYMD,
tbl_ZCH.CARCD,
Year(Date())-1 AS Datecmp, (Mid([effcymd],1,4)) AS Effccyy,
> Right([datecmp],4) AS [Year]
> FROM tbl_ZCH;


Wait a minute... why don't you just use DateAdd() or DateSerial for
this? Looks like you're making things ten times more difficult than
they should be.

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      7th Jul 2007
On Fri, 6 Jul 2007 16:36:02 -0700, Tisha <(E-Mail Removed)>
wrote:

>Hello,
>I'm trying to select a group of records with an effective date >= Jan 1 of
>the prior year forward to date. So I created a compare date of date - 1
>year. Next, I want to compare this date to the EFFCYMD so I created a ccyy
>date field but the selection criteria fails. The date fields look
>different. How do I compare these fields? Thanks!
>
>SELECT tbl_ZCH.GRP, tbl_ZCH.IDNBR, tbl_ZCH.COMPCD, tbl_ZCH.EFFCYMD,
>tbl_ZCH.CARCD, Year(Date())-1 AS Datecmp, (Mid([effcymd],1,4)) AS Effccyy,
>Right([datecmp],4) AS [Year]
>FROM tbl_ZCH;
>


You're treating the date as a string. IT ISN'T. A Date/Time field is actually
stored as a number, a count of days and fractions of a day since a start
point. The format does NOT change what's stored, just how it's displayed.

To get all records since January 1 of the previous year use a criterion on the
date field

>= DateSerial(Year(Date()) - 1, 1, 1)


John W. Vinson [MVP]
 
Reply With Quote
 
=?Utf-8?B?VGlzaGE=?=
Guest
Posts: n/a
 
      9th Jul 2007
The effective date that I need to compare is in ccyymmdd format. The result
date from the function >=DateSerial(Year(Date())-1,1,1) is 1/1/2006 so no
matching records. How do I deal with the differing date formats?
Thanks. I've got a lot too learn.

"John W. Vinson" wrote:

> On Fri, 6 Jul 2007 16:36:02 -0700, Tisha <(E-Mail Removed)>
> wrote:
>
> >Hello,
> >I'm trying to select a group of records with an effective date >= Jan 1 of
> >the prior year forward to date. So I created a compare date of date - 1
> >year. Next, I want to compare this date to the EFFCYMD so I created a ccyy
> >date field but the selection criteria fails. The date fields look
> >different. How do I compare these fields? Thanks!
> >
> >SELECT tbl_ZCH.GRP, tbl_ZCH.IDNBR, tbl_ZCH.COMPCD, tbl_ZCH.EFFCYMD,
> >tbl_ZCH.CARCD, Year(Date())-1 AS Datecmp, (Mid([effcymd],1,4)) AS Effccyy,
> >Right([datecmp],4) AS [Year]
> >FROM tbl_ZCH;
> >

>
> You're treating the date as a string. IT ISN'T. A Date/Time field is actually
> stored as a number, a count of days and fractions of a day since a start
> point. The format does NOT change what's stored, just how it's displayed.
>
> To get all records since January 1 of the previous year use a criterion on the
> date field
>
> >= c>

> John W. Vinson [MVP]
>

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      9th Jul 2007
On Mon, 9 Jul 2007 10:22:02 -0700, Tisha <(E-Mail Removed)>
wrote:

>The effective date that I need to compare is in ccyymmdd format.


Is this a Text field? a Number field? a Date/Time field?

>The result
>date from the function >=DateSerial(Year(Date())-1,1,1) is 1/1/2006 so no
>matching records. How do I deal with the differing date formats?
>Thanks. I've got a lot too learn.


If you store the data in a a Date/Time type field, the format is *completely
irrelevant* to searching - it stores it as a Double Float count of days and
fractions of a day since midnight, December 30, 1899, and searches it exactly
the same way no matter how it's formatted. If you like that format, simply
store the date in a date/time field and use a Format of

yyyymmdd

to see 20070709 for today's date.

If you have other reasons to store the value in a text or number field, you
can use a criterion

>= Format((Year(Date())-1,1,1), "yyyymmdd")


to explicitly cast january 1 of last year into a text string, and search on
that text string.

John W. Vinson [MVP]
 
Reply With Quote
 
=?Utf-8?B?VGlzaGE=?=
Guest
Posts: n/a
 
      9th Jul 2007
I'm getting a syntax error (comma) in query expression
>=Format((Year(Date())-1,1,1),"yyyymmdd")


Btw, the date comes from an IBM AS/400 database.

You've been most helpful.


"John W. Vinson" wrote:

> On Mon, 9 Jul 2007 10:22:02 -0700, Tisha <(E-Mail Removed)>
> wrote:
>
> >The effective date that I need to compare is in ccyymmdd format.

>
> Is this a Text field? a Number field? a Date/Time field?
>
> >The result
> >date from the function >=DateSerial(Year(Date())-1,1,1) is 1/1/2006 so no
> >matching records. How do I deal with the differing date formats?
> >Thanks. I've got a lot too learn.

>
> If you store the data in a a Date/Time type field, the format is *completely
> irrelevant* to searching - it stores it as a Double Float count of days and
> fractions of a day since midnight, December 30, 1899, and searches it exactly
> the same way no matter how it's formatted. If you like that format, simply
> store the date in a date/time field and use a Format of
>
> yyyymmdd
>
> to see 20070709 for today's date.
>
> If you have other reasons to store the value in a text or number field, you
> can use a criterion
>
> >= Format((Year(Date())-1,1,1), "yyyymmdd")

>
> to explicitly cast january 1 of last year into a text string, and search on
> that text string.
>
> John W. Vinson [MVP]
>

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      10th Jul 2007
On Mon, 9 Jul 2007 12:02:02 -0700, Tisha <(E-Mail Removed)>
wrote:

>I'm getting a syntax error (comma) in query expression
>>=Format((Year(Date())-1,1,1),"yyyymmdd")


sorry... my typo! Actually a missing function name:
>= Format(DateSerial(Year(Date()) - 1, 1, 1), "yyyymmdd")


John W. Vinson [MVP]
 
Reply With Quote
 
=?Utf-8?B?VGlzaGE=?=
Guest
Posts: n/a
 
      10th Jul 2007
Perfect. This solved my problem - Thanks!

"John W. Vinson" wrote:

> On Mon, 9 Jul 2007 12:02:02 -0700, Tisha <(E-Mail Removed)>
> wrote:
>
> >I'm getting a syntax error (comma) in query expression
> >>=Format((Year(Date())-1,1,1),"yyyymmdd")

>
> sorry... my typo! Actually a missing function name:
> >= Format(DateSerial(Year(Date()) - 1, 1, 1), "yyyymmdd")

>
> John W. Vinson [MVP]
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare date range with date in columns and input data from another cell Tammy Robinson Microsoft Excel Discussion 2 18th Aug 2011 05:34 PM
compare date field in 1 table to start & end date fields in 2nd ta =?Utf-8?B?RW1pbHlUcnlpbmdUb0xlYXJu?= Microsoft Access Queries 2 19th Sep 2007 08:08 PM
How do I compare just the date portion of date time fields in SQL? =?Utf-8?B?QnJhbmRlbg==?= Microsoft Access Queries 3 29th Nov 2005 11:22 PM
Compare a field date with computer current date Koulla Microsoft Access Form Coding 1 7th Mar 2005 08:07 AM
Compare Date in Cell VS a Variable Date to Hide Row =?Utf-8?B?SmltSQ==?= Microsoft Excel Programming 3 10th Oct 2004 04:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 AM.