kayak / pypika

PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
http://pypika.readthedocs.io/en/latest/
Apache License 2.0
2.43k stars 293 forks source link

Query with date conversion #711

Closed imnileshd closed 1 year ago

imnileshd commented 1 year ago

How do I build like this? SELECT * FROM table WHERE CONVERT(date, ProcessDate)='2022-11-22'

imnileshd commented 1 year ago

I managed to get the required output

from pypika import Query, Table
from pypika import CustomFunction
from pypika.terms import PseudoColumn

table = Table('table')
convert = CustomFunction('CONVERT', ['date', 'date_column'])

query = Query.from_(table) \
    .select('*') \
    .where(convert(PseudoColumn('date'), table.ProcessDate) == '2022-11-22')

print(query)

Output SELECT * FROM "table" WHERE CONVERT(date,"ProcessDate")='2022-11-22' Thanks