why this MySQL SELECT doesn't include the right dates? - mysql

The main problem is, that I have stored in database datetime , not the date (what I need). Ok never mind.
I have thousands of reports stored each day.
I need to LEFT by 10 my datetime_view (to cut the time) and everything's fine. Except this. I'm trying to figure out why do I have to put in the condition + one day from the future? Otherwise it won't search what I want.
SELECT
LEFT(datetime_view,10),
count(type)
FROM reports
WHERE
type IN (1,2,5)
AND
datetime_view>='2012-10-28'
AND
datetime_view<='2012-11-04'
group by LEFT(datetime_view,10);
You can see I must search from the future. Why??
It gives me an output from 28.10 to 3.11 ....

don't use string operations on date/time values. MySQL has a huge set of functions for date/time manipulation. Try
GROUP BY DATE(datetime_view)
instead, which will extract only the date portion of the datetime field. Your string operation is not y10k compliant. Using the date() function is.
As for your plus one day, consider how the comparisons are done: A plain date value, when used in date/time comparisons, has an implicit 00:00:00 time value attached to it, e.g. all dates have a time of "midnight".

i think it's better to use DATE(datetime_view) to cut the time instead of LEFT(datetime_view,10), also on the where condition:
DATE(datetime_view) <= '2012-11-03'

Related

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.

SQL query to get records from same day

I wish to find all records of the current day.
I have a field Date of type DATE.
If I use
WHERE `Date` = '2011-04-07'
it works
but if I use:
WHERE `Date`='CURDATE()'
or
WHERE `Date`='NOW()'
it does not return any results (when there actually are some).
How do I get the current date in the right format to use it in my SQL query?
I am using MySQL
And the date was originally entered in the database using NOW().
Use
WHERE `Date`=CURDATE()
The quotes (') are used to wrap up a string (text).
Edit: I can see now that you say the value was stored with NOW() : it probably includes a time element too. Will update answer imminently..
This will compare the date part of the Date field to today's date:
WHERE DATE(`Date`)=CURDATE()

How to handle partial zero dates (ie. 2010-04-00) in Classic ASP/VBScript?

I have a MySQL-table in which I store, among other things, dates in a normal "date" field. Some dates however are incomplete with either the day or both the day and month missing, substituted with double zeroes, which I believe is the "mysql way to do it". Some examples:
2007-04-03 - 3 April 2007
2006-03-00 - Mars 2006
2008-00-00 - 2008
Only problem is, when I try to receive dates that are "incomplete" with double zeros in classic asp, it won't output it since it won't recognize it as either a date or string.
What I'd like to do is simply this, outputting the date just like a string:
Set RecSet = Connect.Execute("SELECT * FROM mytable")
Response.Write RecSet("mydate")
Set RecSet = nothing
I know that I might solve this easily by simply changing the field from date to varchar, but I don't know if there's any side effects to this, like problems when comparing dates in varchar-fields with dates in dates-fields, or comparing varchar-dates with eachother in the SQL query.
Therefore I'd like to keep storing my dates in a date-field, but since I can't output them I have a problem.
So, anyone know how I can handle (partially) zero-dates in ASP?
You could just replace the 00s with 01s in your select statement:
SELECT REPLACE(date_column,'-00','-01') AS formatted_date
FROM mytable

Mysql time stamp queries

I have a column that uses time stamp. My question is I am a bit confused about how to make queries against it,how would I say
Where $time is after X date
Are queries made in local time or CUT?
When I just try to do where andthe post date /time I get an error because of the space and if I quote it I think it takes it as a string : /
What format do I use for the date in the WHERE clauss !?!?
You can use the normal logical comparisons, for example:
SELECT * FROM table WHERE date >= '2010-01-31'
SELECT * FROM table WHERE date >= '2010-01-31 12:01:01'
Time is generally in your current local time, but you can run the query "SELECT CURTIME()" to check. Also, make sure you have the year-month-date in the right order... that can cause issues.
The manual has more details:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Assuming you talk about TIMESTAMP column type (not Unix timestamp) the format is either 'YYYY-MM-DD HH:MM:SS' (quoted) or YYYYMMDDHHMMSS
Atually, in the first case you can use any spearators you wish (or none), only numbers are taken into ccount

MS Access SELECT and WHERE

I am having trouble getting records out of a database for a certain date. This this the SQL command I have at the moment:
SELECT * FROM ict1 WHERE date='26/03/1992'
It should return one record from my database. "date" is a column and one of the records has '26/03/1992' as the value. I have tested the db connection and I can use:
SELECT * from ict1
So I know it's not that. It's probably just the SQL syntax being a lot different, I'm used to MySQL :#)
Should probably mention that I'm using .NET with an OleDbConnection.
Thanks.
Usually dates need to be formatted as for access like the following
Select * from ict1 where date= #03/26/1992#
The # denotes a date in access.
Be very careful with dates formatted as '10/03/1992' as it could mean 10th of march or 3rd of october depending on where you are.
Also #1992/03/26# also works
You may want to use a date comparison function instead of date = #xxxx/xx/xx#. Date comparisons do not yield expected results because of formatting and data type issues. In SQL Server your date might be stored as a datetime or a date data type. You need to make sure you are comparing things in the same type and format.
DateDiff ("d", #1992/03/26#, date) = 0
Use the date as YYYY/MM/DD format:
SELECT * FROM ict1 WHERE date='1992/03/26'
SELECT * FROM ict1 WHERE date=#1992/03/26#