Re: Select Expression Operators

From: Gary K (GaryK_at_discussions.microsoft.com)
Date: 10/01/04

  • Next message: Steve Kass: "Re: convert from hexadecimal to a decimal"
    Date: Thu, 30 Sep 2004 21:05:04 -0700
    
    

    Thanks Hugo. I've already found that info out (me shoulda looked first, me
    very dumb), but thank you very much for answering and doing a complete job as
    well. :)

    "Hugo Kornelis" wrote:

    > On Mon, 30 Aug 2004 22:49:06 -0700, Gary K wrote:
    >
    > >Sorry for what must seem like a dumb question, but I was wondering why I keep
    > >getting "<operator> not recognized" errors when I try to use the following
    > >sql clause:
    > >
    > >SELECT personid, IIF(LEN(ISNULL(middlename, '')) = 0, firstname + ' ' +
    > >lastname, firstname + ' ' + middlename + ' ' + lastname) AS FullName FROM
    > >mydatabase
    > >
    > >Regardless of which operator I use (=, <, >, etc..) it is unrecognized when
    > >the statement gets compiled. All I want to do is use the sql query to
    > >concatenate the name strings to a full name without having extra spaces in
    > >the middle when there is no middle name specified. I was able to use this
    > >query with MS Access but I am having trouble porting it to SQL server. (Mind
    > >you, in access I was able to use ISNULL by itself, not just checking for zero
    > >length strings) Can someone please point out my error?
    >
    > Hi Gary,
    >
    > SQL Server doesn't offer the same set of functions as Access and it
    > doesn't use the same name for these functions. The functions that you
    > might wish to use in this case are:
    >
    > Access: Nz(expr[, expr])
    > SQL Server: ISNULL(expr, expr) or COALESCE(expr[, expr[, ...]])
    > ISNULL will return the second expression if the first is NULL. COALESCE
    > will return the first non-NULL expression (so with two expressions, it is
    > the same as ISNULL; with more expressions it is equivalent to nested
    > ISNULL expressions).
    >
    > Access: Isnull(expr)
    > SQL Server: expr IS NULL
    > Evaluates to true if the expression is equal to NULL, false otherwise.
    >
    > Access: IIf(expr, truepart, falsepart)
    > SQL Server: CASE (see syntax below)
    > The access function checks the first expression, which should be a logical
    > expression. If it's true, the truepart is returned; else the falsepart. In
    > SQL Server, this can be replaced by the (very powerful) CASE expression,
    > which has two variants.
    >
    > 1. Searched CASE:
    > CASE
    > WHEN boolean-expr1 THEN result-expr1
    > WHEN boolean-expr2 THEN result-expr2
    > .....
    > ELSE else-result-expr
    > END
    > If boolean-expr1 evaluates to true, result-expr1 is returned. If not, SQL
    > Server will go on to evaluate boolean-expr2 and return result-expr2 if
    > that one is true. This will continue until a logical expression is found
    > that evaulates to true or until all boolean expressions have been
    > evaluated; if the last one evaluates to false as well, else-result-expr is
    > returned.
    >
    > 2. Simple CASE:
    > CASE input-expr
    > WHEN value-expr1 THEN result-expr1
    > WHEN value-expr2 THEN result-expr2
    > .....
    > ELSE else-result-expr
    > END
    > This is really just a shorthand for
    > CASE
    > WHEN input-expr = value-expr1 THEN result-expr1
    > WHEN input-expr = value-expr2 THEN result-expr2
    > .....
    > ELSE else-result-expr
    > END
    >
    > After all this theory, let's have a look at your query. In SQL Server, you
    > could write this as
    >
    > SELECT personid, firstname + ' ' + COALESCE (middlename + ' ', '')
    > + lastname) AS FullName
    > FROM mydatabase
    >
    > (Though I would recommend you to choose another name for your table. A
    > database is not the same thing as a table; you can't select FROM a
    > database)
    >
    > Best, Hugo
    > --
    >
    > (Remove _NO_ and _SPAM_ to get my e-mail address)
    >


  • Next message: Steve Kass: "Re: convert from hexadecimal to a decimal"

    Relevant Pages

    • Re: SQL bug, ISNULL function with smalldatetime
      ... ISNULL ... Columnist, SQL Server Professional ... Is it because the ISNULL function would automatically convert the datetime to a smalldatetime? ... > INSERT testDateQuery ...
      (microsoft.public.sqlserver.programming)
    • Re: using a trigger to enforce referential integrity
      ... You the man, Hugo!!! ... > When I have to mimic other users on my dev system, I use SQL Server ... This requires setting the SQL Server authentication mode ... > authentication and provide the login name and password for the test user. ...
      (microsoft.public.sqlserver.programming)
    • Re: percentages of top 10 tallies
      ... Hugo is using what is called a derived table. ... Columnist, SQL Server Professional ... My solution uses less IO and yours has the lowest query cost. ... >Hi Tom, ...
      (microsoft.public.sqlserver.programming)
    • Re: Using ISNULL()
      ... "There is a Replace function in VBA"? ... I know how to use JOIN in a query but such usage differs from the VBA ... It sounded to me like the OP's knowledge of an ISNULL ... SQL Server has one such an ISNULL expression: ...
      (microsoft.public.access.queries)
    • Re: Batch Insert
      ... uom, work_type, status,item_number, lot_number, ... NULL, 'UNPLANNED',item_number, lot_number, (qty - ISNULL ... >Columnist, SQL Server Professional ...
      (microsoft.public.sqlserver.server)