C# SSI将不可转换的nvarchar替换为0
我正在处理一个SSIS作业,该作业将数据从登录表传送到暂存表。同时,我想执行一些DQ检查,包括将一个列从nvarchar类型转换为float类型的检查。没问题,除非肯定会有不可转换的字符串(landing table的原始源文件是excel文件,用户可以在其中输入任何他们想要的内容)。我想用0替换不可转换的字符串。我对c#和SSIS非常、非常陌生。如果您在C# SSI将不可转换的nvarchar替换为0,c#,sql-server,ssis,C#,Sql Server,Ssis,我正在处理一个SSIS作业,该作业将数据从登录表传送到暂存表。同时,我想执行一些DQ检查,包括将一个列从nvarchar类型转换为float类型的检查。没问题,除非肯定会有不可转换的字符串(landing table的原始源文件是excel文件,用户可以在其中输入任何他们想要的内容)。我想用0替换不可转换的字符串。我对c#和SSIS非常、非常陌生。如果您在数据流中,您可能可以使用转换。可能的话,您还可以使用DB命令手动将nvarchar转换为与sql任务一起浮动,如下所示: SELECT (ca
数据流中,您可能可以使用转换。可能的话,您还可以使用DB命令
手动将nvarchar
转换为与sql任务一起浮动,如下所示:
SELECT (case when ISNUMERIC(@input) = 1 then CAST(@input as float) else 0 end)
例如:
declare @input nvarchar(50);
set @input = '502';
SELECT (case when ISNUMERIC(@input) = 1 then CAST(@input as float) else 0 end) as [Result]
…导致502
及
…导致0
如果您已经将数据从登录表读取到脚本作业内部的变量中,则可以按如下方式使用TryParse:
//Perhaps you are going through each row here and have a reference to the value in the questionable Field:
var questionableField = "113Typo";
float convertedValue;
if(float.TryParse(questionableField, out convertedValue))
{
//Insert convertedValue into staging table
}
else
{
//Insert 0 instead
}
如果传递的内容不能转换为该类型,则TryParse将返回false。否则,它将使用out容器(本例中为convertedValue)存储转换后的值,然后返回true。谢谢!我在DB源代码编辑器的SQL命令中使用了上面列出的SQL,它工作正常。我感谢你的帮助!
//Perhaps you are going through each row here and have a reference to the value in the questionable Field:
var questionableField = "113Typo";
float convertedValue;
if(float.TryParse(questionableField, out convertedValue))
{
//Insert convertedValue into staging table
}
else
{
//Insert 0 instead
}