A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Case or IIF?



 
 
Thread Tools Display Modes
  #1  
Old July 1st, 2008, 09:10 PM posted to microsoft.public.access.queries
NBullock
external usenet poster
 
Posts: 11
Default Case or IIF?

Might there be a better way? I am trying to pull text from a field in one
table and then assign those records to that result. For example, if the
printer name contains "gen" it is a member of the "Medical" department or if
it contains "acct" then it is a member of the Accounting department - and so
on.

I do not know of any other way to make this comparison other than maybe a
case statement, but I don't know the syntax for that in SQL.

Can anyone help?


  #2  
Old July 1st, 2008, 09:44 PM posted to microsoft.public.access.queries
Bob Barrows [MVP]
external usenet poster
 
Posts: 441
Default Case or IIF?

NBullock wrote:
Might there be a better way? I am trying to pull text from a field
in one table and then assign those records to that result. For
example, if the printer name contains "gen" it is a member of the
"Medical" department or if it contains "acct" then it is a member of
the Accounting department - and so on.

I do not know of any other way to make this comparison other than
maybe a case statement, but I don't know the syntax for that in SQL.

This is an Access group - CASE does not exist in Jet. Are you creating a
query to run directly in SQL Server? Your subject mentions both Case and
IIF so it is confusing what you want. Do you want to create a JetSQL
query (Access) or a T-SQL query (SQL Server)?

I'm also not clear what you mean by "assign those records to that
result": Are you saying you want it to return a string with the word
"Medical" or "Accounting" depending on what the printer field contains?

I would create a table called DepartmentCodes with two fields:
DepartmentName
PrinterCode
Start with the data you provided:
Medical gen
Accounting acct

Then, assuming the name of the table is Printers:
Select PrinterName,
(select Max(DepartmentName) From DepartmentCodes
where PrinterName like '%' & PrinterCode & '%') As Deparment
From Printers

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


  #3  
Old July 1st, 2008, 09:51 PM posted to microsoft.public.access.queries
Jerry Whittle
external usenet poster
 
Posts: 4,732
Default Case or IIF?

IIf works ok if nested only one deep, maybe two.

Case you can't do in SQL. You need to create it in a module and call the
function in a query.

There's a third choice: Create a table something like:

Printer Dept
gen Medical
acct Accounting

Then use that table to get the Department. It still can be done if 'gen' is
part of of a string if that's the problem.
--
Jerry Whittle, Microsoft Access MVP
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.


"NBullock" wrote:

Might there be a better way? I am trying to pull text from a field in one
table and then assign those records to that result. For example, if the
printer name contains "gen" it is a member of the "Medical" department or if
it contains "acct" then it is a member of the Accounting department - and so
on.

I do not know of any other way to make this comparison other than maybe a
case statement, but I don't know the syntax for that in SQL.

Can anyone help?



  #4  
Old July 2nd, 2008, 01:25 PM posted to microsoft.public.access.queries
Michel Walsh
external usenet poster
 
Posts: 2,404
Default Case or IIF?

SELECT SWITCH ( printer LIKE "*gen*, (SELECT somefield FROM medical WHERE
.... ),
printer LIKE "*acct*", (SELECT somefield
FROM accounting WHERE ... ),
.., ...,
true, "unknown")
FROM somewhere


that can be very slow, though. That is using JET.


Vanderghast, Access MVP



"NBullock" wrote in message
...
Might there be a better way? I am trying to pull text from a field in
one table and then assign those records to that result. For example, if
the printer name contains "gen" it is a member of the "Medical" department
or if it contains "acct" then it is a member of the Accounting
department - and so on.

I do not know of any other way to make this comparison other than maybe a
case statement, but I don't know the syntax for that in SQL.

Can anyone help?




  #5  
Old July 2nd, 2008, 02:10 PM posted to microsoft.public.access.queries
Michel Walsh
external usenet poster
 
Posts: 2,404
Default Case or IIF?

And note that each select statement in the SWITCH has to return just ONE and
only ONE row.


Vanderghast, Access MVP


  #6  
Old July 2nd, 2008, 04:03 PM posted to microsoft.public.access.queries
NBullock
external usenet poster
 
Posts: 11
Default Case or IIF?

Thanks - I will create the table and try as you suggested.

This is JetSQL.

What I mean by Assign those records to the result is, I that printers will
be grouped within the departments. That part is easy.


"Bob Barrows [MVP]" wrote in message
...
NBullock wrote:
Might there be a better way? I am trying to pull text from a field
in one table and then assign those records to that result. For
example, if the printer name contains "gen" it is a member of the
"Medical" department or if it contains "acct" then it is a member of
the Accounting department - and so on.

I do not know of any other way to make this comparison other than
maybe a case statement, but I don't know the syntax for that in SQL.

This is an Access group - CASE does not exist in Jet. Are you creating a
query to run directly in SQL Server? Your subject mentions both Case and
IIF so it is confusing what you want. Do you want to create a JetSQL
query (Access) or a T-SQL query (SQL Server)?

I'm also not clear what you mean by "assign those records to that
result": Are you saying you want it to return a string with the word
"Medical" or "Accounting" depending on what the printer field contains?

I would create a table called DepartmentCodes with two fields:
DepartmentName
PrinterCode
Start with the data you provided:
Medical gen
Accounting acct

Then, assuming the name of the table is Printers:
Select PrinterName,
(select Max(DepartmentName) From DepartmentCodes
where PrinterName like '%' & PrinterCode & '%') As Deparment
From Printers

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.




  #7  
Old July 2nd, 2008, 05:33 PM posted to microsoft.public.access.queries
NBullock
external usenet poster
 
Posts: 11
Default Case or IIF?

Yes IIF can only be nested so far, I ran into that. Plus, it seems to be an
ugly way to go. I am creating a table trying to join to to it.



"Jerry Whittle" wrote in message
...
IIf works ok if nested only one deep, maybe two.

Case you can't do in SQL. You need to create it in a module and call the
function in a query.

There's a third choice: Create a table something like:

Printer Dept
gen Medical
acct Accounting

Then use that table to get the Department. It still can be done if 'gen'
is
part of of a string if that's the problem.
--
Jerry Whittle, Microsoft Access MVP
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.


"NBullock" wrote:

Might there be a better way? I am trying to pull text from a field in
one
table and then assign those records to that result. For example, if the
printer name contains "gen" it is a member of the "Medical" department or
if
it contains "acct" then it is a member of the Accounting department - and
so
on.

I do not know of any other way to make this comparison other than maybe a
case statement, but I don't know the syntax for that in SQL.

Can anyone help?





  #8  
Old July 2nd, 2008, 06:01 PM posted to microsoft.public.access.queries
NBullock
external usenet poster
 
Posts: 11
Default Case or IIF?

I have tried this, but it does not return a vaule for Department

SELECT PrinterName, (select Max(DepartmentName) From DepartmentCodes
where PrinterName like '%' & PrinterCode & '%') AS Department
FROM Smdprtlst;





"NBullock" wrote in message
...
Might there be a better way? I am trying to pull text from a field in
one table and then assign those records to that result. For example, if
the printer name contains "gen" it is a member of the "Medical" department
or if it contains "acct" then it is a member of the Accounting
department - and so on.

I do not know of any other way to make this comparison other than maybe a
case statement, but I don't know the syntax for that in SQL.

Can anyone help?




  #9  
Old July 2nd, 2008, 06:33 PM posted to microsoft.public.access.queries
Bob Barrows [MVP]
external usenet poster
 
Posts: 441
Default Case or IIF?

I will try to reproduce this. Give me some time.

NBullock wrote:
I have tried this, but it does not return a vaule for Department

SELECT PrinterName, (select Max(DepartmentName) From DepartmentCodes
where PrinterName like '%' & PrinterCode & '%') AS Department
FROM Smdprtlst;





"NBullock" wrote in message
...
Might there be a better way? I am trying to pull text from a field
in one table and then assign those records to that result. For
example, if the printer name contains "gen" it is a member of the
"Medical" department or if it contains "acct" then it is a member of
the Accounting
department - and so on.

I do not know of any other way to make this comparison other than
maybe a case statement, but I don't know the syntax for that in SQL.

Can anyone help?


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


  #10  
Old July 2nd, 2008, 06:39 PM posted to microsoft.public.access.queries
Bob Barrows [MVP]
external usenet poster
 
Posts: 441
Default Case or IIF?

Change it to this:

SELECT PrinterName, (select Max(DepartmentName) From DepartmentCodes
where Instr(PrinterName,PrinterCode)0) AS Department
FROM Smdprtlst

NBullock wrote:
I have tried this, but it does not return a vaule for Department

SELECT PrinterName, (select Max(DepartmentName) From DepartmentCodes
where PrinterName like '%' & PrinterCode & '%') AS Department
FROM Smdprtlst;





"NBullock" wrote in message
...
Might there be a better way? I am trying to pull text from a field
in one table and then assign those records to that result. For
example, if the printer name contains "gen" it is a member of the
"Medical" department or if it contains "acct" then it is a member of
the Accounting
department - and so on.

I do not know of any other way to make this comparison other than
maybe a case statement, but I don't know the syntax for that in SQL.

Can anyone help?


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:49 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.