How to get Time from Mysql Date Format DD/MM/YYYY? - mysql

I want to get time from mysql dd/mm/YYYY H:M:S format.
I have tried,
SUBSTRING_INDEX(field, 'delimiter', index)
but am looking for a better solution.
have tried, DATE_FORMAT(field, "%H:%i:%s") but it returns NULL because my date format was not native (YYYY-mm-dd)
it was 02/05/2019 19:38:27
How to get time from this above format in a better way?
NOTE: I am storing date like above.. this fetching form SQL Server

I guess you can first use STR_TO_DATE followed by CAST(... AS time). Casting instead of formatting allows you to use the result in date/time calculations.
SELECT CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
Ideally you should teach SQL Server to export dates in yyyy-MM-dd hh:mm:ss format.

This is how i Resolved,
TIME(STR_TO_DATE(d.in_punch, "%d/%m/%Y %H:%i:%s"))
also as per #Salman A
CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
this also worked.

Related

Convert timestamp (int) to smalldatetime (t-sql)

I have a MySQL-Database with several rows. In that environment, I stored the current time as a Timestamp (int).
Now I need to migrate my data from a MySQL to a T-SQL-Database. I'm running SQL-Server 2008.
I checked several approaches, but couldn't come up with a way which transforms my int into a smalldatetime format.
Is there a build-in function for this? Is this even doable alone in a statement? I really don't want to write a PHP-snippet, which converts the timestamp to the desired format.
Thanks in advance
As per the documentation, smalldatetime uses the following format:
DECLARE #smalldatetime smalldatetime = '1955-12-13 12:43:10';
So, we need to convert the MySQL timestamp into date and format it in the above format to get smalldatetime. This can be done by using FROM_UNIXTIME and DATE_FORMAT functions of MySQL, e.g.:
DATE_FORMAT(FROM_UNIXTIME(timestamp_column), '%e %b %Y') AS 'smalldatetime';
Here's the SQL Fiddle.

MySQL timestamp format and datediff

Hi I'm writing queries for MySQL, and now my database has a column containing the timestamp in this format: 7/14/2015 7:57:49 AM, but I need to use the DATEDIFF function, so how can I convert the timestamp into the format like: 2015-7-14 (or 2015-07-14, I'm not sure which one is correct; just the date)?
This should convert your string to just the date in a date format, then you can use DATEDIFF on the date fields in question:
SELECT STR_TO_DATE(LEFT(t,LOCATE(' ',t) - 1), '%m/%d/%Y') FROM my_table;
The LEFT function will take the substring to the left of the space, which is just your date, then STR_TO_DATE will convert that substring to a date the system can use.
(Not knowing your field and table names, I used t and my_table.)
You don't need to. The way MySQL displays timestamps has nothing to do with the way they're stored internally; as long as it's TYPE TIMESTAMP or some compatible type, the DATEDIFF() function will know what to do with it.
TIMESTAMPs are actually stored as a really huge integer representing (I think) milliseconds from Midnight UTC, January 1st, 1970. The display format is determined by a system global variable, and has nothing to do with the actual value.
Converting from a string to a DATETIME or TIMESTAMP is actually also fairly straightforward using the STR_TO_DATE() function; in your case the format string would be something like
STR_TO_DATE('%c/%e/%Y %l:%i:%s %p', datecol)
although you might have to experiment a bit to make it work reliably.

MySQL extract HOUR from DATETIME and express in AM/PM

sql newbie here. I googled around for a quick solution to this, but failed to find it.
I have a field that is a DATETIME, for e.g., 2014/06/19 15:07:37. I just need to extract the HOUR and express it in AM/PM, i.e., the above result should read 3pm.
I have tried DATE_FORMAT(HOUR(date_field), %r) but this doesn't work.
date_format() expects a date. hour returns an int. You just need
DATE_FORMAT(date_field, '%l%p')
note that %r is a full time, hh:mm:ss, not just the hour.
You need TIME_FORMAT function:
SELECT TIME_FORMAT(NOW(), "%h%p");

mysql date format dd/mm/yyyy?

Does anyone know how to insert into column(of date type) date in the format of dd-mm-yyyy?
I'm using calendar, and it selects days like dd-mm-yyyy, but it can't be inserted because of the format that uses mysql by default (yyyy-mm-dd).
The problem is, that I need to compare dates that has been inserted by the calendar and the dates that has been automatically inserted with current_date function. Basicly I have column DoR (date_of_registration) and Status. I've made DoR of date type, and Status of varchar(). In the Status I use Calendar to insert the date. Thus, I've compared it anyway but it gives strange results. I guess it is not comparing correctly.
Any ideas?
Thanks
Convert the output from calendar into date format like this
$cal_date=17-08-2012;
$date=date('Y-m-d',strtotime($cal_date));
echo $date; //gives date as 2012-08-17
Now you can insert this date into mysql and do comparisons
select str_to_date('31-12-2012', '%d-%m-%Y')
See STR_TO_DATE
All you need are these two functions:
STR_TO_DATE()
and
DATE_FORMAT()
Please, always store dates as a DATE type in MySQL. If you need to select it in another form, use the correct string function DATE_FORMAT!
You don't need to insert database as dd-mm-yyyy. You have to user date format. If you are writing code in php you can parse date what every type you want
Select datetime for database. And insert date data date("Y-m-d H:i:s"); when you want to echo date different formats like
echo date('d-m-Y', strtotime('2012-08-17 19:00:00'));
There is also another way to store the date like, if you are choose that
`<input type="date" name="bdate" />
`
then it directly store the date in to database in yyyy-MM-DD Format.
Here my text field format is dd-mm-yyyy.
Here you don't have to convert the string using strtotime()

Converting date format using str_to_date

In one of my Mysql database table the dates are stored in the format 31-Jan-05.
I'm trying to convert this format to 2005-01-31 before inserting them in other tables.
I've tried in this way str_to_date(exam_date, '%d%M%Y'), but i encounter the following error
Incorrect datetime value: '31-Jan-05' for function str_to_time
Can't i change the date format from 31-Jan-05 to 2005-01-31 using str_to_date?
Thanks in advance.
Yes. But you have two problems.
The second parameter is the current date format. (i.e. of the string)
You need to have the proper format (i.e. %b instead of %M).
Read the docs the for str_to_date().
str_to_date(exam_date, '%d-%b-%y')
Note: If you don't have a zero padded day, then you need to use %e instead of %d.