what is wrong with this query using str_to-date function? - mysql

SELECT STR_TO_DATE(SUBSTRING_INDEX(`REPORTDATETIME`,' ',1),'%m/%d/%y')
FROM crimes
where REPORTDATETIME like '%1/12/2001%'
it is the query which iam using
reportdatetime(varchar) is the column name of table
reportdatetime
1/12/2001 1:30
12/23/2003 1:09
11/12/2001 1:30
5/23/2003 1:09
the result which query gives
2020-1-12
but the result iam expecting is 2001-1-12

Your STR_TO_DATE format should be '%m/%d/%Y' for a four-digit year.
Other peripheral issues to note... I would expect your WHERE clause to catch two different rows (always best to avoid using a leading '%' in LIKE whenever you can). Also, a time of 1:90 looks very strange.

Related

STR_TO_DATE() returns wrong data from right format

Using MySQL 5.7, it's pretended a conversion from strings representing four-digit year followed by two-digit month with no any other character in the middle.
For example, running the following statement
SELECT str_to_date('202105','%Y%m');
What is returned: 160101 being January 1601 instead of May 2021. The required arguments seem correct considering the function in cause.
I was not able to reproduce your exact result on DBFiddle, but I did see it creates a null instead of the expected date. What you can do is append a '01' to get a better date literal:
SELECT str_to_date(concat('202105', '01'),'%Y%m%d');
See it here:
https://www.db-fiddle.com/f/sbArVsLF6BVDGSrYdy8geP/0
You need to specify the day as well, otherwise it won't work in MySQL 8.0+.
You need all parts of the date i.e. year, month and day must be specified for the function to operate properly.
# Incorrect Code:
SELECT str_to_date('202105','%Y%m');
# Correct Code:
SELECT str_to_date('20210501','%Y%m%d');
Outputs
2020-05-01
Ref: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date

Get an integer and convert it to a date - mysql

I am trying to extract the date from a members CPR numbers in my Database, its stored as an integer. (CPR is Danish social security number and the first 6 characters are your date of birth)
Here is my SQL statement however it doesn't quite work. The SELECT LEFT bit works its just the STR_TO_DATE which returns an error.
SELECT LEFT(cpr, 6) AS #iCPR FROM members
STR_TO_DATE(iCPR,'p%m%Y')
It looks like you have a mistake in your STR_TO_DATE format. Try changing it to '%d%m%Y'.
See here for the list of valid specifiers for that function:
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

MySql incorrect datetime value

I know this question has probably been asked but I cant for the life of me figure out what is wrong with my datetime value that mysql doesnt seem to like.
In my situation, I am appending 'explain ' in front of every query to see what the explain plan looks like. This is done BEFORE the actual query is run. The problem is mysql doesnt like the date format in the explain but the regular query runs fine.
This is the error I recieve:
SQL Error: 1292, SQLState: 22007
Incorrect datetime value: '11/19/2015 19:49:34.076' for column 'createdTime' at row 1
Query is:
explain delete from LoggableActivity where createdTime<'11/19/2015 19:49:34.076'
What is wrong with this format? it looks good to me...
And why only the query appended with explain isn't working?
Some more info:
I am using an entity manager in java to create and execute the queries and the date being generated is a result of Java's 'new Date(milliseconds)'.
Thanks
MONTH/DAY/YEAR is not (repeat not) the best sequence in the world
YEAR/MONTH/DAY is much better
try this:
explain
select from LoggableActivity
where createdTime<'2015-11-19 19:49:34.076'
Consider this article at wikipedia
https://en.wikipedia.org/wiki/Date_format_by_country
Look at how many people in the world use either a "big endian" (yyyy-mm-dd) or "little endian" (dd-mm-yyyy) date format. It is far wiser to treat date strings as "big endian". In particular starting a date string by year reduces possible confusion with dates where the day number is less than 13.

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

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#