Archive for August, 2005

Concatenation and logic in SQL calls

I’ve been doing alot of database design lately, although nothing too fancy, just some personal Access databases. However, I have run across some cool stuff that really got me excited (as pathetic as that may seem): concatenating fields and using logic in SQL calls. The only thing that aggravates me is that I’ve went this long without knowing of these little jewels. The concatenation is pretty straightforward; take field A and concatenate field B to it, then alias it as field C, like so:

SELECT tableA.fieldA & " " & tableB.fieldB AS fieldC

which gives you the data in fieldA concatenated to fieldB, separated by a space, as field C.

The logic is even better. I was importing the data from Access to Excel, so I could insert the Excel sheets into a map, all prettied up with lots of formatting. It worked great, except one field in my database contains null values, and when I imported that field from Access, Excel put zeros in place of the null values. This was unacceptable. So I figured that with the IIf function in my SQL call, I could get only the records from the field that were not null. Problem solved. In the end, the call looked something like this:

SELECT tblWell.Well_Name & ' ' & tblWell.Well_No AS ShortWellName, tblWell.SpudDate, tblWell.Status, tblCompletion.Frac, tblGeology.RHOB_Pay_T, tblGeology.Shale_Thick, tblProduction.Online_Date, IIf(tblProduction.Rate<>‘ ‘,tblProduction.Rate & ” on ” & (Format(tblProduction.Rate_Date,”m/d/yyyy”))) AS CurrProd
FROM (((tblWell LEFT JOIN tblCompletion ON tblWell.ID = tblCompletion.ID) LEFT JOIN tblGeology ON tblWell.ID = tblGeology.ID) INNER JOIN tblProduction ON tblWell.ID = tblProduction.ID) INNER JOIN tblFieldNames ON tblWell.FieldNo = tblFieldNames.ID
WHERE (((twlWell.Status)<>‘Proposed’)) AND (((tblFieldNames.FieldName)=’Whatever Field’))
ORDER BY SpudDate ASC;

Whew.