Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6342815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:18:57+00:00 2026-05-24T20:18:57+00:00

How do I get the MIN() of a datetime column and a literal datetime

  • 0

How do I get the MIN() of a datetime column and a literal datetime in SQL Alchemy v0.6.4? I want to clamp a datetime result to a specific range.

I’ve been using sqlalchemy.func.min(column, literal_datetime). This seems to work fine in SQLite but not at all with MySQL, which no doubt means I’m going about this wrong. The error from MySQL is:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '2011-06-14 12:30:00') AS min_1 \nFROM example' at line 1") 'SELECT min(example.my_datetime, %s) AS min_1 \nFROM example' (datetime.datetime(2011, 6, 14, 12, 30),)
  • How can I clamp datetime results in a portable way?
  • (Do I really have to do it in my application?)

Here’s what I’ve been using to explore the problem – works fine as presented here (using a memory-based SQLite DB), doesn’t work with MySQL:

#!/usr/bin/env python

import random
from datetime import datetime

import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.ext.declarative import declarative_base

orm_base = declarative_base()

class MyClass( orm_base ):
    __tablename__ = "example"
    pri_key  = sa.Column(sa.Integer(), primary_key=True)
    my_datetime  = sa.Column(sa.DateTime(), index=True)

engine = sa.create_engine("sqlite:///:memory:", echo=True)

Session = orm.sessionmaker( bind=engine )

orm_base.metadata.bind = engine
orm_base.metadata.create_all()

# Create test-data

session = Session()

random.seed(1234567980)
for i in range(100):
    month = random.randint(1, 12)
    day = random.randint(1, 28)
    hour = random.randint(0, 23)
    minute = random.randint(0, 59)

    my_instance = MyClass()
    my_instance.my_datetime = datetime(2011, month, day, hour, minute)

    session.add( my_instance )

session.commit()
session.close()

# Problem starts here

session = Session()
literal_datetime = datetime(2011, 06, 14, 12, 30)
print session.query( sa.func.min(MyClass.my_datetime) ).one()  # OK
print session.query( sa.func.min(MyClass.my_datetime, literal_datetime) ).all()  # Error in MySQL

I’m not an SQL expert but database portability is important to me, so any tips on how to avoid pitfalls like this in the future are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:19:00+00:00Added an answer on May 24, 2026 at 8:19 pm

    Use sqlalchemy.sql.expression.case expression, which is supported on both mySql and SQLite.

    The min/max functions of SQLite support non-aggregate operation on multiple values. This is not supported on mySql though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get max and min values with other column's values in ms
For the following datatable column, what is the fastest way to get the min
I need to get the minimum DateTime value of a column in a DataTable.
Im using SQLite and trying to get my date into a blackberry datetime field.
Why I can't get the min-height of an object when its parent is set
For example, I am trying to get a min date, a max date, and
Any web service to get the monthly min/max temperatures for cities over a period
Does anybody knows how can I get the max and min value of the
How to get (correctly and/or readable value of) std::numeric_limits<char>::min() ? cout << std::numeric_limits<char>::min() <<
SELECT Id, Value, Sender, Recipient, MIN(DateTime), TypeOf, VideoId, Likes, Comments FROM xx.dbo.StreamView WHERE StreamView.TypeOf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.