Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

Writing SQL query for MS Access

I'm currently organizing an MS Access database and setting up some SQL queries that export data into CSV files for Yahoo Store. One of the things I had to do was to create fields in the CSV file that are compound of other existing fields in the database, and some that even depended on the existence of other physical fields in the Access database.

Suppose you had an Access database with an "Item" table that contained the following fields:

Code, Name, Description, VenderName, VenderId

And say you wanted to create a CSV file that contains the following fields:

Title, Manufacturer

Where:

Title: A combination of Code and Name fields in the database.

Manufacturer: A combination of VenderId and VenderName in the database.

The following SQL query can be added to your Access Database to make the Title field:

SELECT [Item.Code] & " " & [Item.Name] As Title FROM Item;

To make the Manufacturer field, modify the query above as follows:

SELECT [Item.Code] & " " & [Item.Name] As Title, [Item.VenderId] & " " & [Item.VenderName] As Manufacturer FROM Item;

The next challenge I had was to create a field whose value depended on the existence of another field.

For example, the CSV file contained an additional field named Caption, whose value is determined as follows:

If Description value exists, then Caption=Description

else then Caption=Code + " " + Name + " " + VenderName

How do you translate that in SQL?

The answer is to use the IIF statement, something from VB coding I believe...

The following SQL will generate the Caption field as described above:

SELECT IIF( ([Description] Is Null), ([Item.Code] & " " & [Item.Name] & " " & [Item.VenderName]), [Description]) As Caption FROM Item;

kick it on DotNetKicks.com

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author