Skip to content
Advertisement

T-SQL Pivot Behavior Question, Pivot not behaving as expected with CAS Numbers

I have a database of analytical laboratory data, one analyte per record (MS SQL Server Express 2014 (12.0.2000)). I have successfully (more or less) gotten results that I expect using a chemical’s name. Chemical names, however, can vary from one lab to another. Example being trichloroethylene is the same as trichloroethene. Chemicals also have a Chemical Abstracts Service number (CAS Number) that is unique to the chemical, regardless of the synonyms used for that chemical and are of the format 99999-99-9, where the first series of digits can range from 2 to 5, from what I have seen. When I use the CAS Number all it returns are NULLS. Following are the two variations, first the successful one, then the one I really want to use, as the query should rely on the field that contains unique values.

This returns values where the chemical name is spelled the same as in the query, but there are multiple laboratories and they don’t spell the chemical names consistently. That is why I would prefer to use CAS Numbers.

Then the query relying on the CAS Number:

This query returns NULL for every entry. I am confused as I have seen multiple online examples where the pivoted fields contain numbers and they apparently work. CAS Numbers obviously aren’t numbers, but strings, and setting them off with [‘307-24-4′] or [N’307-24-4’] yields nothing different.

So, what obvious thing am I overlooking? In advance of any responses, I want to thank you for your time.

Dale

Advertisement

Answer

Based on your explanation the pivot in your second query should be:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement