Hi All,
I have a query that outputs a "ver_number" unique field and five different date fields (date1, date2, ...date5) that can have null values.
I would like to make a table out of this query that takes the "ver_number" field records and assigns for each "ver_number" a date called "final_date" selected from the five dates in this order:
final_date=date1 if null then
final_date=date2 if null then
final_date=date3 and so on...
if all five dates are null then this ver_number is skipped and not added to the table.
18 1687
Hi All,
I have a query that outputs a "ver_number" unique field and five different date fields (date1, date2, ...date5) that can have null values.
I would like to make a table out of this query that takes the "ver_number" field records and assigns for each "ver_number" a date called "final_date" selected from the five dates in this order:
final_date=date1 if null then
final_date=date2 if null then
final_date=date3 and so on...
if all five dates are null then this ver_number is skipped and not added to the table.
Try something like this.... - SELECT [Ver_number], IIf([date1] Is Not Null,[date1],IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) AS Final_Date INTO NewTableName
-
FROM yourTableName
-
WHERE (((IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) Is Not Null));
Hi All,
I have a query that outputs a "ver_number" unique field and five different date fields (date1, date2, ...date5) that can have null values.
I would like to make a table out of this query that takes the "ver_number" field records and assigns for each "ver_number" a date called "final_date" selected from the five dates in this order:
final_date=date1 if null then
final_date=date2 if null then
final_date=date3 and so on...
if all five dates are null then this ver_number is skipped and not added to the table.
- SELECT Ver_Number, Date1, Date2, Date3, Date4, Date5, fCalculateFinalDate([Date1],[Date2],[Date3],[Date4],[Date5]) AS Final_Date INTO tblNewFinalDate
-
FROM tblFinalDate;
- Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
-
If IsNull(Date_1) Then
-
If IsNull(Date_2) Then
-
If IsNull(Date_3) Then
-
If IsNull(Date_4) Then
-
If IsNull(Date_5) Then
-
fCalculateFinalDate = Null
-
End If
-
Else
-
fCalculateFinalDate = Date_4
-
End If
-
Else
-
fCalculateFinalDate = Date_3
-
End If
-
Else
-
fCalculateFinalDate = Date_2
-
End If
-
Else
-
fCalculateFinalDate = Date_1
-
End If
-
End Function
- SELECT Ver_Number, Date1, Date2, Date3, Date4, Date5, fCalculateFinalDate([Date1],[Date2],[Date3],[Date4],[Date5]) AS Final_Date INTO tblNewFinalDate
-
FROM tblFinalDate;
- Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
-
If IsNull(Date_1) Then
-
If IsNull(Date_2) Then
-
If IsNull(Date_3) Then
-
If IsNull(Date_4) Then
-
If IsNull(Date_5) Then
-
fCalculateFinalDate = Null
-
End If
-
Else
-
fCalculateFinalDate = Date_4
-
End If
-
Else
-
fCalculateFinalDate = Date_3
-
End If
-
Else
-
fCalculateFinalDate = Date_2
-
End If
-
Else
-
fCalculateFinalDate = Date_1
-
End If
-
End Function
Just out of curiousity ADezii, do you know if your method is more efficient than what I suggested? (I've never actually used a Make Table query until I tested my sql posted above) It just seems like so much more work to get the same result, but if it reduces bloat and is faster, then I could see the point.
Try something like this.... - SELECT [Ver_number], IIf([date1] Is Not Null,[date1],IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) AS Final_Date INTO NewTableName
-
FROM yourTableName
-
WHERE (((IIf([date2] Is Not Null,[date2],IIf([date3] Is Not Null,[date3],IIf([date4] Is Not Null,[date4],IIf([date5] Is Not Null,[date5],""))))) Is Not Null));
Thanks mlcampeau, it did work but one thing it didn't do is ignore the [ver_number] that had all 5 dates null and not add it to the new table.
Thanks ADezii, that worked too
how can I tweak your code to not include the "ver_number" that has all 5 dates null?
ADezii, your code seems to have missed Date5.
All records that have only Date5 as not null are having null Final_Date in the new table
Thanks ADezii, that worked too
how can I tweak your code to not include the "ver_number" that has all 5 dates null?
- INSERT INTO tblNewFinalDate ( Ver_Number, Date1, Date2, Date3, Date4, Date5, Final_Date )
-
SELECT Ver_Number, Date1, Date2, Date3, Date4, Date5, fCalculateFinalDate([Date1],[Date2],[Date3],[Date4],[Date5]) AS Final_Date
-
FROM tblFinalDate
-
WHERE Date1 Is Not Null OR Date2 Is Not Null OR Date3 Is Not Null OR Date4 Is Not Null OR Date5 Is Not Null;
ADezii, your code seems to have missed Date5.
All records that have only Date5 as not null are having null Final_Date in the new table
- Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
-
If IsNull(Date_1) Then
-
If IsNull(Date_2) Then
-
If IsNull(Date_3) Then
-
If IsNull(Date_4) Then
-
If IsNull(Date_5) Then
-
fCalculateFinalDate = Null
-
Else
-
fCalculateFinalDate = Date_5
-
End If
-
Else
-
fCalculateFinalDate = Date_4
-
End If
-
Else
-
fCalculateFinalDate = Date_3
-
End If
-
Else
-
fCalculateFinalDate = Date_2
-
End If
-
Else
-
fCalculateFinalDate = Date_1
-
End If
-
End Function
NeoPa 32,556
Expert Mod 16PB
Late to the party here, but wouldn't Nz() make life a lot easier for the dates?
Thanks mlcampeau, it did work but one thing it didn't do is ignore the [ver_number] that had all 5 dates null and not add it to the new table.
- SELECT [Ver_number], IIf([date1] IS NOT NULL,[date1],IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],""))))) AS Final_Date INTO NewTableName
-
FROM Table1
-
WHERE (((IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],Null))))) IS NOT NULL));
This worked in my test database. I changed the "" to Null in the final IIF statement
- Public Function fCalculateFinalDate(Date_1, Date_2, Date_3, Date_4, Date_5) As Variant
-
If IsNull(Date_1) Then
-
If IsNull(Date_2) Then
-
If IsNull(Date_3) Then
-
If IsNull(Date_4) Then
-
If IsNull(Date_5) Then
-
fCalculateFinalDate = Null
-
Else
-
fCalculateFinalDate = Date_5
-
End If
-
Else
-
fCalculateFinalDate = Date_4
-
End If
-
Else
-
fCalculateFinalDate = Date_3
-
End If
-
Else
-
fCalculateFinalDate = Date_2
-
End If
-
Else
-
fCalculateFinalDate = Date_1
-
End If
-
End Function
Thanks ADezii, that seems to work now
- SELECT [Ver_number], IIf([date1] IS NOT NULL,[date1],IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],""))))) AS Final_Date INTO NewTableName
-
FROM Table1
-
WHERE (((IIf([date2] IS NOT NULL,[date2],IIf([date3] IS NOT NULL,[date3],IIf([date4] IS NOT NULL,[date4],IIf([date5] IS NOT NULL,[date5],Null))))) IS NOT NULL));
This worked in my test database. I changed the "" to Null in the final IIF statement
Thanks mlcampeauIt, it worked now after adding the condition for date1 in the WHERE clause which was missing in your code.
I like the fact that I have 2 sets of code to work with, that way I'm able to verify results.
Thanks ADezii, that seems to work now
Not a problem, glad it's working for you. This is a classic example of more than 1 way to skin an apple. You have 2 diverse approaches which arrive at the same solution via different execution plans. If you are ever working with a large number of Records, you may want to see which approach is faster. My initial guess would be that the Function approach would be significantly faster because of the nature of IIF() being notoriously innefficient, the fact that it evaluates both conditions, and there are simply too many of them in the statement. Do not go by my say so since I am definately prejudiced against certain Functions, notably IIf().
Late to the party here, but wouldn't Nz() make life a lot easier for the dates?
Welcome to the party, NeoPa! It probably would make life easier, I just have this thing about using the Nz() Function with Dates.
Thanks mlcampeauIt, it worked now after adding the condition for date1 in the WHERE clause which was missing in your code.
I like the fact that I have 2 sets of code to work with, that way I'm able to verify results.
Whoops, didn't notice I missed date1! Glad you got it to work anyhow!
NeoPa 32,556
Expert Mod 16PB
Welcome to the party, NeoPa! It probably would make life easier, I just have this thing about using the Nz() Function with Dates.
Oh well, multiple solutions and ideas is always worthwhile anyway, as not everyone can appreciate that mine are always the best :D
BTW Only the VBA IIf() function evaluates both sides. The SQL IIf() function is optimised and doesn't suffer from this performance (and logic) penalty.
Oh well, multiple solutions and ideas is always worthwhile anyway, as not everyone can appreciate that mine are always the best :D
BTW Only the VBA IIf() function evaluates both sides. The SQL IIf() function is optimised and doesn't suffer from this performance (and logic) penalty.
Still sticking by the IIF(), are we? (LOL).
NeoPa 32,556
Expert Mod 16PB
Not at all. My solution would use Nz(). However, Michelle's solution would not have suffered by using the IIf() as it was in SQL.
I try to use what is appropriate for the occasion (none of these anti-function prejudices for me :D)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: PMB |
last post by:
Thank you in advance for any and all assistance.
I'm trying to use a make table query to pull the last transactionID, so I
can use an append query to reset the transactionID to the next...
|
by: Oreo Bomb |
last post by:
I have a secured database that contains a Read-Only group. This group
has permissions to view reports, but cannot add, edit, or delete any
DB objects. One of the reports the group needs access to...
|
by: Kathy Krizl |
last post by:
I'm probably doing something stupid, but I have a make table query.
One of the tables I reference has some check box fields in it. Their
Data Type is Yes/No, their field property format is Yes/No,...
|
by: LesM |
last post by:
This is a change of behaviour between Access 2000 SP3 and Access 2002
SP3.
I have Progress table that is linked via ODBC into Access using OpenLink
Lite for Progress 9.0b. For over a year, using...
|
by: Hanif Merali |
last post by:
Hello,
I'm having some difficulties creating a make table query. The source
table which I'm basing the make table query has the fields:
CIF
BusLine1-2001
BusLine1-2002
BusLine1-2003...
|
by: rednexgfx_k |
last post by:
All,
Problem Summary: I've running about 30 make table queries via VBA in
Access 2000, and my database goes from 14,000k to over 2,000,000k. In
addition, the longer the procedure runs, the...
|
by: Bob Alston |
last post by:
Anyone know a way to make all access to a linked table, in another
Access MDB, read only?
I really don't want all the hassle of implementing full access security.
I can't do this at the server...
|
by: danijela.simunovic |
last post by:
Hi!
Is there a way that when I run a "make table query" and an "append
query" that i won't be asked those 2 or 3 questions from access like
:"you are about to run a make table...","the existing...
|
by: ken |
last post by:
Hi,
I use this command to run a make table query without opening it...
CurrentDb.Execute "make table query name"
Access tells me that it can't execute a select query...? Its a make
table query...
|
by: Robertf987 |
last post by:
Hi, I'm a bit stuck with an access database. I'm using access 2000 if that's any help.
Right, it's 3:40am right now and I'm rather tired, but I *hope* this makes sense.
I have a table which...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |