I am trying to validate if a string variable in a U-SQL SELECT can be interpreted as an integer, som I am trying to use int.TryParse to replace "0" and "" with default value of two and everything above 10 with 10. Here is the code:
DECLARE @maxAvgkWh double = 100.00;
DECLARE @defaultM2 int = 90;
DECLARE @defaultPersons int = 2;
// Extracting installations and their information
@forDecisionTree =
EXTRACT [InstallationId] string,
[PrimaryHeatingType] string,
[Persons] string,
[SquareMeters] string,
[LatestAvgDailykWh] double
FROM "adl://some text file in azure data lake"
USING Extractors.Tsv(skipFirstNRows : 1, silent : true);
// Making sure that NULLS and zeroes and abnormal values are replaced with default values
@forDecisionTreeHouseTypeReplNulls =
SELECT [InstallationId],
[PrimaryHeatingType],
(
! int.TryParse(Persons, out var _pers) || _pers <= 0 ?
@defaultPersons :
_pers > 10 ?
10 :
_pers
).ToString() AS [Persons],
(
! int.TryParse([SquareMeters], out var _m2) || _m2 <= 0 ?
@defaultM2 :
_m2 > 500 ?
500 :
_m2
).ToString() AS [SquareMeters],
[LatestAvgDailykWh]
FROM @forDecisionTreeHouseType
WHERE [LatestAvgDailykWh] < @maxAvgkWh;
I keep getting the following error:
C# error CS1003: Syntax error, ',' expected
at token '_pers', line 108 near the ###:
...! int.TryParse([Persons], out var ### _pers) || _pers <= 0 ? ...
TryParse
is not one of the functions you can call directly. It has to be wrapped as an inline function. A simple example:
@output =
SELECT FirstName,
(
(Func<string, int?>)
(inputString => // input_paramater
{
int outputValue;
return int.TryParse(inputString, out outputValue) ? (int?)outputValue : (int?)null;
}
)
) (Salary) AS someDate
FROM @Employees;