Solved

Access SQL to T-SQL

Posted on 2015-01-28
11
286 Views
Last Modified: 2015-01-28
I'm faily new to T-SQL (on a SQL Server 2014), and are trying to convert this  simple Access SQL to T-SQL - as far as I can read IIF/IF is also valid from SQL Server 2012, but I can't see the forrest for trees :)

Please advice
SELECT tbl_CHAIN.ChainID, tbl_CHAIN_SORT.ArticleID, IIf([ArticleName] Is Null,'N/A',[ArticleName]) AS ArticleN
FROM tbl_ARTICLE RIGHT JOIN (tbl_CHAIN LEFT JOIN tbl_CHAIN_SORT ON tbl_CHAIN.ChainID = tbl_CHAIN_SORT.ChainID) ON tbl_ARTICLE.ArticleID = tbl_CHAIN_SORT.ArticleID;
0
Comment
Question by:Bojerne
  • 5
  • 4
  • 2
11 Comments
 
LVL 11

Accepted Solution

by:
David Kroll earned 250 total points
ID: 40575516
SELECT tbl_CHAIN.ChainID, tbl_CHAIN_SORT.ArticleID,
case
  when [ArticleName] is null then 'N/A'
  else [ArticleName]
end as ArticleN,
FROM tbl_ARTICLE RIGHT JOIN (tbl_CHAIN LEFT JOIN tbl_CHAIN_SORT ON tbl_CHAIN.ChainID = tbl_CHAIN_SORT.ChainID) ON tbl_ARTICLE.ArticleID = tbl_CHAIN_SORT.ArticleID;
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40575517
Change

IIf([ArticleName] Is Null,'N/A',[ArticleName])

to

ISNULL([ArticleName],'N/A')

See https://msdn.microsoft.com/en-us/library/ms184325.aspx for details.
0
 
LVL 1

Author Comment

by:Bojerne
ID: 40575529
That was quick :) - when trying your solution David I get:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'FROM'.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40575531
From David's solution, delete the comma at the end of line 5.
0
 
LVL 1

Author Comment

by:Bojerne
ID: 40575535
of course - thank you so much
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 40575538
Good catch Phillip!
0
 
LVL 1

Author Comment

by:Bojerne
ID: 40575550
Btw - isn't IF/IIF a T-SQL function after version 2012 ?
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40575555
No problem David.

IIF has been a function since 2012 - see the drop-down box in https://msdn.microsoft.com/en-us/library/hh213574.aspx

That's how I check if a specific function is used in version X.
0
 
LVL 1

Author Comment

by:Bojerne
ID: 40575577
Super - great tip. But howcome the Access syntax "IIf([ArticleName] Is Null,'N/A',[ArticleName]) AS ArticleN" throws an error - the T-SQL IIF syntax looks similar ?
Thank you
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40575581
Because IS NULL is not a function - you can use it in the WHERE, but not in an IIF.
0
 
LVL 1

Author Comment

by:Bojerne
ID: 40575589
aha - thank you again :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Viewers will learn how the fundamental information of how to create a table.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question