you can emit that exact SQL using func.abs() in conjunction with the extract() 
function, which is a standalone SQLA construct.

from sqlalchemy import func, extract

func.abs(extract('epoch', header.udatetime - self.header.udatetime))


On Jan 27, 2011, at 4:21 PM, Petra Clementson wrote:

> hi all,
> 
> I've been trying to work out some SQLalchemy code to query two datetime 
> columns and choose the one closest in time. I have some code that does this, 
> but it requires using PostgreSQL and looks a little messy:
> 
> query = query.order_by("ABS(EXTRACT(EPOCH FROM (header.utdatetime - 
> :utdatetime_x)))").params(utdatetime_x= self.header.utdatetime)
> 
> I've been working on a piece of code that is all SQLalchemy, but I keep 
> getting an error that I can't use the absolute value function on an interval 
> and casting the interval to Numeric isn't working either:
> 
> query = query.order_by(func.abs(cast(Header.utdatetime - 
> self.header.utdatetime, Numeric())).asc())
> 
> 
> any help would be greatly appreciated.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to