Results 1 to 13 of 13
  1. #1
    alka is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Nov 2013
    Posts
    25

    CASE in SQL

    I have 3 column



    Column 1 from another queryA has the field ValueA
    Column 2 from another queryB has the field ValueB
    and I want my 3rd column to be: =[queryA].[ValueA]-[queryB].[ValueB]


    So put in field of colum3: ValueColumn3: [queryA].[ValueA]-[queryB].[ValueB]

    But now I want that if the ValueColumn3 is negative, I want it to show 0 else if it is >0, show the calculated value.

    I have tried several CASE but everytime I get a synthax error.

    Tried something like this:


    CASE
    WHEN ([queryA].[ValueA]-[queryB].[ValueB]) BETWEEN -100000000 AND 0 THEN 0
    ELSE
    ([queryA].[ValueA]-[queryB].[ValueB]])
    END As ValueColumn3




    There must be a better way to do it and I cant get any case to work

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    CASE
    WHEN [queryA].[ValueA]-[queryB].[ValueB] < 0 THEN 0
    ELSE
    [queryA].[ValueA]-[queryB].[ValueB]
    END As ValueColumn3
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    alka is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Nov 2013
    Posts
    25
    Still get the same error (you can see that the code i use is different but I wanted you to understand)

    Click image for larger version. 

Name:	1.png 
Views:	9 
Size:	38.4 KB 
ID:	14538

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is this an Access query or SQL Server? Case doesn't work in Access.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    alka is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Nov 2013
    Posts
    25
    Oh I didn't know that! It is a Access query :S

  6. #6
    alka is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Nov 2013
    Posts
    25
    I should do my case in a event code? or is there another way to do this in SQL Query Access?

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can use an IIf() function in place of the Case function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    alka is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Nov 2013
    Posts
    25
    IFF(
    -([inventaire].[lb_qte_recu]-[Besoin travail].[lbt_qte_necessaire])< 0 THEN 0
    ELSE
    -([inventaire].[lb_qte_recu]-[Besoin travail].[lbt_qte_necessaire])
    END) As Lb_qte_demande


    Same error

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Check in help for the proper syntax.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    alka is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Nov 2013
    Posts
    25
    http://msdn.microsoft.com/en-us/libr...ice.15%29.aspx


    <Message> in query expression <expression>. (Error 3075)


  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    IIf(Criteria, TrueResult, FalseResult)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    alka is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Nov 2013
    Posts
    25
    WOW YAY! Thanks man!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. CASE STATEMENT or IF-ELSE-THEN? HELP
    By Shakenaw in forum Access
    Replies: 9
    Last Post: 06-08-2015, 11:24 AM
  2. SQL code help (Case)
    By alka in forum Access
    Replies: 10
    Last Post: 11-26-2013, 06:15 PM
  3. Replies: 5
    Last Post: 10-23-2012, 03:55 PM
  4. Case when
    By krufruf in forum Queries
    Replies: 2
    Last Post: 07-20-2012, 03:59 PM
  5. Case Else
    By FinChase in forum Programming
    Replies: 3
    Last Post: 02-01-2012, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums