Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Translating Access SQL Statement to T-SQL

Translating Access SQL Statement to T-SQL

Scheduled Pinned Locked Moved Database
databasehelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Morore_Charles_007
    wrote on last edited by
    #1

    I have the following code, which i generated using Access DB, i need to translate it to T-SQL, am mainly getting error on the IIF statement SELECT dbo.casyAgeGroups.Sequence, dbo.casyAgeGroups.Description, Sum(IIf([gender]='Male' And [age]>=[startage] And [age]<=[endage],1,0)) AS Male, Sum(IIf([gender]='Female' And [age]>=[startage] And [age]<=[endage],1,0)) AS Female FROM dbo.casyAgeGroups, [qryStats-AgeGenderProfileDetails] GROUP BY dbo.casyAgeGroups.Sequence, dbo.casyAgeGroups.Description, [qryStats-AgeGenderProfileDetails].EntityType HAVING ((([qryStats-AgeGenderProfileDetails].EntityType)=0));

    Richard DeemingR 1 Reply Last reply
    0
    • M Morore_Charles_007

      I have the following code, which i generated using Access DB, i need to translate it to T-SQL, am mainly getting error on the IIF statement SELECT dbo.casyAgeGroups.Sequence, dbo.casyAgeGroups.Description, Sum(IIf([gender]='Male' And [age]>=[startage] And [age]<=[endage],1,0)) AS Male, Sum(IIf([gender]='Female' And [age]>=[startage] And [age]<=[endage],1,0)) AS Female FROM dbo.casyAgeGroups, [qryStats-AgeGenderProfileDetails] GROUP BY dbo.casyAgeGroups.Sequence, dbo.casyAgeGroups.Description, [qryStats-AgeGenderProfileDetails].EntityType HAVING ((([qryStats-AgeGenderProfileDetails].EntityType)=0));

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The IIF function was added in SQL 2012[^]. If you're using an earlier version, you'll need to replace the IIF function with a CASE block[^]:

      Sum(CASE
      WHEN gender = 'Male' And age >= startage And age <= endage THEN 1
      ELSE 0
      END)

      You could also simplify the age condition by using the BETWEEN operator[^]:

      Sum(CASE
      WHEN gender = 'Male' And age Between startage And endage THEN 1
      ELSE 0
      END)


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups