By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,657 Members | 1,682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,657 IT Pros & Developers. It's quick & easy.

Access Database Query Help

Catalyst159
100+
P: 111
I am trying to correct the following query:

Expand|Select|Wrap|Line Numbers
  1. UPDATE marriages SET marriages.[Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy')
  2.  
  3. WHERE marriages.[Date of Marriage] Is Not Null OR marriages.[Date of Marriage] <> 'VOID';


***What I am trying to do is change this so it will not try and update any records with a [Date of Marriage] value of 'VOID' and also will not try and update any records that have a [Date of Marriage] value of only the year for example '1917' or '1980'.
***

How could I go about changing this query to reflect what I want? I appreciate any help. Thanks.
Nov 16 '11 #1
Share this Question
Share on Google+
20 Replies


TheSmileyCoder
Expert Mod 100+
P: 2,321
First a quick question.
Is [Date of Marriage] a text field or a date field?
The example provided seems to suggest a text field, since you can have 'void' in your field.
Nov 16 '11 #2

ADezii
Expert 5K+
P: 8,685
How about Updating only if a valid Date exists, as in:
Expand|Select|Wrap|Line Numbers
  1. WHERE IsDate(marriages.[Date of Marriage])
Nov 16 '11 #3

Catalyst159
100+
P: 111
You are correct. It is a text field.
Nov 16 '11 #4

Catalyst159
100+
P: 111
But if a valid date exists then what would it actually be updating?
Nov 16 '11 #5

Catalyst159
100+
P: 111
This would update only format then ?
Nov 16 '11 #6

TheSmileyCoder
Expert Mod 100+
P: 2,321
I believe so.
But always take backup before you run a big update query if your not sure on the results :)
Nov 16 '11 #7

Catalyst159
100+
P: 111
The problem I am having now is when I run the following query:

Expand|Select|Wrap|Line Numbers
  1. UPDATE marriages_cpy SET marriages_cpy.[Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy')
  2. WHERE marriages_cpy.[Date of Marriage] <> 'VOID' OR IsDate([Date of Marriage]);
  3.  

Any records that have a [Date of Marriage] value of only a year like "1917" or "1980" or any that are not valid dates the values are deleted after running the query. However it does keep the VOID value. I do not want to delete any of the values that are not valid dates. I only want to skip those and not update them.
Nov 16 '11 #8

TheSmileyCoder
Expert Mod 100+
P: 2,321
You just need to loose the <>Void part, since 1980 for example is different from void, and as such gets updated.
Since Void is not a valid date, the IsDate() criteria is enough for your needs.
Expand|Select|Wrap|Line Numbers
  1. UPDATE marriages_cpy SET marriages_cpy.[Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy') 
  2. WHERE IsDate([Date of Marriage]);
Nov 16 '11 #9

Catalyst159
100+
P: 111
So what would happen to a record that has a value of 1980 then? It would remain 1980 right ?
Nov 16 '11 #10

TheSmileyCoder
Expert Mod 100+
P: 2,321
Yes 1980 would remain 1980. The query will only work on valid dates.
Nov 16 '11 #11

Catalyst159
100+
P: 111
Yes you are right. I just tested it. Looks good. Any ideas as to why it was deleting the value before?
Nov 16 '11 #12

TheSmileyCoder
Expert Mod 100+
P: 2,321
No I dont really see why it would be deleted. What I tried is this:
Alt-F11 to open VBE, then Ctrl-G to open/goto immediate pane.
Then I typed:
Expand|Select|Wrap|Line Numbers
  1. ? format("1980","m/d/yyyy")
which returned [6-2-1905] (Which is 1980 days after what VB counts as day 0 (12-30-1899)

So I could understand if 1980 got replaced by [6-2-1905], but it being deleted makes no real sense to me.
Nov 16 '11 #13

Catalyst159
100+
P: 111
I don't understand why it returns the 6-2-1905. Could you explain this a little more for me. I would appreciate it. Thanks.
Nov 16 '11 #14

TheSmileyCoder
Expert Mod 100+
P: 2,321
When a date gets stored, it gets stored as a double, and each time it gets retrieved, it gets formated back to a date.
For example Today is:Cdbl(Date())=40863
where as now() gives: cdbl(Now())=40863,9424768519
the 40863 represents the date, where as .9424768519 represents the time of the day, I presume, as a fraction of a full day, as you can see the day is almost over here :P.

From alot of views it makes sense to store the date as a number, because for instance its alot easier to do comparisions on whether or not a date is earlier (smaller) or later (greater) then another date. Im sure there are sites out there that explain it better then me though, so if your still unclear, go look it up. ;P


Edit: PS. Its just convention that says that 0 is 30/12/1899.
Nov 16 '11 #15

Catalyst159
100+
P: 111
I have a better idea of what is happening now. Thanks for the explanation and the help.

Catalyst
Nov 16 '11 #16

NeoPa
Expert Mod 15k+
P: 31,712
Smiley:
PS. Its just convention that says that 0 is 30/12/1899.
It's not really a convention. It's just a date that MS decided to use.

NB. Because dates are stored as Doubles, they can also handle negative values so much older historical dates can also be represented by simply using negative numbers
Expand|Select|Wrap|Line Numbers
  1. ?CDate(-1),CDate(-657434)
  2. 29/12/1899    01/01/100
Your update query could be simply (fundamentally similar to other suggestions but slightly shorter) :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [Marriages]
  2. SET    [Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy')
  3. WHERE  IsDate([Date of Marriage])
Assuming your actual requirement is to update all date strings to the same format as long as they can be so updated, then ADezii's suggestion (from post #3) is a perfect solution for you.
Nov 17 '11 #17

Catalyst159
100+
P: 111
Thanks for the input Neo. So far the update query is working for my situation. It is unusual how access handles the dates. I am definitely getting a better understanding of it though. Thanks again.
Nov 17 '11 #18

Rabbit
Expert Mod 10K+
P: 12,421
It's how all computers handle dates. An arbitrary date is chosen as the 0 date and every other date is relative to that date.
Nov 17 '11 #19

P: 1
Expand|Select|Wrap|Line Numbers
  1. UPDATE marriages SET marriages.[Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy')
  2.  
  3. WHERE marriages.[Date of Marriage] Is Not Null OR marriages.[Date of Marriage]Or marriages.[CDate] <> 'VOID';
Nov 17 '11 #20

NeoPa
Expert Mod 15k+
P: 31,712
@Sumon14
I'm not sure about that. It adds nothing to previous solutions and misses various points already made. Above all, it won't work.
Nov 18 '11 #21

Post your reply

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