IF in T-SQL Column Expression

  • Thread starter Thread starter grrr223
  • Start date Start date
G

grrr223

PLEASE PLEASE PLEASE HELP ME!!! Sorry, but this has been driving m
crazy all day and I'm about to kill someone or something.

I have a report in an Access Data Project that I want to group in tw
groups:

1. Previous Activity - Records with doc_date < @StartDate
2. Current Activity - Records with doc_date >= @StartDate

Since Access's grouping features are faily me, *I have decided t
simply add a column to to the query I am using as the recordsource fo
my report that simply evaluates to 0 if the record meets condition 1
and evaluates to 1 if it meets condition 2.* I will then group m
report on that column to get exactly the two groups I want.

I am looking for T-SQL syntax to get my column into my query. I alread
know how to group by the column.

*I essentially want the equivalent of IFF(doc_date < @StartDate,0,1
but T-SQL obviously doesn't handle JET/VB operators, so if someon
could PLEASE help me with this simple question by telling me what th
SELECT statement might look like to add this column to my query*, I'l
be your best friend
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In T-SQL you can use the CASE statement in place of the VB IIf()
function. E.g.:

SELECT
CASE WHEN doc_date < @StartDate THEN 0 ELSE 1 END As Activity,
< other cols >
FROM ... etc. ...


MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDryBIechKqOuFEgEQIvKACgpu+pM3qCX6qAHXG0KBPsg/R6oawAnjhj
TP7HmwzlxjYRTaay0UU7Ltv+
=lI7T
-----END PGP SIGNATURE-----
 
grrr223 said:
PLEASE PLEASE PLEASE HELP ME!!! Sorry, but this has been driving me
crazy all day and I'm about to kill someone or something.

I have a report in an Access Data Project that I want to group in two
groups:

1. Previous Activity - Records with doc_date < @StartDate
2. Current Activity - Records with doc_date >= @StartDate

Since Access's grouping features are faily me, *I have decided to
simply add a column to to the query I am using as the recordsource for
my report that simply evaluates to 0 if the record meets condition 1.
and evaluates to 1 if it meets condition 2.* I will then group my
report on that column to get exactly the two groups I want.

I am looking for T-SQL syntax to get my column into my query. I already
know how to group by the column.

*I essentially want the equivalent of IFF(doc_date < @StartDate,0,1)
but T-SQL obviously doesn't handle JET/VB operators, so if someone

But it does have duplicates of certain functions.
could PLEASE help me with this simple question by telling me what the
SELECT statement might look like to add this column to my query*, I'll
be your best friend!

So, you're looking for the T-SQL equivalent of VBA's IIF function?

That would be, IIF . . .

Just open Books Online, and lookup IIF.
As nearly as I can tell, it's identical to VBA's IIF.
 
So, you're looking for the T-SQL equivalent of VBA's IIF function?
That would be, IIF . . .

Just open Books Online, and lookup IIF.
As nearly as I can tell, it's identical to VBA's IIF.

No - that is in Analysis Services, not T-SQL.

In T-SQL you have to use Case.

The query designer (if you are using it), does not support Case, so your
best bet is to get as much of the SQL written as you can in Query Designer,
then copy the SQL to Query Analyzer and add the Case statement there.

Hope that helps
 
Thank you, no matter where I looked I couldn't find the simpl
explanation that Case is the appropriate replacement for IIF, you a
least let me go home a happy person
 
Thank you, no matter where I looked I couldn't find the simpl
explanation that Case is the appropriate replacement for IIF, you a
least let me go home a happy person
 
Thank you, no matter where I looked I couldn't find the simpl
explanation that Case is the appropriate replacement for IIF, you a
least let me go home a happy person
 
Back
Top