Yves
Yves

Reputation: 107

SQL: How To Select Earliest Date

I have a date column & I am simply trying to know the earliest date. I use the command:

select Min(Install_date) From PocketGemsSchema.pocketgemstable2;

This returns 1-Dec-17

But the minimum date from my sample data is actually 1-Nov-17.

Can anyone help please?

Upvotes: 0

Views: 3258

Answers (3)

Keith John Hutchison
Keith John Hutchison

Reputation: 5277

I would change the column to a date or a datetime type and sort out any bugs that arise.

Upvotes: 0

Yogesh Jain
Yogesh Jain

Reputation: 11

Try this: If your Install_date contain datatype varchar than

SELECT MIN(CAST(Install_date AS DATE)) 
FROM PocketGemsSchema.pocketgemstable2

SELECT FORMAT(MIN(CAST(Install_dateAS DATE)), 'dd-MMM-yy ') 
FROM PocketGemsSchema.pocketgemstable2

If your Install_date contain datatype date or datetime than your query will work

Upvotes: 1

sumit
sumit

Reputation: 15464

I think its the data type issue , you can try two approach

  1. convert the field to datatime and your query should work
  2. cast it on the run time like below

Mysql

SELECT Min(Str_to_date(Install_date, '%m/%d/%Y')) FROM pocketgemsschema.pocketgemstable2;

SQL server

SELECT Min(Cast(Install_date as datetime)) FROM pocketgemsschema.pocketgemstable2;

Upvotes: 0

Related Questions