sql search between date Error - mysql

I want to retrieve data between two specific date and I am getting that perfectly right with this query
select * from POS.dbo.voucher where date_time between '10 october 2014 00.00.00 ' and '11 october 2014 12.00.00'
but the issue is if I'm changing the month something like
select * from POS.dbo.voucher where date_time between '10 march 2014 00.00.00 ' and '11 march 2014 12.00.00'
It's still returning me with the same records. Seems like its only comparing the date and returning me the records.
Any idea where i m doing it wrong?
I am taking date as varchar in database.
Thanks!!

You appear to be using SQL Server (based on the dbo). Use a standard date format for date constants:
select *
from POS.dbo.voucher
where date_time between '2014-10-10 00:00:00' and '2014-10-11 12:00:00'
However, for this to work, you need to store dates using proper date formats in the database. So, you should fix the data structure to store dates using the correct data type.

I agree you would be better off changing the field from varchar to datetime. If that's not an option then you should convert the varchar field in the query into a datetime as appropriate in MS SQL Server, probably through a function, perhaps through a cast. BTW, best to tag with actually db used, and maybe SQL too.

Related

Count and group by day MySQL query

How can i count and group by day in MySQL. My issue is that I'm not using a MySQL time stamp.
I'm saving my date in below format
date("F j, Y");
So the date will be November 24, 2015, November 25, 2015, November 26, 2015 etc.
Can i keep the format as it is and count and group by day?
The answer is - technically - yes, you can groupy by day and keep this format using mysql's str_to_date() function to convert your string to a date type and extract the day part out of it using one of the day related functions.
The question is, why would you do this? You should store the date either in a timestamp or datetime column and format the dates to your specification when you query it using date_format() function. This way you can retrieve the dates in the desired format and you retain the ability to easily perform date arithmetic within your sql code.

MYSQL query: select between date with local format

my mysql database tb_date (varchar 20):
16 November 2014
06 December 2014
01 April 2014
12 April 2015
I want select between 01 January 2014 until 31 December 2014, how the query is with date conversion?
thanks..
This is an anti-pattern, storing date values in VARCHAR columns, rather than using datatypes specifically designed and implemented for storing date values... DATE, DATETIME or TIMESTAMP.
To answer your question, before it gets closed, you could use the STR_TO_DATE function to convert the strings into DATE datatype, and then do the comparison. MySQL won't be able to make use of an index range scan operation, it will need to evaluate that function on every flipping row in the table.
As an example:
SELECT t.mycol
FROM mytable t
WHERE STR_TO_DATE(t.mycol,'%d %M %Y') >= '2014-01-01'
AND STR_TO_DATE(t.mycol,'%d %M %Y') < '2015-01-01'
We'll need to check the MySQL Reference Manual to verify that '%M' is the right format specifier for the full month name...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Yes, it looks like I guessed right. M is the month name.
As I already commented, you should store your date as Timestamp or Data format then you could simply compare.
However, there is still a solution.. You can convert the varchar to a date directly in your query :
select * from yourTable
where (str_to_date(tb_date, '%d %M %Y') between '2014-01-01' and '2014-12-31');
But please don't use this hack and change your date format...
Edit : If you are really willing to use varchar to store your date, change it to varchar(17) which is the max character possible using your string format.

Convert Date to UTC timestamp in mysql

I have a table with the following columns:
|start_date |TZ |
|Dec 2, 2012 |Eastern |
|Dec 2, 2012 |GMT |
Note 1: our server is in UTC time.
Note 2:The column start_date is a date field, not a timestamp field. Dec 2nd 2012 implicitly means "2012-12-02 00:00:00"
Note 3: The above table is actually multiple normalized tables, but for simplicity, I de-normalized it.
Note 4: I can put anything into the TZ table to make this easy.
I would like to select from my_table where start_date <= now()
However, this doesn't work because of timezone. If the current date/time is
Dec 1st Eastern at 9PM (which is Dec 2nd 1AM UTC), the above query will return both results,
but I really only want the 2nd one. This is further complicated by daylight savings.
Ideally, I would like a query that does the following:
select * from my_table where convert_to_utc_timestamp(start_date,tz) <= now()
The above method would convert start_date to a timestamp and then convert it to the right timezone.
How would I do this in SQL?
There are two functions you'll probably find useful.
The first is:
STR_TO_DATE(start_date,'%M %d,%Y')
That will get your string, in the specified format, converted to a MySQL DATE datatype.
If you have the mysql.time_zone_name et al. tables populated, you can use the function:
CONVERT_TZ()
(need to check that CONVERT_TZ takes a DATE and will return a DATETIME or TIMESTAMP, or include a time component in the string being converted to get a DATETIME, e.g.
STR_TO_DATE( CONCAT(start_date,' 00:00:00'),'%M %d,%Y %T')
Wrap that expression in the CONVERT_TZ() function, e.g.
CONVERT_TZ( datetime_expr ,'US/Eastern','GMT')
To make use of the values stored in your TZ column, those are going to need to match, or you need to come up with a way to match to, the values stored in the mysql.time_zone_name table.

How to insert just year and month into date field?

I have a column called table_date which currently I am using now() to insert the current date (2011-02-23). I know I can manipulate this with sql/php to show me year and monthname. However, I want to know if it's possible to just insert into table_date the current date as year-month like this 2011-02? Thanks
A DATE field is always going to be a full date, and as far as I know, it is also always required to specify a full date.
It might be easiest to use 01, like 2011-02-01 for February 2011.
Obviously, you can format the output of a DATE field to your liking when querying it:
SELECT DATE_FORMAT(fieldname,"%Y-%m");
If you insert something like 2010-10 into a DATE column, mysql throws a warning and inserts 0000-00-00. If you do not want to specify a certain day, you can insert something like 2010-10-00. Pay attention when querying for "all entries in October 2010" since WHERE date >= '2010-10-01' will not return 2010-10-00.

datetime searching using sub-sections of the YYYY-MM-DD HH:MM string

[MySQL/PHP] My table has a date column of datetime format. All records are of the YYYY-MM-DD HH:MM:SS variety.
MySQL queries like SELECT record FROM table WHERE date > '1941' AND date < '1945' work nicely.
MySQL queries like SELECT record FROM table WHERE date > '1941-03-01' AND date < '1945-01-30' also work nicely.
But what about if I wanted all records that were filed in March, regardless of year? Or all records filed on the 17th, regardless of month/year?
``SELECT record FROM table WHERE date = '03'` clearly doesn't work.
I know I could snag it with a LIKE '%-03-%' parameter, but that doesn't leave room for me to search for range, like all records from March to May.
Help? :-)
Try WHERE MONTH(DATE(`date`)) BETWEEN '03' AND '05'
The DATE() part is to extract the date from the timestamp to be used with MONTH().
You can use MySQL date functions:
SELECT record FROM table WHERE MONTH(date) = 3
SELECT record FROM table WHERE DAY(date) = 17
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
If you look at http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html, you will find many useful functions for your purpose.
... WHERE MONTH(date) = 3, e.g. =)