mysql query with 'like' doesn't work with varchar and space - mysql

I have a Db with one table with 3 fields like the following:
user_id TimeStamp Azioni
where the 'timestamp' field is a varchar(25) like this: 2012/09/19 16:34:01.95
It is a varchar and not a timestamp value because i need it to be in the shown format.
And i cannot change its type even if i wanted to.
Now, I'm trying to get all db entries with the same date. For example, when Timestamp contains 2012/09/19
I tied several queries:
Query 0:
SELECT Azioni.Action
FROM Azioni
WHERE TimeStamp LIKE '2012/09/19%'
Query 1:
SELECT `Azioni`.*
FROM Azioni
Where `TimeStamp` LIKE '{2012/09/19}%'
Query 2:
SELECT `Azioni` . *
FROM Azioni
WHERE LOCATE( '2008/09/19', `TimeStamp` ) >0
Query 3:
SELECT `Azioni` . *
FROM Azioni
WHERE INSTR( `TimeStamp` , '2012/09/19' ) >0
Query 4:
SELECT * FROM `Azioni`
WHERE `TimeStamp` like '2012|/09|/19%' escape '|'
and I always get: MySQL returned an empty result set (i.e. zero rows).
But I am sure there are rows containing the said timestamp. What am i doing wrong? Does the 'space' between date and time create a problem? If so how can i solve it? Do you have any suggestion?
EDIT:
Aa suggested, from
SELECT TIMESTAMP, HEX( TIMESTAMP )
FROM Azioni
i get the following
2009-06-06 09:28:00.0000 323030392D30362D30362030393A32383A30302E30303030
2009-06-06 09:29:00.0000 323030392D30362D30362030393A32393A30302E30303030
2009-06-06 09:30:51.0000 323030392D30362D30362030393A33303A35312E30303030
2009-06-06 14:25:00.0000 323030392D30362D30362031343A32353A30302E30303030
2009-06-06 14:26:00.0000 323030392D30362D30362031343A32363A30302E30303030
EDIT 2:
ehm yeah, i was typing the date wrong in the query. Sigh, i'm stupid. Sorry for wasting your time guys.

How about this:
where timestamp like '2012/09/19%'
And, if you are going to call the field timestamp you should store it as a date/datetime/timestamp. Call it something else if it is going to be stored as a string. Timestamp is actually the name of a type in MySQL, so having that in a column name with a different type is quite misleading.
EDIT:
Have you tried:
where left(timestamp, 10) = '2012/09/19'
It sounds like there are string characters in the field, which are preventing reasonable code from working.

SELECT * FROM Azioni
WHERE `TimeStamp' LIKE '2012/09/19%'

Related

Mysql subquery or something better

I am somewhat new to mysql and I am having an issue on how I should best write the following query. Say I have a table that has a datetime column as well as a few others I want to search on. Since this is just one table, I don't think a join statement would be appropriate here (but I may be wrong since I have not done much in the way of join statements) and I think a subquery is what I need here. So my initial query is to search the table based on a search string the user entered and then I want to limit that on a datetime (start date and end date) also specified by the user in an HTML form.
Table Schema
id, datetime, host, level, message
I want to select any rows that contain $searchstring first so something like ...
SELECT * FROM $table WHERE (level LIKE '%$searchstring%') OR (message LIKE '%$searchstring%') LIMIT $offset,$limit
If I want to limit the above results also by the datetime column, the query would look something like this ...
SELECT * FROM $table WHERE (datetime >='$startdate') AND (datetime < '$enddate')
How can I best merge these queries into one so I can first get any rows that match the search query and then further limit the rows by the start and end datetime?
TIA
You can achieve that by using a single where condition.
In your case:
SELECT * FROM $table WHERE ((level LIKE '%$searchstring%') OR (message LIKE '%$searchstring%')) AND (datetime >='$startdate') AND (datetime < '$enddate') LIMIT $offset,$limit
You don't have to use a JOIN but only add a condition
SELECT *
FROM $table
WHERE (level LIKE '%$searchstring%' OR message LIKE '%$searchstring%')
AND
datetime >='$startdate'
AND datetime < '$enddate'
LIMIT $offset,$limit

Min function returning max valu and Max function returning min value

I have a table "abcd" with column name as "avg" and values "100" and "83".
When I try
select max(avg) from abcd -- Returns 83
select min(avg) from abcd -- Returns 100
seems quite weird to me. I have never imagined that I will be posting something like this in SO. It might be a minor thing to look but it's kicking my day out to solve it.
Am using MySQL and phpMyAdmin
Sounds like a string. A simple solution is to turn it to a number:
select max(avg + 0)
This uses "silent conversion", so it will not raise an error if the value is not numeric.
A better solution might be to turn it into an actual number in the data:
alter table t modify column avg int;
(The values appear to be integers.)
change the datatype of your column avg
if you using varchar its give wrong output on number function
using below query alter the column
ALTER TABLE `abcd` CHANGE `avg` `avg` INT(11) NOT NULL;
OR try this
SELECT max( cast(avg as unsigned) ) as avg FROM `abcd`
SELECT min( cast(avg as unsigned) ) as avg FROM `abcd`

What causes a Date to be <null> while specifically selecting the NOT NULL fields?

In one of my MYSQL databases, I have done the following query :
SELECT * FROM mytable WHERE date_column < 1971-01-01 AND date_column IS NOT NULL;
This returns about 3000 items where the date is shown as NULL in the mysql command line prompt, and in the IntelliJ IDEA mysql database editor.
The column has a DATE type.
The database is a copy of a prod database environment that has been running for a couple years.
How can a record end up in that state ? What is that state ? How can I fix it properly ?
PS: The < 1971/01/01 clause was just to filter the correct dates out of the query. If I just query on the IS NOT NULL clause, I still get these weird dates of course.
I am surprised this works. First, date constants should be surrounded by single quotes:
SELECT *
FROM mytable
WHERE date_column < '1971-01-01' AND date_column IS NOT NULL;
Second, this cannot return a NULL value for date_column. This suggests one of two things:
date_col is actually another type, such as varchar and you are seeing 'NULL' the string, not NULL the value.
another column is NULL.

SQL select order by date in varchar

I have my date in database in varchar column and i can't change it. However i want to sort things from newest to latest. My date in database looks like:
2014-09-22 10:28:28
So what i try is something like:
$sql = "SELECT * FROM axnmrs_cases WHERE vin = :vin ORDER BY STR_TO_DATE(date_created,'%b-%e-%Y') ASC LIMIT 30";
But unfortunately this not change anything for me , even if i change ASC to DESC , nothing changeing in result
and also something like:
$sql = "SELECT * FROM axnmrs_cases WHERE vin = :vin ORDER BY CONVERT(date_created, date, 103)";
This throw syntax SQL error and I have no idea why.
Is here anybody who can show me the right way?
Date stored in varchar is not a real date and hence the order by also does not give you what you want. The best approach would be store date always in mysql native data types. However in your case you can use str_to_date() function to convert the varchar dates to real date and then use it for sort something as
order by str_to_date(date_created,'%Y-%m-%d %H:%i:%s');
$sql = "SELECT * FROM `axnmrs_cases` WHERE `vin` = ':vin' ORDER BY `date_created` ASC LIMIT 30";
Already tried something like this?
You are using the wrong format in your STR_TO_DATE function, if the date is in the format:
2014-09-22 10:28:28
Then you need to use
STR_TO_DATE(date_created, '%Y-%m-%d %H:%i:%s')
i.e. the format you give should match the format your varchar is in.
Example on SQL Fiddle
In your case you are using '%b-%e-%Y', so you are looking for a date like Jan-1-2014, for a full list of the specifiers in the format defintion see the My SQL Docs for DATE_FORMAT
Also, CONVERT(date_created, date, 103) does not work because it is SQL Server Syntax.
Finally, I would really, really try and change the column data type. Storing dates as a varchar is never a good idea. Anything else is just a workaround, not a solution.

Filter dates stored as varchar in mysql

I am working with a MySQL database where dates are stored as varchar like this:
'2013-01-31' in column cl_223
I need to select only records from 2013 so I tried:
SELECT ..
FROM ....
Where cl_223 Like '2013'
But that does not seem to work.
Thanks for all help!
You must add % as a wildcard :
SELECT ..
FROM ....
WHERE cl_223 LIKE '2013%'
Storing a datettime value in a varchar column complicates some functionality on date time operations. But of course you can select your values writing such a query as follow
SELECT * FROM table_name WHERE cl_223 LIKE '2013%'
But if you don't have any performance issue you can convert the varchar column to a datetime value and write stronger typed query like this:
SELECT * FROM table_name WHERE STR_TO_DATE(cl_223,'%Y-%m-%d2') BETWEEN '2013-01-01' AND '2013-12-31'
But if you need a date time value as a date time in your process you'd better store it in a datetime column instead of a varchar column.
The query should be
SELECT ..
FROM ....
Where cl_223 Like '2013%'
However, the better solution would be to store the dates as DATE data types. If the dates in that column are always used in the format they're in now, the change would be backwards compatible. It would also allow for easier processing of the date values.