I have a temp table that I imported using SSIS. I was having some trouble importing the data as anything other than varchar. The end result is to take the data in this table and copy it to 3 other tables but I need to copy some of the varchar columns as numeric and some as datetime.
I also have 4 columns called century, year, month and day that need to be concatenated and copied to another table as a datetime.
In general, you would use CAST or CONVERT to convert the data from VARCHAR to numeric. Post some examples of your input data and the output you need to get more details.
Did you mean converted to NUMERIC? If you really meant "converted to string", there is nothing further you need to do. VARCHAR is already string.
If you do want to convert 12 digits to NUMERIC, what is the data type of the destination column(s), specifically those that can be 10,11 and 12 digits long? The maximum an int can store is some 10-digit number that starts with a 2.
One thing that possibly might work is to simply select from your staging table and insert into your destination table(s) without you doing any conversions. The varchar columns should implicitly be converted to numeric. If that does not work - i.e., if you are getting some errors, that means there are some strings (varchar values) that cannot be converted to the numeric data appropriate for that column. There are some limitations to implicit conversions - the table on this page shows what can be converted to what.
and we run some SQL on the Staging Table to check if the data in conforms, such as "Should be a valid date" or "Should be numeric". Any rows that don't pass the test get an error number (first error found only) and a concatenated list of error messages. We then don't import any rows where ErrorNo <> 0 and we have a report that the user can run to see the ErrorMsg (and data) on rows that have an error - so they can fix up the data in the source system before the next import.
Don't rely on IsNumeric() to test a string as being a valid numeric value. There are invalid strings that it gives a false-positive result for
A reg-ex expressions is usually good enough e.g.
SELECT CASE WHEN TestValue LIKE '%[^-0-9.]%' THEN 1 -- Non numeric character
WHEN TestValue LIKE '_%-%' THEN 2 -- minus sign not at the front
WHEN TestValue LIKE '%.%.%' THEN 3 -- two or more decimal places
ELSE 0
END AS [Test],
IsNumeric(TestValue) AS [IsNumeric],
'"' + TestValue + '"'
FROM
(
SELECT '12345' AS TestValue
UNION ALL SELECT 'A12345'
UNION ALL SELECT 'A2345A'
UNION ALL SELECT '123A45'
UNION ALL SELECT '-12345'
UNION ALL SELECT '12345-'
UNION ALL SELECT '123-45'
UNION ALL SELECT '.12345'
UNION ALL SELECT '12345.'
UNION ALL SELECT '123.45'
UNION ALL SELECT '.123.45'
UNION ALL SELECT '123.45.'
UNION ALL SELECT '1E1' -- Scientific format, very unlikely that would be valid in your data!
UNION ALL SELECT '1.1'
UNION ALL SELECT '1,1'
UNION ALL SELECT '1,000'
UNION ALL SELECT ' 1' -- This might reasonably be included as "allowed"
UNION ALL SELECT '$1' -- might reasonable be included, but won't convert to NUMERIC
) AS T
--
ORDER BY [Test], [IsNumeric], TestValue