Re: Query Help with IF stmt



On Fri, 1 Apr 2005 07:23:11 -0800, Andy wrote:

>Hi All,
>Can someone please help me with below access query to convert to sql ?
>UPDATE tblEmp
>SET emprank = IIf('[tblemp]![ID] Mod 3', '[tblemp]![ID] Mod 3', 3).
>Thanks in advance
>
>Andy

Hi Andy,

Hmmm, this seems to use undocumented (or maybe it is - but I couldn't
find it) conversion of some numeric datatype to boolean in Access. I'll
assume that 0 gets converted to False and 1 or 2 both to True.

The equivalent in proper SQL would probably be:

UPDATE Employees
SET EmpRank = CASE WHEN ID % 3 = 0 THEN 3 ELSE ID % 3 END

And some other ways to achieve the same result are:

UPDATE Employees
SET EmpRank = ((ID - 1) % 3) + 1

UPDATE Employees
SET EmpRank = COALESCE(NULLIF(ID % 3, 0), 3)


However, if the EmpRank will always be based on ID as above, then the
proper solution is to not store it at all - simply calculate the rank in
a query, or (if you must) included it as a computed column. That way,
you'll never have to worry about values getting out of sync anymore.
(Of course, if this is just the starting value and it might change
later, then this remark doesn't apply).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
.



Relevant Pages

  • Re: Automating Access from Excel
    ... Thanks Andy! ... Actually I got my code running - it was a syntax thing. ... > This might be a help for getting data to and from Excel and Access: ... > includes examples of using variables in SQL queries. ...
    (microsoft.public.excel.programming)
  • RE: List of Matches in WHERE Clause
    ... In that case i would use some code to insert a new SQL to a Query ... to an Access Query from my application. ... open-ended list parameter to an Access query? ... where SomeColumn In ...
    (microsoft.public.access.queries)
  • Re: Grosse Tabellen wegsichern!? Bitte Eure Meinung!
    ... "Andy Haberer" schrieb im ... > Hi Christa, ... > auch am sinnvollsten die Daten auf dem SQL Server zu belassen... ... >> Daten, di eälter als 6 Monate sind, in die neue Tabelle schreiben und aus ...
    (microsoft.public.de.sqlserver)
  • Re: MS Access SQL > ASP SQL problem....
    ... > this code to work in ASP? ... The only way to debug a sql statement is to know what it is. ... Access Query Builder and run it without modification (unless wildcards are ... Please reply to the newsgroup. ...
    (microsoft.public.inetserver.asp.general)
  • Re: Looping through recordset
    ... How to loop through a global variable Rowset ... "Andy C" wrote in message ... > I have an SQL Execute task that returns a recordset. ... What I want to do is use this recordset to start a workflow of several objects for each row that is returned. ...
    (microsoft.public.sqlserver.dts)