SQL Problem | | |
First post to this community so am not sure if this is the correct place.
Here goes.
I have a MS Access db that keeps track of employees sick and annual leave
balances. In it, I have a report, built around a query, that lists
everyone's leave balances. There are columns (fields) that are generated
with nested IIF statements. Everything there works like it should and the
results are accurate.
Now, I'm wanting to put this SQL query to a VB project. I've gone through
the walkthroughs to bring SQL Server db table information to the form by an
creating OleDbDataAdapter, an OleDbConnection, binding form controls to the
Data Set, etc. I'm getting a better understanding of how that works but not
fluent yet, if you know what I mean.
My problem is that I'm can't create a new OleDbConnection with the SQL
statement copied from my MS Access application. VB doesn't seem to like my
nested IIF statements (with IS NULL or IS NOT NULL). I'm pretty sure there's
a way around this, or is there?
Thanks for the help.
--
JMorrell | | | | re: SQL Problem
"JMorrell" <JMorrell@discussions.microsoft.com> wrote in message
news:81495492-BD7E-4292-AC23-D21FDDC27730@microsoft.com...[color=blue]
> First post to this community so am not sure if this is the correct place.
> Here goes.
>
> I have a MS Access db that keeps track of employees sick and annual leave
> balances. In it, I have a report, built around a query, that lists
> everyone's leave balances. There are columns (fields) that are generated
> with nested IIF statements. Everything there works like it should and the
> results are accurate.
>
> Now, I'm wanting to put this SQL query to a VB project. I've gone through
> the walkthroughs to bring SQL Server db table information to the form by[/color]
an[color=blue]
> creating OleDbDataAdapter, an OleDbConnection, binding form controls to[/color]
the[color=blue]
> Data Set, etc. I'm getting a better understanding of how that works but[/color]
not[color=blue]
> fluent yet, if you know what I mean.
>
> My problem is that I'm can't create a new OleDbConnection with the SQL
> statement copied from my MS Access application. VB doesn't seem to like[/color]
my[color=blue]
> nested IIF statements (with IS NULL or IS NOT NULL). I'm pretty sure[/color]
there's[color=blue]
> a way around this, or is there?
>
> Thanks for the help.
> --
> JMorrell[/color]
[color=blue]
> My problem is that I'm can't create a new OleDbConnection with the SQL
> statement ...[/color]
What does your statement look like?
/Fredrik | | | | re: SQL Problem
Hi J,
AFAIK, iif is not supported inside SQL except for analysis services, not
inside t-sql. Can you show us some code so that we can try to help you.
Bernie Yaeger
"JMorrell" <JMorrell@discussions.microsoft.com> wrote in message
news:81495492-BD7E-4292-AC23-D21FDDC27730@microsoft.com...[color=blue]
> First post to this community so am not sure if this is the correct place.
> Here goes.
>
> I have a MS Access db that keeps track of employees sick and annual leave
> balances. In it, I have a report, built around a query, that lists
> everyone's leave balances. There are columns (fields) that are generated
> with nested IIF statements. Everything there works like it should and the
> results are accurate.
>
> Now, I'm wanting to put this SQL query to a VB project. I've gone through
> the walkthroughs to bring SQL Server db table information to the form by
> an
> creating OleDbDataAdapter, an OleDbConnection, binding form controls to
> the
> Data Set, etc. I'm getting a better understanding of how that works but
> not
> fluent yet, if you know what I mean.
>
> My problem is that I'm can't create a new OleDbConnection with the SQL
> statement copied from my MS Access application. VB doesn't seem to like
> my
> nested IIF statements (with IS NULL or IS NOT NULL). I'm pretty sure
> there's
> a way around this, or is there?
>
> Thanks for the help.
> --
> JMorrell[/color] | | | | re: SQL Problem
Thanks for the reply. The SQL statement in Access is based on multiple
queries. Having said that, here is my SSQL statement:
SELECT TblEmp.SSN, [lname] & ", " & [fname] AS Name, TblLeaveBal.AnnBal,
TblLeaveBal.SickBal, TblLeaveBal.CompBal, qrySumSick.SumSick,
qrySumAnn.SumAnn, qrySumComp.SumOfLeaveHours AS SumComp, IIf([compbal] Is Not
Null,(nz([compbal]-nz([sumofleavehours]))),(IIf([sumofleavehours] Is
Null,[compbal]))) AS NewCompBal, (([sickbal]-[sumsick])+[sumofsupdateval]) AS
NS, IIf([sumsick] Is
Null,([sickbal]+[sumofsupdateval]),(([sickbal]-[sumsick])+[sumofsupdateval]))
AS NewSickBal, IIf([sumann] Is
Null,([annbal]+[sumofaupdateval]),(([annbal]-[sumann])+[sumofaupdateval])) AS
NewAnnBals, IIf([compbal] Is
Null,([compbal]+[sumofleavehours]),(([compbal]-[sumofleavehours]))) AS
NewzCompBal, IIf([sumann] Is Null,([annbal]+[sumofaupdateval])) AS NewAnnBal,
qrySumAnnAccrue.SumOfAUpdateVal
FROM (((((TblEmp LEFT JOIN TblLeaveBal ON TblEmp.SSN = TblLeaveBal.SSN) LEFT
JOIN qrySumAnn ON TblEmp.SSN = qrySumAnn.SSN) LEFT JOIN qrySumSick ON
TblEmp.SSN = qrySumSick.SSN) LEFT JOIN qrySumSickAccrue ON TblEmp.SSN =
qrySumSickAccrue.EmpID) LEFT JOIN qrySumAnnAccrue ON TblEmp.SSN =
qrySumAnnAccrue.EmpID) LEFT JOIN qrySumComp ON TblEmp.SSN = qrySumComp.SSN
WHERE (((TblEmp.Term)=0))
ORDER BY [lname] & ", " & [fname];
Most appreciative of any help!
JMorrell
"Fredrik Wahlgren" wrote:
[color=blue]
>
> "JMorrell" <JMorrell@discussions.microsoft.com> wrote in message
> news:81495492-BD7E-4292-AC23-D21FDDC27730@microsoft.com...[color=green]
> > First post to this community so am not sure if this is the correct place.
> > Here goes.
> >
> > I have a MS Access db that keeps track of employees sick and annual leave
> > balances. In it, I have a report, built around a query, that lists
> > everyone's leave balances. There are columns (fields) that are generated
> > with nested IIF statements. Everything there works like it should and the
> > results are accurate.
> >
> > Now, I'm wanting to put this SQL query to a VB project. I've gone through
> > the walkthroughs to bring SQL Server db table information to the form by[/color]
> an[color=green]
> > creating OleDbDataAdapter, an OleDbConnection, binding form controls to[/color]
> the[color=green]
> > Data Set, etc. I'm getting a better understanding of how that works but[/color]
> not[color=green]
> > fluent yet, if you know what I mean.
> >
> > My problem is that I'm can't create a new OleDbConnection with the SQL
> > statement copied from my MS Access application. VB doesn't seem to like[/color]
> my[color=green]
> > nested IIF statements (with IS NULL or IS NOT NULL). I'm pretty sure[/color]
> there's[color=green]
> > a way around this, or is there?
> >
> > Thanks for the help.
> > --
> > JMorrell[/color]
>
>[color=green]
> > My problem is that I'm can't create a new OleDbConnection with the SQL
> > statement ...[/color]
>
> What does your statement look like?
>
> /Fredrik
>
>
>[/color] | | | | re: SQL Problem
"JMorrell" <JMorrell@discussions.microsoft.com> wrote in message
news:06910314-3CE0-46D2-BF04-9A1491915A09@microsoft.com...[color=blue]
> Thanks for the reply. The SQL statement in Access is based on multiple
> queries. Having said that, here is my SSQL statement:
>
> SELECT TblEmp.SSN, [lname] & ", " & [fname] AS Name, TblLeaveBal.AnnBal,
> TblLeaveBal.SickBal, TblLeaveBal.CompBal, qrySumSick.SumSick,
> qrySumAnn.SumAnn, qrySumComp.SumOfLeaveHours AS SumComp, IIf([compbal] Is[/color]
Not[color=blue]
> Null,(nz([compbal]-nz([sumofleavehours]))),(IIf([sumofleavehours] Is
> Null,[compbal]))) AS NewCompBal, (([sickbal]-[sumsick])+[sumofsupdateval])[/color]
AS[color=blue]
> NS, IIf([sumsick] Is
>[/color]
Null,([sickbal]+[sumofsupdateval]),(([sickbal]-[sumsick])+[sumofsupdateval])
)[color=blue]
> AS NewSickBal, IIf([sumann] Is
> Null,([annbal]+[sumofaupdateval]),(([annbal]-[sumann])+[sumofaupdateval]))[/color]
AS[color=blue]
> NewAnnBals, IIf([compbal] Is
> Null,([compbal]+[sumofleavehours]),(([compbal]-[sumofleavehours]))) AS
> NewzCompBal, IIf([sumann] Is Null,([annbal]+[sumofaupdateval])) AS[/color]
NewAnnBal,[color=blue]
> qrySumAnnAccrue.SumOfAUpdateVal
> FROM (((((TblEmp LEFT JOIN TblLeaveBal ON TblEmp.SSN = TblLeaveBal.SSN)[/color]
LEFT[color=blue]
> JOIN qrySumAnn ON TblEmp.SSN = qrySumAnn.SSN) LEFT JOIN qrySumSick ON
> TblEmp.SSN = qrySumSick.SSN) LEFT JOIN qrySumSickAccrue ON TblEmp.SSN =
> qrySumSickAccrue.EmpID) LEFT JOIN qrySumAnnAccrue ON TblEmp.SSN =
> qrySumAnnAccrue.EmpID) LEFT JOIN qrySumComp ON TblEmp.SSN = qrySumComp.SSN
> WHERE (((TblEmp.Term)=0))
> ORDER BY [lname] & ", " & [fname];
>
> Most appreciative of any help!
> JMorrell
>
>[/color]
You can't use IIF in SQL Server. The equivalent is called CASE. Snipped
from books online.
"
CASE
Evaluates a list of conditions and returns one of multiple possible result
expressions.
CASE has two formats:
a.. The simple CASE function compares an expression to a set of simple
expressions to determine the result.
b.. The searched CASE function evaluates a set of Boolean expressions to
determine the result.
Both formats support an optional ELSE argument.
Syntax
Simple CASE function:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END
Searched CASE function:
CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END
Arguments
input_expression
Is the expression evaluated when using the simple CASE format.
input_expression is any valid Microsoft® SQL ServerT expression.
WHEN when_expression
Is a simple expression to which input_expression is compared when using the
simple CASE format. when_expression is any valid SQL Server expression. The
data types of input_expression and each when_expression must be the same or
must be an implicit conversion.
n
Is a placeholder indicating that multiple WHEN when_expression THEN
result_expression clauses, or multiple WHEN Boolean_expression THEN
result_expression clauses can be used.
THEN result_expression
Is the expression returned when input_expression equals when_expression
evaluates to TRUE, or Boolean_expression evaluates to TRUE. result
expression is any valid SQL Server expression.
ELSE else_result_expression
Is the expression returned if no comparison operation evaluates to TRUE. If
this argument is omitted and no comparison operation evaluates to TRUE, CASE
returns NULL. else_result_expression is any valid SQL Server expression. The
data types of else_result_expression and any result_expression must be the
same or must be an implicit conversion.
WHEN Boolean_expression
Is the Boolean expression evaluated when using the searched CASE format.
Boolean_expression is any valid Boolean expression.
Result Types
Returns the highest precedence type from the set of types in
result_expressions and the optional else_result_expression. For more
information, see Data Type Precedence.
Result Values
Simple CASE function:
a.. Evaluates input_expression, and then, in the order specified,
evaluates input_expression = when_expression for each WHEN clause.
b.. Returns the result_expression of the first (input_expression =
when_expression) that evaluates to TRUE.
c.. If no input_expression = when_expression evaluates to TRUE, SQL Server
returns the else_result_expression if an ELSE clause is specified, or a NULL
value if no ELSE clause is specified.
Searched CASE function:
a.. Evaluates, in the order specified, Boolean_expression for each WHEN
clause.
b.. Returns result_expression of the first Boolean_expression that
evaluates to TRUE.
c.. If no Boolean_expression evaluates to TRUE, SQL Server returns the
else_result_expression if an ELSE clause is specified, or a NULL value if no
ELSE clause is specified.
Examples
A. Use a SELECT statement with a simple CASE function
Within a SELECT statement, a simple CASE function allows only an equality
check; no other comparisons are made. This example uses the CASE function to
alter the display of book categories to make them more understandable.
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
Here is the result set:
Category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive's Data 19.99
Business Straight Talk About Compu 19.99
avg
==========================
13.73
Category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99
avg
==========================
11.49
Category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User Friendly? 22.95
avg
==========================
21.48
Category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology Life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59
avg
==========================
13.50
Category Shortened Title Price
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi, Anyone? 14.99
Traditional Cooking Onions, Leeks, and Garlic 20.95
avg
==========================
15.96
(21 row(s) affected)
B. Use a SELECT statement with simple and searched CASE function
Within a SELECT statement, the searched CASE function allows values to be
replaced in the result set based on comparison values. This example displays
the price (a money column) as a text comment based on the price range for a
book.
USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
GO
Here is the result set:
Price Category Shortened Title
--------------------- --------------------
Not yet priced Net Etiquette
Not yet priced The Psychology of Co
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title Life Without Fear
Very Reasonable Title Emotional Security:
Coffee Table Title Is Anger the Enemy?
Coffee Table Title Cooking with Compute
Coffee Table Title Fifty Years in Bucki
Coffee Table Title Sushi, Anyone?
Coffee Table Title Prolonged Data Depri
Coffee Table Title Silicon Valley Gastr
Coffee Table Title Straight Talk About
Coffee Table Title The Busy Executive's
Expensive book! Secrets of Silicon V
Expensive book! Onions, Leeks, and G
Expensive book! Computer Phobic And
Expensive book! But Is It User Frien
(18 row(s) affected)
C. Use CASE with SUBSTRING and SELECT
This example uses CASE and THEN to produce a list of authors, the book
identification numbers, and the book types each author has written.
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
Type =
CASE
WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
END
FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
Here is the result set:
Name au_id title_id Type
------------------------- ----------- -------- -------------------
Johnson White 172-32-1176 PS3333 Psychology
Marjorie Green 213-46-8915 BU1032 Business
Marjorie Green 213-46-8915 BU2075 Business
Cheryl Carson 238-95-7766 PC1035 Popular Computing
Michael O'Leary 267-41-2394 BU1111 Business
Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
Dean Straight 274-80-9391 BU7832 Business
Abraham Bennet 409-56-7008 BU1032 Business
Ann Dull 427-17-2319 PC8888 Popular Computing
Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
Charlene Locksley 486-29-1786 PC9999 Popular Computing
Charlene Locksley 486-29-1786 PS7777 Psychology
Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
Innes del Castillo 712-45-1867 MC2222 Modern Cooking
Michel DeFrance 722-51-5454 MC3021 Modern Cooking
Stearns MacFeather 724-80-9391 BU1111 Business
Stearns MacFeather 724-80-9391 PS1372 Psychology
Livia Karsen 756-30-7391 PS1372 Psychology
Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
Sheryl Hunter 846-92-7186 PC8888 Popular Computing
Anne Ringer 899-46-2035 MC3021 Modern Cooking
Anne Ringer 899-46-2035 PS2091 Psychology
Albert Ringer 998-72-3567 PS2091 Psychology
Albert Ringer 998-72-3567 PS2106 Psychology
(25 row(s) affected)"/Fredrik | | | | re: SQL Problem
Hi Fred,
You are correct; I did not think about it, but, yes, case is a reasonable
replacement.
Bernie
"Fredrik Wahlgren" <fredrik.p.wahlgren@mailbox.swipnet.se> wrote in message
news:ujGaV1lAFHA.2316@TK2MSFTNGP15.phx.gbl...[color=blue]
>
> "JMorrell" <JMorrell@discussions.microsoft.com> wrote in message
> news:06910314-3CE0-46D2-BF04-9A1491915A09@microsoft.com...[color=green]
>> Thanks for the reply. The SQL statement in Access is based on multiple
>> queries. Having said that, here is my SSQL statement:
>>
>> SELECT TblEmp.SSN, [lname] & ", " & [fname] AS Name, TblLeaveBal.AnnBal,
>> TblLeaveBal.SickBal, TblLeaveBal.CompBal, qrySumSick.SumSick,
>> qrySumAnn.SumAnn, qrySumComp.SumOfLeaveHours AS SumComp, IIf([compbal] Is[/color]
> Not[color=green]
>> Null,(nz([compbal]-nz([sumofleavehours]))),(IIf([sumofleavehours] Is
>> Null,[compbal]))) AS NewCompBal,
>> (([sickbal]-[sumsick])+[sumofsupdateval])[/color]
> AS[color=green]
>> NS, IIf([sumsick] Is
>>[/color]
> Null,([sickbal]+[sumofsupdateval]),(([sickbal]-[sumsick])+[sumofsupdateval])
> )[color=green]
>> AS NewSickBal, IIf([sumann] Is
>> Null,([annbal]+[sumofaupdateval]),(([annbal]-[sumann])+[sumofaupdateval]))[/color]
> AS[color=green]
>> NewAnnBals, IIf([compbal] Is
>> Null,([compbal]+[sumofleavehours]),(([compbal]-[sumofleavehours]))) AS
>> NewzCompBal, IIf([sumann] Is Null,([annbal]+[sumofaupdateval])) AS[/color]
> NewAnnBal,[color=green]
>> qrySumAnnAccrue.SumOfAUpdateVal
>> FROM (((((TblEmp LEFT JOIN TblLeaveBal ON TblEmp.SSN = TblLeaveBal.SSN)[/color]
> LEFT[color=green]
>> JOIN qrySumAnn ON TblEmp.SSN = qrySumAnn.SSN) LEFT JOIN qrySumSick ON
>> TblEmp.SSN = qrySumSick.SSN) LEFT JOIN qrySumSickAccrue ON TblEmp.SSN =
>> qrySumSickAccrue.EmpID) LEFT JOIN qrySumAnnAccrue ON TblEmp.SSN =
>> qrySumAnnAccrue.EmpID) LEFT JOIN qrySumComp ON TblEmp.SSN =
>> qrySumComp.SSN
>> WHERE (((TblEmp.Term)=0))
>> ORDER BY [lname] & ", " & [fname];
>>
>> Most appreciative of any help!
>> JMorrell
>>
>>[/color]
> You can't use IIF in SQL Server. The equivalent is called CASE. Snipped
> from books online.
> "
> CASE
> Evaluates a list of conditions and returns one of multiple possible result
> expressions.
>
> CASE has two formats:
>
> a.. The simple CASE function compares an expression to a set of simple
> expressions to determine the result.
>
>
> b.. The searched CASE function evaluates a set of Boolean expressions to
> determine the result.
> Both formats support an optional ELSE argument.
>
> Syntax
> Simple CASE function:
>
> CASE input_expression
> WHEN when_expression THEN result_expression
> [ ...n ]
> [
> ELSE else_result_expression
> ]
> END
>
> Searched CASE function:
>
> CASE
> WHEN Boolean_expression THEN result_expression
> [ ...n ]
> [
> ELSE else_result_expression
> ]
> END
>
> Arguments
> input_expression
>
> Is the expression evaluated when using the simple CASE format.
> input_expression is any valid Microsoft® SQL ServerT expression.
>
> WHEN when_expression
>
> Is a simple expression to which input_expression is compared when using
> the
> simple CASE format. when_expression is any valid SQL Server expression.
> The
> data types of input_expression and each when_expression must be the same
> or
> must be an implicit conversion.
>
> n
>
> Is a placeholder indicating that multiple WHEN when_expression THEN
> result_expression clauses, or multiple WHEN Boolean_expression THEN
> result_expression clauses can be used.
>
> THEN result_expression
>
> Is the expression returned when input_expression equals when_expression
> evaluates to TRUE, or Boolean_expression evaluates to TRUE. result
> expression is any valid SQL Server expression.
>
> ELSE else_result_expression
>
> Is the expression returned if no comparison operation evaluates to TRUE.
> If
> this argument is omitted and no comparison operation evaluates to TRUE,
> CASE
> returns NULL. else_result_expression is any valid SQL Server expression.
> The
> data types of else_result_expression and any result_expression must be the
> same or must be an implicit conversion.
>
> WHEN Boolean_expression
>
> Is the Boolean expression evaluated when using the searched CASE format.
> Boolean_expression is any valid Boolean expression.
>
> Result Types
> Returns the highest precedence type from the set of types in
> result_expressions and the optional else_result_expression. For more
> information, see Data Type Precedence.
>
> Result Values
> Simple CASE function:
> a.. Evaluates input_expression, and then, in the order specified,
> evaluates input_expression = when_expression for each WHEN clause.
>
>
> b.. Returns the result_expression of the first (input_expression =
> when_expression) that evaluates to TRUE.
>
>
> c.. If no input_expression = when_expression evaluates to TRUE, SQL
> Server
> returns the else_result_expression if an ELSE clause is specified, or a
> NULL
> value if no ELSE clause is specified.
> Searched CASE function:
> a.. Evaluates, in the order specified, Boolean_expression for each WHEN
> clause.
>
>
> b.. Returns result_expression of the first Boolean_expression that
> evaluates to TRUE.
>
>
> c.. If no Boolean_expression evaluates to TRUE, SQL Server returns the
> else_result_expression if an ELSE clause is specified, or a NULL value if
> no
> ELSE clause is specified.
> Examples
> A. Use a SELECT statement with a simple CASE function
> Within a SELECT statement, a simple CASE function allows only an equality
> check; no other comparisons are made. This example uses the CASE function
> to
> alter the display of book categories to make them more understandable.
>
> USE pubs
> GO
> SELECT Category =
> CASE type
> WHEN 'popular_comp' THEN 'Popular Computing'
> WHEN 'mod_cook' THEN 'Modern Cooking'
> WHEN 'business' THEN 'Business'
> WHEN 'psychology' THEN 'Psychology'
> WHEN 'trad_cook' THEN 'Traditional Cooking'
> ELSE 'Not yet categorized'
> END,
> CAST(title AS varchar(25)) AS 'Shortened Title',
> price AS Price
> FROM titles
> WHERE price IS NOT NULL
> ORDER BY type, price
> COMPUTE AVG(price) BY type
> GO
> Here is the result set:
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Business You Can Combat Computer S 2.99
> Business Cooking with Computers: S 11.95
> Business The Busy Executive's Data 19.99
> Business Straight Talk About Compu 19.99
>
> avg
> ==========================
> 13.73
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Modern Cooking The Gourmet Microwave 2.99
> Modern Cooking Silicon Valley Gastronomi 19.99
>
> avg
> ==========================
> 11.49
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Popular Computing Secrets of Silicon Valley 20.00
> Popular Computing But Is It User Friendly? 22.95
>
> avg
> ==========================
> 21.48
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Psychology Life Without Fear 7.00
> Psychology Emotional Security: A New 7.99
> Psychology Is Anger the Enemy? 10.95
> Psychology Prolonged Data Deprivatio 19.99
> Psychology Computer Phobic AND Non-P 21.59
>
> avg
> ==========================
> 13.50
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Traditional Cooking Fifty Years in Buckingham 11.95
> Traditional Cooking Sushi, Anyone? 14.99
> Traditional Cooking Onions, Leeks, and Garlic 20.95
>
> avg
> ==========================
> 15.96
>
> (21 row(s) affected)
> B. Use a SELECT statement with simple and searched CASE function
> Within a SELECT statement, the searched CASE function allows values to be
> replaced in the result set based on comparison values. This example
> displays
> the price (a money column) as a text comment based on the price range for
> a
> book.
>
> USE pubs
> GO
> SELECT 'Price Category' =
> CASE
> WHEN price IS NULL THEN 'Not yet priced'
> WHEN price < 10 THEN 'Very Reasonable Title'
> WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
> ELSE 'Expensive book!'
> END,
> CAST(title AS varchar(20)) AS 'Shortened Title'
> FROM titles
> ORDER BY price
> GO
> Here is the result set:
>
> Price Category Shortened Title
> --------------------- --------------------
> Not yet priced Net Etiquette
> Not yet priced The Psychology of Co
> Very Reasonable Title The Gourmet Microwav
> Very Reasonable Title You Can Combat Compu
> Very Reasonable Title Life Without Fear
> Very Reasonable Title Emotional Security:
> Coffee Table Title Is Anger the Enemy?
> Coffee Table Title Cooking with Compute
> Coffee Table Title Fifty Years in Bucki
> Coffee Table Title Sushi, Anyone?
> Coffee Table Title Prolonged Data Depri
> Coffee Table Title Silicon Valley Gastr
> Coffee Table Title Straight Talk About
> Coffee Table Title The Busy Executive's
> Expensive book! Secrets of Silicon V
> Expensive book! Onions, Leeks, and G
> Expensive book! Computer Phobic And
> Expensive book! But Is It User Frien
>
> (18 row(s) affected)
> C. Use CASE with SUBSTRING and SELECT
> This example uses CASE and THEN to produce a list of authors, the book
> identification numbers, and the book types each author has written.
>
> USE pubs
> SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
> RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
> Type =
> CASE
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
> END
> FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
> Here is the result set:
>
> Name au_id title_id Type
> ------------------------- ----------- -------- -------------------
> Johnson White 172-32-1176 PS3333 Psychology
> Marjorie Green 213-46-8915 BU1032 Business
> Marjorie Green 213-46-8915 BU2075 Business
> Cheryl Carson 238-95-7766 PC1035 Popular Computing
> Michael O'Leary 267-41-2394 BU1111 Business
> Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
> Dean Straight 274-80-9391 BU7832 Business
> Abraham Bennet 409-56-7008 BU1032 Business
> Ann Dull 427-17-2319 PC8888 Popular Computing
> Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
> Charlene Locksley 486-29-1786 PC9999 Popular Computing
> Charlene Locksley 486-29-1786 PS7777 Psychology
> Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
> Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
> Innes del Castillo 712-45-1867 MC2222 Modern Cooking
> Michel DeFrance 722-51-5454 MC3021 Modern Cooking
> Stearns MacFeather 724-80-9391 BU1111 Business
> Stearns MacFeather 724-80-9391 PS1372 Psychology
> Livia Karsen 756-30-7391 PS1372 Psychology
> Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
> Sheryl Hunter 846-92-7186 PC8888 Popular Computing
> Anne Ringer 899-46-2035 MC3021 Modern Cooking
> Anne Ringer 899-46-2035 PS2091 Psychology
> Albert Ringer 998-72-3567 PS2091 Psychology
> Albert Ringer 998-72-3567 PS2106 Psychology
>
> (25 row(s) affected)"/Fredrik
>
>[/color] | | | | re: SQL Problem
Wow. that certainly says a lot.
Did I mention that my Access query includes queries, which include queries?
How is that handled with t-sql?
tia,
JMorrell
"Fredrik Wahlgren" wrote:
[color=blue]
>
> "JMorrell" <JMorrell@discussions.microsoft.com> wrote in message
> news:06910314-3CE0-46D2-BF04-9A1491915A09@microsoft.com...[color=green]
> > Thanks for the reply. The SQL statement in Access is based on multiple
> > queries. Having said that, here is my SSQL statement:
> >
> > SELECT TblEmp.SSN, [lname] & ", " & [fname] AS Name, TblLeaveBal.AnnBal,
> > TblLeaveBal.SickBal, TblLeaveBal.CompBal, qrySumSick.SumSick,
> > qrySumAnn.SumAnn, qrySumComp.SumOfLeaveHours AS SumComp, IIf([compbal] Is[/color]
> Not[color=green]
> > Null,(nz([compbal]-nz([sumofleavehours]))),(IIf([sumofleavehours] Is
> > Null,[compbal]))) AS NewCompBal, (([sickbal]-[sumsick])+[sumofsupdateval])[/color]
> AS[color=green]
> > NS, IIf([sumsick] Is
> >[/color]
> Null,([sickbal]+[sumofsupdateval]),(([sickbal]-[sumsick])+[sumofsupdateval])
> )[color=green]
> > AS NewSickBal, IIf([sumann] Is
> > Null,([annbal]+[sumofaupdateval]),(([annbal]-[sumann])+[sumofaupdateval]))[/color]
> AS[color=green]
> > NewAnnBals, IIf([compbal] Is
> > Null,([compbal]+[sumofleavehours]),(([compbal]-[sumofleavehours]))) AS
> > NewzCompBal, IIf([sumann] Is Null,([annbal]+[sumofaupdateval])) AS[/color]
> NewAnnBal,[color=green]
> > qrySumAnnAccrue.SumOfAUpdateVal
> > FROM (((((TblEmp LEFT JOIN TblLeaveBal ON TblEmp.SSN = TblLeaveBal.SSN)[/color]
> LEFT[color=green]
> > JOIN qrySumAnn ON TblEmp.SSN = qrySumAnn.SSN) LEFT JOIN qrySumSick ON
> > TblEmp.SSN = qrySumSick.SSN) LEFT JOIN qrySumSickAccrue ON TblEmp.SSN =
> > qrySumSickAccrue.EmpID) LEFT JOIN qrySumAnnAccrue ON TblEmp.SSN =
> > qrySumAnnAccrue.EmpID) LEFT JOIN qrySumComp ON TblEmp.SSN = qrySumComp.SSN
> > WHERE (((TblEmp.Term)=0))
> > ORDER BY [lname] & ", " & [fname];
> >
> > Most appreciative of any help!
> > JMorrell
> >
> >[/color]
> You can't use IIF in SQL Server. The equivalent is called CASE. Snipped
> from books online.
> "
> CASE
> Evaluates a list of conditions and returns one of multiple possible result
> expressions.
>
> CASE has two formats:
>
> a.. The simple CASE function compares an expression to a set of simple
> expressions to determine the result.
>
>
> b.. The searched CASE function evaluates a set of Boolean expressions to
> determine the result.
> Both formats support an optional ELSE argument.
>
> Syntax
> Simple CASE function:
>
> CASE input_expression
> WHEN when_expression THEN result_expression
> [ ...n ]
> [
> ELSE else_result_expression
> ]
> END
>
> Searched CASE function:
>
> CASE
> WHEN Boolean_expression THEN result_expression
> [ ...n ]
> [
> ELSE else_result_expression
> ]
> END
>
> Arguments
> input_expression
>
> Is the expression evaluated when using the simple CASE format.
> input_expression is any valid Microsoft® SQL ServerT expression.
>
> WHEN when_expression
>
> Is a simple expression to which input_expression is compared when using the
> simple CASE format. when_expression is any valid SQL Server expression. The
> data types of input_expression and each when_expression must be the same or
> must be an implicit conversion.
>
> n
>
> Is a placeholder indicating that multiple WHEN when_expression THEN
> result_expression clauses, or multiple WHEN Boolean_expression THEN
> result_expression clauses can be used.
>
> THEN result_expression
>
> Is the expression returned when input_expression equals when_expression
> evaluates to TRUE, or Boolean_expression evaluates to TRUE. result
> expression is any valid SQL Server expression.
>
> ELSE else_result_expression
>
> Is the expression returned if no comparison operation evaluates to TRUE. If
> this argument is omitted and no comparison operation evaluates to TRUE, CASE
> returns NULL. else_result_expression is any valid SQL Server expression. The
> data types of else_result_expression and any result_expression must be the
> same or must be an implicit conversion.
>
> WHEN Boolean_expression
>
> Is the Boolean expression evaluated when using the searched CASE format.
> Boolean_expression is any valid Boolean expression.
>
> Result Types
> Returns the highest precedence type from the set of types in
> result_expressions and the optional else_result_expression. For more
> information, see Data Type Precedence.
>
> Result Values
> Simple CASE function:
> a.. Evaluates input_expression, and then, in the order specified,
> evaluates input_expression = when_expression for each WHEN clause.
>
>
> b.. Returns the result_expression of the first (input_expression =
> when_expression) that evaluates to TRUE.
>
>
> c.. If no input_expression = when_expression evaluates to TRUE, SQL Server
> returns the else_result_expression if an ELSE clause is specified, or a NULL
> value if no ELSE clause is specified.
> Searched CASE function:
> a.. Evaluates, in the order specified, Boolean_expression for each WHEN
> clause.
>
>
> b.. Returns result_expression of the first Boolean_expression that
> evaluates to TRUE.
>
>
> c.. If no Boolean_expression evaluates to TRUE, SQL Server returns the
> else_result_expression if an ELSE clause is specified, or a NULL value if no
> ELSE clause is specified.
> Examples
> A. Use a SELECT statement with a simple CASE function
> Within a SELECT statement, a simple CASE function allows only an equality
> check; no other comparisons are made. This example uses the CASE function to
> alter the display of book categories to make them more understandable.
>
> USE pubs
> GO
> SELECT Category =
> CASE type
> WHEN 'popular_comp' THEN 'Popular Computing'
> WHEN 'mod_cook' THEN 'Modern Cooking'
> WHEN 'business' THEN 'Business'
> WHEN 'psychology' THEN 'Psychology'
> WHEN 'trad_cook' THEN 'Traditional Cooking'
> ELSE 'Not yet categorized'
> END,
> CAST(title AS varchar(25)) AS 'Shortened Title',
> price AS Price
> FROM titles
> WHERE price IS NOT NULL
> ORDER BY type, price
> COMPUTE AVG(price) BY type
> GO
> Here is the result set:
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Business You Can Combat Computer S 2.99
> Business Cooking with Computers: S 11.95
> Business The Busy Executive's Data 19.99
> Business Straight Talk About Compu 19.99
>
> avg
> ==========================
> 13.73
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Modern Cooking The Gourmet Microwave 2.99
> Modern Cooking Silicon Valley Gastronomi 19.99
>
> avg
> ==========================
> 11.49
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Popular Computing Secrets of Silicon Valley 20.00
> Popular Computing But Is It User Friendly? 22.95
>
> avg
> ==========================
> 21.48
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Psychology Life Without Fear 7.00
> Psychology Emotional Security: A New 7.99
> Psychology Is Anger the Enemy? 10.95
> Psychology Prolonged Data Deprivatio 19.99
> Psychology Computer Phobic AND Non-P 21.59
>
> avg
> ==========================
> 13.50
>
> Category Shortened Title Price
> ------------------- ------------------------- --------------------------
> Traditional Cooking Fifty Years in Buckingham 11.95
> Traditional Cooking Sushi, Anyone? 14.99
> Traditional Cooking Onions, Leeks, and Garlic 20.95
>
> avg
> ==========================
> 15.96
>
> (21 row(s) affected)
> B. Use a SELECT statement with simple and searched CASE function
> Within a SELECT statement, the searched CASE function allows values to be
> replaced in the result set based on comparison values. This example displays
> the price (a money column) as a text comment based on the price range for a
> book.
>
> USE pubs
> GO
> SELECT 'Price Category' =
> CASE
> WHEN price IS NULL THEN 'Not yet priced'
> WHEN price < 10 THEN 'Very Reasonable Title'
> WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
> ELSE 'Expensive book!'
> END,
> CAST(title AS varchar(20)) AS 'Shortened Title'
> FROM titles
> ORDER BY price
> GO
> Here is the result set:
>
> Price Category Shortened Title
> --------------------- --------------------
> Not yet priced Net Etiquette
> Not yet priced The Psychology of Co
> Very Reasonable Title The Gourmet Microwav
> Very Reasonable Title You Can Combat Compu
> Very Reasonable Title Life Without Fear
> Very Reasonable Title Emotional Security:
> Coffee Table Title Is Anger the Enemy?
> Coffee Table Title Cooking with Compute
> Coffee Table Title Fifty Years in Bucki
> Coffee Table Title Sushi, Anyone?
> Coffee Table Title Prolonged Data Depri
> Coffee Table Title Silicon Valley Gastr
> Coffee Table Title Straight Talk About
> Coffee Table Title The Busy Executive's
> Expensive book! Secrets of Silicon V
> Expensive book! Onions, Leeks, and G
> Expensive book! Computer Phobic And
> Expensive book! But Is It User Frien
>
> (18 row(s) affected)
> C. Use CASE with SUBSTRING and SELECT
> This example uses CASE and THEN to produce a list of authors, the book
> identification numbers, and the book types each author has written.
>
> USE pubs
> SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
> RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
> Type =
> CASE
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
> WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
> END
> FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
> Here is the result set:
>
> Name au_id title_id Type
> ------------------------- ----------- -------- -------------------
> Johnson White 172-32-1176 PS3333 Psychology
> Marjorie Green 213-46-8915 BU1032 Business
> Marjorie Green 213-46-8915 BU2075 Business
> Cheryl Carson 238-95-7766 PC1035 Popular Computing
> Michael O'Leary 267-41-2394 BU1111 Business
> Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
> Dean Straight 274-80-9391 BU7832 Business
> Abraham Bennet 409-56-7008 BU1032 Business
> Ann Dull 427-17-2319 PC8888 Popular Computing
> Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
> Charlene Locksley 486-29-1786 PC9999 Popular Computing
> Charlene Locksley 486-29-1786 PS7777 Psychology
> Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
> Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
> Innes del Castillo 712-45-1867 MC2222 Modern Cooking
> Michel DeFrance 722-51-5454 MC3021 Modern Cooking
> Stearns MacFeather 724-80-9391 BU1111 Business
> Stearns MacFeather 724-80-9391 PS1372 Psychology
> Livia Karsen 756-30-7391 PS1372 Psychology
> Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
> Sheryl Hunter 846-92-7186 PC8888 Popular Computing
> Anne Ringer 899-46-2035 MC3021 Modern Cooking
> Anne Ringer 899-46-2035 PS2091 Psychology
> Albert Ringer 998-72-3567 PS2091 Psychology
> Albert Ringer 998-72-3567 PS2106 Psychology
>
> (25 row(s) affected)"/Fredrik
>
>
>[/color] | | | | re: SQL Problem
"JMorrell" <JMorrell@discussions.microsoft.com> wrote in message
news:D23F6932-DC81-4F0C-9BF5-B91795561E7F@microsoft.com...[color=blue]
> Wow. that certainly says a lot.
>
> Did I mention that my Access query includes queries, which include[/color]
queries?[color=blue]
> How is that handled with t-sql?
>
> tia,
> JMorrell
>
>[/color]
I don't know much about Access. I guess sub queries are handled pretty much
the same way. SQL Server is probably more stringent.
/Fredrik | | | | re: SQL Problem
"JMorrell" <JMorrell@discussions.microsoft.com> wrote:
[color=blue]
>Wow. that certainly says a lot.
>
>Did I mention that my Access query includes queries, which include queries?
>How is that handled with t-sql?
>
>tia,
>JMorrell
>
>[/color]
The Access "query object" plays the same role as a "view"
does in a relational database. AFAIK a view may refer to
another view - though the this may not be desirable for
other reasons.
Many of these "Access queries" would be better implemented
as stored procedures in SQL-Server. As stored procedures can
use other stored procedures, you could minimize your
duplication of logic and code.
However, while you stated that you are working with
SQL-Server for the sake of the walkthrough, you didn't
expressly state that you are going to migrate your data from
Access to SQL-Server. OleDb can work with an Access database
file, e.g.:
If you are using Access, you need to use the
OleDbConnection, OleDbCommand, and OleDbDataAdapter
object found in the System.Data.OleDb namespace and
change your connection string to something like the
following:
"Provider=Microsoft.Jet.OleDb.4.0;
Data Source=C:\Access\Northwind.mdb"
But then you are going to be limited to the functionality
and syntax of Access. http://msdn.microsoft.com/library/de...singadonet.asp
'Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.'
Martin Fowler,
'Refactoring: improving the design of existing code', p.15 | | | | re: SQL Problem
Thanks again for the reply.
My Access db is distributed to one user for input and about a dozen users
for view only access. The data sits on our SQL server. All of my queries
sit on each user's pc with the exception of a stored procedure being called
for 2 reports.
As mentioned, I'm a newby with VB and am wanting to eventually make my
application web enabled. I wanted to get my feet wet by creating a windows
app to produce an often-used display screen showing data.
Unless there are other suggestions, I should bone up on stored procedures.
JMorrell
"UAError" wrote:
[color=blue]
> "JMorrell" <JMorrell@discussions.microsoft.com> wrote:
>[color=green]
> >Wow. that certainly says a lot.
> >
> >Did I mention that my Access query includes queries, which include queries?
> >How is that handled with t-sql?
> >
> >tia,
> >JMorrell
> >
> >[/color]
>
> The Access "query object" plays the same role as a "view"
> does in a relational database. AFAIK a view may refer to
> another view - though the this may not be desirable for
> other reasons.
>
> Many of these "Access queries" would be better implemented
> as stored procedures in SQL-Server. As stored procedures can
> use other stored procedures, you could minimize your
> duplication of logic and code.
>
> However, while you stated that you are working with
> SQL-Server for the sake of the walkthrough, you didn't
> expressly state that you are going to migrate your data from
> Access to SQL-Server. OleDb can work with an Access database
> file, e.g.:
>
> If you are using Access, you need to use the
> OleDbConnection, OleDbCommand, and OleDbDataAdapter
> object found in the System.Data.OleDb namespace and
> change your connection string to something like the
> following:
>
> "Provider=Microsoft.Jet.OleDb.4.0;
> Data Source=C:\Access\Northwind.mdb"
>
>
> But then you are going to be limited to the functionality
> and syntax of Access.
>
> http://msdn.microsoft.com/library/de...singadonet.asp
>
>
> 'Any fool can write code that a computer can understand.
> Good programmers write code that humans can understand.'
> Martin Fowler,
> 'Refactoring: improving the design of existing code', p.15
>[/color] | | | | re: SQL Problem
Hi,
Your specific problem would be better addressed by using the TSQL isnull
statement. example as follows
select ISNULL(col_1,col_2) FROM MyTable
This would return the contents of col_1 unless it was null, then it would
return the contents of col_2
HTH
Paul.
"JMorrell" wrote:
[color=blue]
> First post to this community so am not sure if this is the correct place.
> Here goes.
>
> I have a MS Access db that keeps track of employees sick and annual leave
> balances. In it, I have a report, built around a query, that lists
> everyone's leave balances. There are columns (fields) that are generated
> with nested IIF statements. Everything there works like it should and the
> results are accurate.
>
> Now, I'm wanting to put this SQL query to a VB project. I've gone through
> the walkthroughs to bring SQL Server db table information to the form by an
> creating OleDbDataAdapter, an OleDbConnection, binding form controls to the
> Data Set, etc. I'm getting a better understanding of how that works but not
> fluent yet, if you know what I mean.
>
> My problem is that I'm can't create a new OleDbConnection with the SQL
> statement copied from my MS Access application. VB doesn't seem to like my
> nested IIF statements (with IS NULL or IS NOT NULL). I'm pretty sure there's
> a way around this, or is there?
>
> Thanks for the help.
> --
> JMorrell[/color] |  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|