Home > OS >  sql cast syntax
sql cast syntax

Time:09-12

Having trouble with cast syntax. I get an error message with this and I am having trouble figuring out what the problem is. I'm using pandasql.

SELECT CAST((((male_year_df.total_male_suicides) / female_year_df.total_female_suicides)) AS DECIMAL(1,5)) AS male_female_ratio), male_year_df.year

EDIT: this is the error message: OperationalError: near ")": syntax error

CodePudding user response:

your parethesis are too much of, also DECIMAL(1,5) is not allowed as the first number has to be equal or bigger than the second number

SELECT CAST(
            
              (
                (male_year_df.total_male_suicides) 
                / (female_year_df.total_female_suicides)
            ) AS DECIMAL(5,5)) AS male_female_ratio
              , male_year_df.year

You can first define the firsst number with the precision you need and then divide the second number, so that the engine will not use integer division

 SELECT CAST(male_year_df.total_male_suicides AS DECIMAL(5,5)) 
     / (female_year_df.total_female_suicides) AS male_female_ratio
              , male_year_df.year
  • Related