While extracting ERP products' references from SQL server, it is extracted as text. These references sometimes contains only numbers, and sometimes it contains numbers + the letters CN. I would like to extract it as a number when it only contains numbers, and as a text if it contains the letters CN. Unfortunately, I did not find a way to do it.
This is what I tried ("INVMB.MB110" being the the field "Reference" in the SQL server):
SELECT
CASE
WHEN INVMB.MB110 LIKE '%CN%'
THEN INVMB.MB110
ELSE CAST(INVMB.MB110 AS INT) END
AS 'Reference'
I was expecting to convert to integer only if the reference does not contain 'CN', but it seems it tries to convert it in any case. Here is the error message I get:
Failed converting varchar value '30903CN013 ' to data type int.
You can create two columns one for the string data and one for the numeric data. Assuming you have SQL Server 2016 or higher you can use the
TRY_CONVERTorTRY_PARSEfunction.The
TRY_CONVERTfunction will convert the string to a numeric if it is possible otherwise it will return NULL.Note that the
TRY_CONVERTfunction will return a numeric value of 0 if the source string is an empty string. If you don’t want 0 to be retuned for an empty string, the use theTRY_PARSEfunction.If you absolutely require the values in a single column, you can convert the values to a SQL_VARIANT types (after you converted the string to the desired base type). I personally avoid SQL_VARIANT types like the plague, there is just too many performance and implicit conversion issues associated with them. I have included an example of a query that returns some sample data.