I have database that has a column that is a Georgian date like{YYYYMMDD}. I want to have a column(or 3 for year, month and day) for Jalai date. I can easily write and script using python to convert Georgian date to Jalali. This is what I write in python:
import jdatetime as jdt
georgian = str(20170828)
def GtoJ(georgian):
jalai = jdt.GregorianToJalali(int(georgian[:4]),int(georgian[4:6]),int(georgian[-2:]))
# print('{}{:02}{:02}'.format(jalai.jyear,jalai.jmonth,jalai.jday))
return '{}{:02}{:02}'.format(jalai.jyear,jalai.jmonth,jalai.jday)
print(GtoJ(georgian)) ##13960606
Assume I create an empty column in my postgresql database with name jdate, using sqlalchemy or any other sql engine I update every row with following codes:
for date in history_tabel_date:
jdate = GtoJ(str(date))
con.execute('update history_tale jdate= value_jdate where date = {}'.format(date))
What is the way to write a sql function that do the same thing?
Generally is it a good idea to write script outside sql or sql functions are better and recommended?