Search code examples
sqlteradata

How do I fix this in my SQL, it has a problem with character type: ' ', in my first one for metro it doesn't return


cod_cli = df\['cod_cli'\].tolist()

cod_cli_str = ','.join(map(str, cod_cli))

\# clientesAtacado =

query = f"""

select \* from P_VTVVIEDB.VBRA_CITY

where bracityid in (select bracityid

from P_VTVVIEDB.VSW_ADDRESS

where swobjectid in (select swsiteid from P_VTVVIEDB.VSW_SITE where swcustomerid in

(SELECT swcustomerid FROM P_VTVVIEDB.VSW_CUSTOMER WHERE CONCAT('40',left(TELCNPJ,8)) IN (${cod_cli_str}))))

;

my return the query it's this:

Bad character in format or data of SW_CUSTOMER.TELCNPJ.
 at gosqldriver/teradatasql.formatError ErrorUtil.go:85

Solution

  • # assume cod_cli as list of strings
    cod_cli_str = ','.join(f"'{item}'" for item in cod_cli) #format each item as string literal
    
    query = f"""
    select * from P_VTVVIEDB.VBRA_CITY
    where bracityid in (
        select bracityid
        from P_VTVVIEDB.VSW_ADDRESS
        where swobjectid in (
            select swsiteid 
            from P_VTVVIEDB.VSW_SITE 
            where swcustomerid in (
                SELECT swcustomerid 
                FROM P_VTVVIEDB.VSW_CUSTOMER 
                WHERE CONCAT('40', left(TRIM(TELCNPJ), 8)) IN (${cod_cli_str})
            )
        )
    );
    
    
    • make sure TELCNPJ is a string or converted/cast to a string
    • CONCAT('40', left(TELCNPJ, 8)) result has to match format of values in cod_cli_str
    • adjust query to wrap each item in '' to make sure SQL process them as string
    • Used TRIM to remove unexpected white space [you can choose to handle more unexpected values]