How to alias a column name with special character to new column name with underscore instead of speacial character?

Viewed 403

I have a table with column name as 'product/price' i want to rename it to product_price while creating a copy of the existing table in sparksql.

create table newtable as select product/price as product_price from existing table

this is giving me error "analysis exception error: cannot resolve 'product'

I also tried doing it as below:

create table newtable as select 'product/price' as product_price from existing table

this does change the column name but all the values in it are also changed to product_price.

1 Answers
  // select `product/price` as product_price .... to your column ?
  //

  val d1 = Seq("test", "Test1", "test2").toDF("product/price")
  d1.show(false)
  //      +-------------+
  //      |product/price|
  //      +-------------+
  //      |test         |
  //      |Test1        |
  //      |test2        |
  //      +-------------+

  val d2 = d1.withColumnRenamed("product/price", "product_price")
  d2.show(false)
  //      +-------------+
  //      |product_price|
  //      +-------------+
  //      |test         |
  //      |Test1        |
  //      |test2        |
  //      +-------------+
Related