I have a column which holds date values as varchar as in the format of
date_n date_s
2016-11-10::12:36:05 2016-NOV-10::12:36:05
2016-11-10 2016-NOV-10
Now all I need is to get date from both of columns but I am only able to get from column date_n along with timestamp and nothing from date_s The query I tried as
SELECT * FROM table where CAST(date_n AS DATE)=CURDATE();
which returns both element as result but when I change date_n to date_s returns empty and all I need output as date only no time stamp
So is there any way to sort this out
Hi try this logic
WHERE TRIM(UPPER(SUBSTRING_INDEX(date_s,':',1))) =
TRIM(CONCAT(YEAR(NOW()),'
-',UPPER(SUBSTRING((MONTHNAME(STR_TO_DATE(MONTH(NOW()),'%m'))),1,3)),'-',DAY(NOW())))
WHERE date_s LIKE CONCAT(YEAR(NOW()),'-',UPPER(SUBSTRING(MONTHNAME(NOW()), 1, 3)),SUBSTRING(CURRENT_DATE(), 8), '%');
Related
I am trying to analyze order_Date column and column have multiple date format i want to convert all those date in same format which wull make be easier to analyze the order_date.
I am trying to analyze the order_date however this column have multiple date format 2019/07/15 and 1/13/2014
Howeever, while converting different format date with one format yyyy/mm/dd with query.
select date_format(order_date, '%y/%m/%d'),orderid from superstore;
it shows null values like this.
i have tried to use `CAST as well but it shows every single value as null.
select case when order_date like '%Y' then date_format(order_date, '%Y/%m/%d') else null end as newdate from superstore;
date_format funtion is used to format a date datatype you should use https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date any null values returned by str_to_date either failed or started as null. You will need to examine these and adjust the str_to_date parameters appropriately. There is a catch though is 20/2/20 y/m/d or d/m/y (for example) and how can you differentiate month and day where both are <=12?
For example
drop table if exists t;
create table t
(dt varchar(10));
insert into t values
('1/1/2020'),('2020/1/12'),('12/12/12'),(null),('13-14-15');
select dt,
case when length(substring_index(dt,'/',-1)) = 4 then str_to_date(dt,'%d/%m/%Y')
when length(substring_index(dt,'/',1)) = 4 then str_to_date(dt,'%Y/%m/%d')
when length(substring_index(dt,'/',1)) = 2 then str_to_date(dt,'%y/%m/%d')
else str_to_date(dt,'%y/%m/%d')
end dateformatted
from t;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=553219f33ad9e9a4404fc4c0cb6571c9
note in no case can I identify month and day and sometimes year..
i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?
Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.
Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal
WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.
So I have a table called dash with two columns: value and date.
I have a timestamp variable called localtime.
Both localtime and date are in yyyy-mm-dd hh-mm format.
I need to find the closest timestamp on dash, return the value.
Right now what I have doesn't work.
def convValueChecking(cursor, localtime):
cursor.execute("SELECT Value, MIN(TIMESTAMPDIFF(MINUTE, DATE, %s)) FROM dash", (localtime))
value = cursor.fetchall()
Update:
This one seems to work, the order_date > locatime is very important, otherwise it looks for the smallest negative number
cursor.execute(
"SELECT order_value, order_date FROM dashboard \
WHERE order_date > %s\
ORDER BY ABS(TIMESTAMPDIFF(MINUTE, %s, order_date))\
LIMIT 1", (localtime, localtime,))
Try replacing the SQL string with:
SELECT
value
FROM
dash
WHERE
TIMEDIFF(Date, %s) IN
(
SELECT
MIN(TIMEDIFF(Date, %s))
FROM
dash
)
MySQL syntax means you need to do SELECT then FROM and then declare the WHERE part.
TIMEDIFF works out the difference between two timestamps. TIMESTAMPDIFF works out the difference between two parts of a timestamp (eg between the months, days, seconds etc.). If you want the closest date you should use TIMEDIFF to work out the smallest difference overall.
NB. The words Value and Date also have special properties in MySQL, so you may want to change your column name.
SQLFiddle.
I have MySQL table
id product p_image
1 G images\20131030164545.jpg
2 S images\20131230164545.jpg
3 V images\20140110164545.jpg
4 R images\20140320164545.jpg
5 K images\20140526164545.jpg
6 L images\20150110164545.jpg
7 SK images\20150120164545.jpg
Here I need to extract products from above table where p_image timestamp between two dates (for example I need to extract from 2013/12/01 to 2014/07/30 dates)
In this query I need to extract timestamp from this string 'images\20140526164545.jpg' and convert this to date format and select values between two dates.
Assuming the format of the string is fixed (which it looks to be) you can use thesubstrfunction to extract the timestamp and then cast it to a date and filter by it. Something like this should work:
select * from table1
where cast(substr(p_image FROM 7 FOR 14) as date)
between '2013/12/01' and '2014/07/30'
Sample SQL Fiddle
There might be more efficient ways to do this, but this should give you an idea to start with.
Edit: if the string can vary the something like left(right(p_image, 18), 14) should work.
The dates in p_image are in YYYYMMDD format, so you can compare them as strings. That is, there is no reason to convert the strings to a date data type.
Hence you can just do:
where substr(p_image, 8, 8) between '20131201' and '20140730'
If the position of the date is not fixed but always after the /, you can do:
where left(substring_index(p_image, '/', -1), 8) between '20131201' and '20140730'
Try this it will working :
select * from `datetest` where substr(p_image, 8, 8) between '20131201' and '20140730'
Screenshot of Phpmyadmin :
I have an issue in regards to trying to run a sql statement that returns the values of a column called month(of which I have defined as a varchar type, but only has integer values 1-12) as the associated month name. So, for example, the query would return a value of 1 as january. The issue I have is I am trying to use date_format
select date_format(month,'%M')from db.table name
but the values return as null. I was informed that the month values have to be a 'date' type in order for date_format to work. However, the values in this column 'month' are simply integers. So I run into the issue of not being able to assign the date type to the month columns because they're just integers and not correct format for dates? How could I take these single integers and return the month then?
Syntax
DATE_FORMAT(date,format)
Requires date as first param
Check out MySQL date function here:
http://www.w3schools.com/sql/sql_dates.asp
For this you can use this,
SELECT col as MonthNumber,
MONTHNAME(STR_TO_DATE(col, '%m')) as MonthName
FROM table_name
WHERE col <= 12