SQLTeam.com | Weblogs | Forums

SQL equivalent in Python

Hello Community,

I’m trying to write a SQL function that is equivalent to following Python function. Can someone help me with the code to in SQL?

def rename_file(origin_path, dest_path, file_type, new_name):
  filelist = dbutils.fs.ls(origin_path)#list all files from origin path
  filtered_filelist = [x.name for x in filelist if x.name.endswith("."+file_type)]#keep names of the files that match the type requested
  if len(filtered_filelist) > 1:#check if we have more than 1 files of that type
    print("Too many "+file_type+" files. You will need a different implementation")
  elif len(filtered_filelist) == 0: #check if there are no files of that type
    print("No "+file_type+" files found")
    dbutils.fs.mv(origin_path+"/"+filtered_filelist[0], dest_path+"/"+new_name+"."+file_type)#move the file to a new path (can be the same) changing the name in the process

TBH, I just curious as to whether the same can be accomplished in SQL.


It can be written in SQL
using case statements


please see if the below link helps you