I'm setting up a .NET program to manipulate some data in our Redshift database. Most of my commands go through without a problem, but when I try to execute an INSERT statement involving a CONVERT function in the select, it replaces the entirety of the CONVERT function with the text "TODO: FUNC" and then I get this error:
System.Data.Odbc.OdbcException (0x80131937): ERROR [42601] [Amazon][Amazon Redshift] (30) Error occurred while trying to execute a query: [SQLState 42601] ERROR: syntax error at or near ":" LINE 1: ...case when package_size is null then null else TODO: FUNC end...
The SQL command runs with no errors from Aginity Workbench. If I take out the CONVERT function, I get an error that my types are mismatched.
OdbcConnection conn = new OdbcConnection(connString);
conn.Open();
string sql = "truncate table sch.table; insert into sch.table (package_size) select case when package_size is null then null else convert(numeric(9,3),left(package_size,6) || '.' || right(package_size,3)) end as package_size from sch.stage_table;";
OdbcCommand da = new OdbcCommand(sql, conn);
da.ExecuteNonQuery();
conn.Close();
For any reason, the CONVERT function in this scenario doesn't work. But you can use CAST instead of this.
CAST ( expression AS type )
expression :: type
In your case:
cast(left(package_size,6) || '.' || right(package_size,3) as numeric(9,3))
official documentation: https://docs.aws.amazon.com/redshift/latest/dg/r_CAST_function.html
User contributions licensed under CC BY-SA 3.0