Il 10/02/2015 09:33, jo ha scritto:
Hi all,
I can't realize how to create a sql custom function. I need to
transform a string column value into a number as in:
def mese(par):
mm = dict(A=1,B=2,C=3,D=4,E=5,H=6,L=7,M=8,P=9,R=10,S=11,T=12)
return mm[par.upper()]
I tried to use it in a query as this one below, but it doesn't work:
session.query(Ana).filter(Ana.c.mese==mese(sa.func.substr(Ana.c.cfiscale,8,9))
Please, could someone help me to write a such function?
thanks
j
Hi Jo,
probably you forgot to register function?
Sure you can optimize this code, but this work.
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
e = create_engine('sqlite://')
metadata = MetaData()
t = Table('t', metadata, Column('v', String(1)))
metadata.create_all(e)
def mese(par):
mm = dict(A=1,B=2,C=3,D=4,E=5,H=6,L=7,M=8,P=9,R=10,S=11,T=12)
return mm[par.upper()]
conn = sessionmaker(e)().connection()
conn.connection.connection.create_function("mese",1,mese)
conn.execute( t.insert().values(v='P'))
conn.execute( t.insert().values(v='D'))
conn.execute("select v, mese(v) from t").fetchall()
>>>[(u'P', 9), (u'D', 4)]
Glauco
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.