Mysql strip time component from datetime - sql

I need to do a date comparison in Mysql without taking into account the time component i.e. i need to convert '2008-11-05 14:30:00' to '2008-11-05'
Currently i am doing this:
SELECT from_days(to_days(my_date))
Is there a proper way of doing this?

Yes, use the date function:
SELECT date(my_date)

select date(somedate) is the most common.
If you need to accommodate other formats, you can use:
SELECT DATE_FORMAT(your_date, '%Y-%m-%d');

In PostgreSQL you use the TRUNC() function, but I'm not seeing it for MySQL. From my brief Googling, it looks like you'll need to cast your DATETIME value to be just DATE.
date_col = CAST(NOW() AS DATE)
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html

Just a simple way of doing it date("d F Y",strtotime($row['date'])) where $row['date'] comes from your query

You could use ToShortDateString();

Related

How to change the format of a date field in SQL?

I have a date field in one of my tables that's formatted like this: 20220212.
I want to CAST it like this: 2022/02/12 but a simple CAST(DATE_FIELD AS DATE) doesn't seem to work. ANyone know how I can achieve this with SQL in Snowflake?
Thanks!
Please try to_date()
to_date(DATE_FIELD, 'YYYY/MM/DD')
Documentation
Select to_date('20220212','YYYYMMDD')::date as dt, to_char(dt,'YYYY/MM/DD') ;

SQlite compare dates without timestamp

In SQLite, how to compare date without passing timestamp?
The date format is 2018-03-18 08:24:46.101655+00 and I want to compare against only date part as 2018-03-18.
I have tried as where mydate='2018-03-18' but that didn't return any records.
Similarly, tried Date(mydate)='2018-03-18' but that didn't help either.
How can I compare dates ignoring the timestamp part?
select * from mytable
where strftime('%Y-%m-%d', mydate) = '2018-03-18'
This is not one of the supported date formats.
To extract the date part from the string, use substr():
... WHERE substr(mydate, 1, 10) = '2018-03-18'
It might be a better idea to store dates in a correct format in the database to begin with.
It is looking that there is problem with date format.
Sqlite doesn't understand data like '+00' in date.
So date() and strftime() will not work here if data type is 'timestamp with time zone'.
Try by using like clause.
Try using strftime
SELECT strftime('%Y %m %d', 'columnName');
you can find it here strftime.php

SQL: Casting text to date

I have a TEXT column of dates and need to convert them to dates, but the two methods I'm using are not working correctly. See below.
SELECT CAST("12/01/2009" as date);
12
This only returns the first digit before stoping at the '/'.
SELECT DATE("12/01/2009");
Returns nothing
I also tried CONVERT, but I'm using SQLite and it doesn't appear to support it as I'm getting a syntax error. Any suggestions on how to solve this?
Try using STR_TO_DATE function
SELECT STR_TO_DATE('12/01/2009','%m/%d/%Y');
SQL FIDDLE DEMO
SqLite doesn't have date type. You need to do string manipulation do achieve this.
SELECT CAST('2009-01-12' AS DATE);
Use it.
It returns 2014-02-28

Convert to Time Format Only

I hv the following table which I hv converted:-
by using the following SQL command:-
How can I convert it to this format:-
I believe it has something to do with the "convert(datetime, '7:30:00',120)".
Thanks
Use CONVERT(time,'07:30:00') instead - but you also need to come up with a more appropriate default value for the ELSE clause of your CASE expression, maybe '00:00:00'? 0 isn't a time.
Try
SELECT CONVERT(VARCHAR(8),GETDATE(),108) as TimeIn

How do I convert this oracle date

I am trying to convert from one date format to another. I am not sure how to write the functions.
My source date looks like 01/15/2009 01:23:15
The format I need is 01152009.
Thanks
Try this.
TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY HH:MI:SS'),'MMDDYYY')
More info here,
http://psoug.org/reference/date_func.html
Does this work for you? It assumes the date is in date format but will work with timestamp
select to_char(YourDateField,'DDMMYYYY') from dual;
You can always convert it back to a date using the TO_DATE function if you need that format.
select TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY MI:HH:SS'),'MMDDYYYY') from dual
if your field is already of data type date then you should only do:
select TO_CHAR(<fieldname>,'ddmmyyyy') ...