             (kx/literal (u/date->iso-8601 expr))
             expr)]
     (case unit
-      :default         (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d %H:%i:%s") v)
-      :second          (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d %H:%i:%s") (date-trunc :second v))
-      :minute          (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d %H:%i:%s") (date-trunc :minute v))
+      :default         (date-format (str "%Y-%m-%d %H:%i:%s") v)
+      :second          (date-format (str "%Y-%m-%d %H:%i:%s") (date-trunc :second v))
+      :minute          (date-format (str "%Y-%m-%d %H:%i:%s") (date-trunc :minute v))
       :minute-of-hour  (extract-integer :minute v)
-      :hour            (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d %H:%i:%s") (date-trunc :hour v))
+      :hour            (date-format (str "%Y-%m-%d %H:%i:%s") (date-trunc :hour v))
       :hour-of-day     (extract-integer :hour v)
-      :day             (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d") (date-trunc :day v))
+      :day             (date-format (str "%Y-%m-%d") (date-trunc :day v))
       :day-of-week     (extract-integer :day_of_week v)
       :day-of-month    (extract-integer :day_of_month v)
       :day-of-year     (extract-integer :day_of_year v)
-      :week            (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d") (date-trunc :week v))
+      :week            (date-format (str "%Y-%m-%d") (date-trunc :week v))
       :week-of-year    (extract-integer :week v)
-      :month           (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d") (date-trunc :month v))
+      :month           (date-format (str "%Y-%m-%d") (date-trunc :month v))
       :month-of-year   (extract-integer :month v)
-      :quarter         (k/sqlfn :DATE_FORMAT (str "%Y-%m-%d") (date-trunc :quarter v))
+      :quarter         (date-format (str "%Y-%m-%d") (date-trunc :quarter v))
       :quarter-of-year (extract-integer :quarter v)
       :year            (extract-integer :year v))))
 

    :milliseconds (kutils/func (str "TRY_CAST(%s as TIMESTAMP)") [expr])))

(defn- date-trunc [unit expr]
  "date_trunc('interval', timestamp): truncates a timestamp to a given interval"
  (k/sqlfn :DATE_TRUNC (kx/literal unit) expr))

(defn- extract    [unit expr]
  "extract(field from expr): extracts subfields of a timestamp"

  (:require [metabase.util.korma-extensions :as kx]
            [korma.sql.utils :as kutils]
            [korma.core :as k]
            [metabase.util :as u]
            [metabase.driver :as driver])
  (:import (java.sql Timestamp)))

(defn unix-timestamp->timestamp [_ expr seconds-or-milliseconds]
