Mysql data and convert - mysql

I have table that contain a string field with data.
The date is in this format: dd/mm/yyyy
I need to show the max date.
I try some thing but without success
Select max(to_date) from payment
Thanks

Use the STR_TO_DATE() function:
SELECT MAX(STR_TO_DATE(to_date, '%d/%m%Y')) FROM payment;

you must convert your string to a date,
because now you are doing the max over a string not a date
SELECT MAX(STR_TO_DATE(to_date, '%d/%m/%Y'))
FROM payment

Related

How to parse time of ISO timestamp in mysql?

How can a ISO datetime String timestamp be correctly parsed to time type column in mysql? I noticed the following:
select CAST('2013-09-05T10:10:02' as time) from mytable limit 1
Result incorrect:
00:20:13
select CAST(CAST('2013-09-05T10:10:02' as datetime) as time) from mytable limit 1
Result correct:
10:10:02
Why do I have to make a double CAST here to get the correct time? And more important: how is time parsing done property?
Because you have a string which you need to first cast it into date format.
If you cast it in time like below:
select CAST('2014-09-05T10:10:02' as time)
00:20:14
select CAST('2015-09-05T10:10:02' as time)
00:20:15
select CAST('2013' as time) --below casting string as time
00:20:13
If you monitor closely its treating it as string and getting year as time.
So you need to cast it datetime first then time.
select CAST(CAST('2013-09-05T10:10:02' as datetime) as time)
Basically when you try to convert something to a time that looks like an integer MySQL treats that as something in the form HHMMSS, so your 2013-09-05T10:10:02 becomes 00:20:13. To convert properly, the value needs to be a MySQL datetime, which you can do via CAST or you can use STR_TO_DATE to convert your date string to a MySQL date, then TIME to extract the time part of it:
SELECT TIME(STR_TO_DATE('2013-09-05T10:10:02', '%Y-%m-%dT%H:%i:%s'))
Output:
10:10:02
Demo on dbfiddle

SQL STR_TO_DATE

I'm having trouble with the following code:
INSERT into `fun` ( funner)
SELECT YEAR(STR_TO_DATE(SUBSTRING(time,1,4), '%Y'))
FROM `orig`
returning the warning:
Incorrect datetime value: '1880' for function str_to_date
time is a varchar column in the table orig with the format yyyy/mm.
I want to extract the year section from this varchar and translate it into a year datatype using STR_TO_DATE
I would recommend using one of the usual date and time MySQL datatypes, instead of the rarely used YEAR datatype : DATE, DATETIME and TIMESTAMP.
If you want to turn your string to a date datatype, then :
STR_TO_DATE(my_column, '%Y/%m')
You can use the YEAR() function on this date, and it will return an integer value :
YEAR(STR_TO_DATE(my_column, '%Y/%m'))
Finally : if all you want is get the year from a date stored as string, then you can directly extract it the string using SUBSTR :
SUBSTR(my_column, 1, 4)
This returns a string (not an integer), that MySQL will implictely convert to a number when used in numeric context.
You can convert SUBSTRING(time,1,4) to integer:
SELECT CONVERT(SUBSTRING(time,1,4),UNSIGNED INTEGER) FROM orig
there is no need to convert it first to a date and then convert to integer.
I guess you need to return an integer value since you use the YEAR() function.
If not a simple SELECT SUBSTRING(time,1,4) FROM orig will do.
What does time look like? If year has the data type year, then you can insert a date into the column:
INSERT into `clean` (year)
SELECT DATE(CONCAT(SUBSTRING(time, 1, 4), '-01-01'))
FROM `orig` ;
I am not a fan of the year data type. You should just put the entire date into the column.

SELECT range of date in Text field

Date
9/25/2015
9/26/2015
9/27/2015
9/28/2015
9/29/2015
9/30/2015
10/1/2015
10/2/2015
10/3/2015
10/4/2015
10/5/2015
Can anyone help me in MySQL. I would like to select only date from 9/28/2015 to 10/4/2015.
Please take note, this date is in Text field.
Thank you.
you can use STR_TO_DATE(yourdatefield, '%m/%d/%Y') to convert text to date and you can later use between clause to restrict output data.
Convert first your dates using CONVERT, then use BETWEEN in your WHERE clause.
Try this..
SELECT * FROM TableName
WHERE Date BETWEEN CONVERT(DATE,'9/28/2015') AND CONVERT(DATE,'10/4/2015')
You can try like this:
WHERE `Date` BETWEEN CAST('9/28/2015' AS DATE) AND CAST('10/4/2015' AS DATE)
or
WHERE STR_TO_DATE(`Date`, '%m/%d/%Y') BETWEEN STR_TO_DATE('9/28/2015', '%m/%d/%Y') AND STR_TO_DATE('10/4/2015', '%m/%d/%Y')
DEMO
Also try to avoid storing dates as Text. Instead use Date datatype to store dates.
Try this , in where clause used function str_to_date
SELECT `dateVal`,`id`
FROM `datecheck`
WHERE STR_TO_DATE(`dateVal`,'%m/%d/%Y') between STR_TO_DATE('9/28/2015',
'%m/%d/%Y') AND STR_TO_DATE('10/4/2015', '%m/%d/%Y')
I had the same type of issue but in my case the date stored also contains the time (also in a text field, for instance 2017-09-11 05:07:58 PM). This is for a wordpress site but I want to query the database directly, the date is in a meta_value.
To make this work I ended using a subbstring of the date, i am posting this in case it helps someone:
SELECT ID, display_name, user_email, meta_value FROM bfge_users, bfge_usermeta WHERE (bfge_users.ID = bfge_usermeta.user_id) AND meta_key ='user_login' AND CAST(SUBSTR(meta_value,1,POSITION(' ' IN meta_value)) AS DATE) between '2017-09-11' AND '2017-09-13';
use between, you can read more here:
Select data from date range between two dates
and question is dublicate

SQL Where Date is greater than X

I am trying to run the query;
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail, PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE (PO_PurchaseOrderDetail.PurchaseOrderNo=PO_PurchaseOrderHeader.PurchaseOrderNo)
AND ***(PO_PurchaseOrderHeader.PurchaseOrderDate > '2013-12-31')***
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo, PO_PurchaseOrderDetail.PurchaseOrderNo
My problem is, that the date is stored in the YYYY-MM-DD format. I need to only show data after a certain date. So, because 2013-12-31 is a string rather than a number, the operator > doesn't work. Any ideas?
Try to CAST string as DATE in following:
P.S. if PurchaseOrderDate is not DATE datatype you could also CAST It: CAST(PO_PurchaseOrderHeader.PurchaseOrderDate AS DATE)
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail,
PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE PO_PurchaseOrderDetail.PurchaseOrderNo = PO_PurchaseOrderHeader.PurchaseOrderNo AND
PO_PurchaseOrderHeader.PurchaseOrderDate > CAST('2013-12-31' AS DATE)
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo

Change datatime to date dd/mm/yy

Im trying to query some data and one of them is a datetime format. I want that is shows dd/mm/yy with no time on it directly form the select. Is this possible?
My query is and the join_date is datetime that i need to be changed to short date:
$query = mysql_query("SELECT id,username,join_date,is_active FROM members",$con)
or trigger_error(mysql_error());
This query goes directy in a Json output array. So i want to convert it directy form the query.
Use the MySQL DATE_FORMAT function for this:
SELECT id, username, DATE_FORMAT(join_date, '%d/%m/%y') AS join_formatted, is_active
FROM members
In this example, the column name for the formatted date will be join_formatted, and its type will be VARCHAR.
The format string returns the date as dd/mm/yy as requested, but I'm personally more comfortable when the date includes the full century. To get the full century, use uppercase Y in the format string: %d/%m/%Y.
try this :
"SELECT id, username, DATE_FORMAT(join_date,'%d/%m/%y'), is_active FROM members"
Use DATE_FORMAT:
SELECT id, username, DATE_FORMAT(join_date, "%d/%m/%y") AS date FROM members;
%Y Year, numeric, four digits
%y Year, numeric (two digits)
For details about date format link