473,405 Members | 2,300 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,405 software developers and data experts.

DateTime comparison problem when use format() or dateValue()

In our database, the publication year and month are saved in different columns, the query is to find out the pubs after 7/1/2006. My following query works but not get the desired dataset, e.g. publications during 10/1/2006 to 12/31/2006 was not listed in the result. I am frustrated, please help! Thanks very much.
Expand|Select|Wrap|Line Numbers
  1. SELECT Publications.PubYear, Publications.PubMonth, format(mid(PubMonth,1,3) & ' 1,' & PubYear, 'short date') AS Expr2,mid(PubMonth,1,3), IsDate(DateValue(format(mid(PubMonth,1,3) & ' 1,' & PubYear, 'short date')))
  2.  
  3. FROM [select * from publications where PubYear is not null and pubMonth is not null and (pubyear=2006 or pubyear=2007)]. as Publications
  4. where format(mid(PubMonth,1,3) & ' 1,' & PubYear, 'short date') >= #2006-07-01#
PS.
I tried to use DataValue(), but it always gives 'Datatype mismatch..."error (there's no null value, all values seem are date type for me).
Feb 19 '08 #1
4 4501
Stewart Ross
2,545 Expert Mod 2GB
In our database, the publication year and month are saved in different columns, the query is to find out the pubs after 7/1/2006. ... SELECT Publications.PubYear, Publications.PubMonth, format(mid(PubMonth,1,3) & ' 1,' & PubYear, 'short date') AS Expr2,mid(PubMonth,1,3), IsDate(DateValue(format(mid(PubMonth,1,3) & ' 1,' & PubYear, 'short date')))

FROM [select * from publications where PubYear is not null and pubMonth is not null and (pubyear=2006 or pubyear=2007)]. as Publications
where format(mid(PubMonth,1,3) & ' 1,' & PubYear, 'short date') >= #2006-07-01#
Hi Anniebai. The use of Format is not helping you here. Format returns a string representation of a date, not a date as it is internally stored (as a number). You are also using a subquery, which seems a lot for a simple query.
As you already hold the year and month values as discrete columns within your publication table (assuming these are integers), you could compare these directly with the year and month you need to find:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Select * from publications where (PubYear >= 2006) AND (PubMonth>=7);
  3.  
If the PubYear and PubMonth values are text rather than Integers or Longs you can convert them to integers using the CInt function (e.g. where (CInt(PubYear) > 2006)...

If you do need to convert your publication year and month to dates, use DateSerial instead of Format:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Select * from publications where (DateSerial(PubYear, PubMonth, 1) >= #07/01/2006#; 
  3.  
Note that the standard Access format of a date value given inside the hashes is mm/dd/yyyy, not yyyy-mm-dd as in your SQL.

Hope this helps.

-Stewart
Feb 19 '08 #2
NeoPa
32,556 Expert Mod 16PB
You can either convert your columns to a number, a string or a date. As you're using manipulation anyway, I'd go for the number. You need to compare the whole item in one go for best results as there are situations where the month can actually be LESS than the required month (if the year is greater). BTW I'm assuming you're meaning ON OR after 1 July 2006.
Expand|Select|Wrap|Line Numbers
  1. WHERE ([PubYear]*100+[PubMonth])>200606
Feb 20 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Oops! Error in my first reply in the line
Expand|Select|Wrap|Line Numbers
  1.  
  2. Select * from publications where (PubYear >= 2006) AND (PubMonth>=7);
  3.  
This condition is in error as it selects only those months from July onwards. Neopa's combination of year and month is more elegant!

A corrected version of the SQL statement is
Expand|Select|Wrap|Line Numbers
  1. Select * from publications where (PubYear > 2006) Or ((PubYear = 2006) AND (PubMonth>=7));
Anyway, NeoPa's solution is simpler and more elegant...

-Stewart
Feb 20 '08 #4
NeoPa
32,556 Expert Mod 16PB
...Anyway, NeoPa's solution is simpler and more elegant...

-Stewart
Thanks Stewart. I could hardly ask for a higher compliment :)
Feb 20 '08 #5

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

Similar topics

1
by: colinhumber | last post by:
I have a datetime variable coming from my ASP.NET application that has a time portion. I give my users the option to perform an equals, greater than, less than, or between comparison. The trouble...
3
by: arijitchatterjee123 | last post by:
Hi Group, I am new with SQL Server..I am working with SQL Server 2000. I am storing the date in a nvarchar column of atable.... Now I want to show the data of Weekends..Everything is OK...But the...
4
by: Byron | last post by:
Hi, I had a script set up to create a redirect until a certain date, and I left the script in there after that date thinking it would just be dormant until I changed the date parameters. To...
5
by: rs | last post by:
I have a table with a timestamp field which contains the date and time. ie. 9/13/2004 9:10:00 AM. I would like to split this field into 2 fields, one with just the DATE portion ie 9/13/2004 and...
10
by: DontellTrevell via AccessMonster.com | last post by:
HELP!!....I need to calculate the numer of days elapsed between two field. But, the date format is YYYYMMDD. How can i accomplsh this? -- Dontell Trevell Message posted via AccessMonster.com...
2
by: rhaazy | last post by:
I need to know how I can format a string in C# to get the current date/ time, so that I can do a comparison against a date time column in MS SQL Server 2005. The date/time column in the database...
3
by: huohaodian | last post by:
Hi, How can I convert a value created from DateTime.Now() to the datetime format that SQL Server recognises? Thanks in advance.
5
by: Sreenivas | last post by:
Hi every one, I need to compare to datetime values , done in stored procedure.The input datetime parameter is in dd/mm/yyyy hh:mm:ss AM/PM format ,the datetime values to be compared are also...
1
by: remya1000 | last post by:
I’m using VB.net 2003 application program. I am trying to do a select statement whereby I'm searching between 2 datetime values that are being stored as datetime. records are stored inside...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.