Forum Discussion

pmscorca's avatar
pmscorca
Icon for Post Prodigy rankPost Prodigy
2 years ago
Solved

Using Spark SQL to alter a column of an existing lakehouse table

Hi, I try to use a Spark SQL code to alter the data type and the lenght of a column of an existing lakehouse table. This syntax goes in error %%sql ALTER TABLE MYTABLE ALTER COLUMN mycolumn varch...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi pmscorca ,

     

    Thanks for the reply from frithjof_v .

     

    The types supported by Cast() method include: "string", "boolean", "byte", "short", "int", "long", "float", "double", "decimal", "date", "timestamp".

     

    Here is an official document about Cast() method:

    Column.Cast(String) Method (Microsoft.Spark.Sql) - .NET for Apache Spark | Microsoft Learn

     

    In Spark SQL, there is no direct way to cast a string to a varchar with a specified length.

     

    Here is an alternative:

     

    You can use withColumn and cast:

    from pyspark.sql.functions import col
    
    df = spark.sql("SELECT * FROM MYTABLE")
    df = df.withColumn("mycolumn", col("mycolumn").cast("new_data_type"))

     

    If you have any other questions, please feel free to contact me.

     

    Best Regards,
    Yang
    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!