1

I tried to create a expression like this

let performedDate = Expression<String>("strftime('%Y-%m-%d', performedDate)")

and then compare with another string using filter method. But the generated SQL is

"strftime('%Y-%m-%d', performedDate)" = '2017-08-17'.

There is a way to remove quotation marks from "strftime('%Y-%m-%d', performedDate)" or I have to use raw SQL string?

1 Answers1

1

I solved my problem by creating a extension on Expression type like this

 
extension Expression {

    public func strftime(format: String) -> Expression {
        return Expression("strftime('\(format)', \(template))", bindings)
    }
}
 

and then use

let date = Expression<String>("startDate")

let dateFormat = date.strftime(format: "%Y-%m-%d")

You can use dateFormat as projection or in filter method as well.

  • I try to use same function but on column with datatype . So, it is crashing when try to read from output. /*** let createdFormate = createAt.strftime(format: "%Y-%m-%d") for item in try db.prepare(headAngleMaster.select(createdFormate, angle.average)) { if let average = item[angle.average] { print("\(average)") } let formattedDate = item[createdFormate] //CRASHING HERE print("\(formattedDate)") } ***/ – girish_pro Nov 28 '17 at 12:19