How to convert nodejs string To mysql? - mysql

I am trying to put a date to my mysql database. The column type is date.
the value of orderDate is '2018-04-18' however after putting it into the database it is changed to 1997-00-00 why is that and how can i get the actual value into the database?
This is the function I used: STR_TO_DATE(${orderDate}, "%Y-%M-%D");
If I dont use the function I get an error saying 1997 is an invalid date.

Related

How to convert date from MM/dd/yyyy to yyyy-MM-dd in MySQL? Error: 1411. Incorrect datetime value: '' for function str_to_date

I just need help on how to convert date from MM/dd/yyyy to yyyy-MM-dd in MySQL?
I already asked this question a few months ago, and I was able to successfully convert it. Please see thread here.
However, I'm having difficulty with this new dataset. Previously, when I created thie previous post, ALL of my dataset were in the format MM/dd/yyyy, to which I successfully converted to yyyy-MM-dd using the following query:
Update human_resources.timekeeping Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y');
However, for this new problem that I am encountering, I have a mix of data format in one table. Some time entries are in the format of MM/dd/yyyy while some are in the format yyyy-MM-dd. (Datatype is VARCHAR)
I need to change my old format MM/dd/yyyy to my new format yyyy-MM-dd.
I'm trying to use the same query:
Update human_resources.time_entry_request Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y');
but I'm encountering the following error:
Error Code: 1411. Incorrect datetime value: '2021-05-08' for function
str_to_date
On the other hand, I tried to use this query: (Specified the actual date in the where clause)
Update human_resources.time_entry_request Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y') where Actual_Date = "05/31/2021";
and it was successful. (Changing specific MM/dd/yyyy time entry to format yyyy-MM-dd).
I believe the error that is causing this problem is that the dataset already contains the new and accurate format which is the yyyy-MM-dd.
It also happens vice versa, tried to change the new format into the old one so I can change them to the new one altogether.
PS. The data type for Actual_Date is VARCHAR. I tried changing it to DATE() but am receiving this error:
Executing:
ALTER TABLE `human_resources`.`time_entry_request`
CHANGE COLUMN `Actual_Date` `Actual_Date` DATE NULL DEFAULT NULL ;
Operation failed: There was an error while applying the SQL script to
the database.
ERROR 1292: Incorrect date value: '06/04/2021' for
column 'Actual_Date' at row 17
SQL Statement:
ALTER TABLE `human_resources`.`time_entry_request`
CHANGE COLUMN `Actual_Date` `Actual_Date` DATE NULL DEFAULT NULL
Is there a way I can change the old format to the new one, please? Would really appreciate your help! The dataset contains a mix of MM/dd/yyyy while some are in the format yyyy-MM-dd
My previous post contains ALL MM/dd/yyyy so I was able to convert it successfully.

In SQL Server getting month or date using LIKE and Wildcard Characters gives error

I was trying to find customers born in month of december from a table customers using this query :
SELECT *
FROM customers
WHERE date_of_birth LIKE '____-12-%' ;
But it throws an error : ER_WRONG_VALUE: Incorrect DATE value: '____-12-%'
The type of date_of_birth value is DATE that is it's defined in table as date_of_birth DATE,
Snapshot of the error that i got :
What exactly is wrong here? I remember earlier ones i have used this and it worked fine but now (may be due to new updates) it's not working. Doesn't even seem to be because of MySQL Strict Mode. I can't get what exactly is causing problem.
Also please don't suggest me alternatives i already have those like using MONTH() works fine :
SELECT *
FROM customers
WHERE MONTH(date_of_birth) = 12;
Do not use string functions on date data types. Period. Mixing data types just leads to problems in SQL. Using like on a date requires converting the date to a string and the exact format depends on the locality and settings of the server.
If your data of birth is a string, fix it:
alter table modify column date_of_birth date;
Then the correct way is to use the syntax which you -- for some unknown reason -- specifically do not want to use:
where month(date_of_birth) = 12
If date_of_birth is stored as a string and for some reason cannot be stored using a native format, then you are depending on the unspecified format of the string. Presumably, you want something like this:
where date_of_birth like '%-12-%' -- assuming date_of_birth is a string
assuming that the month is in the middle of the date string, surrounded by hyphens, and the day is either at the beginning or end.
try to where month(date_or_birth)=12
i did not try yet..but there month function in sql

Parsing Custom Date Format in MySQL

Problem
I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.
What I've tried
A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.
Question
Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?
As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.

How to convert a string to date and extract values in Access query

I'm using Access DB 2007 - 2010; I've tried to import many CSV files but the timestamp column keeps failing to import correctly.
So I linked all of the CSV's to an Access DB and I'm trying to query all of the tables.
I'm trying to extract the year and day of the year from the time stamp (which is currently a string)
I'm trying to combine the Format with datepart functions and it keeps failing. (it just says error in the table)
The format function by itself works but I can't combine it with anything.
I'm basically trying to do this:
select datepart("y", Format(gmt, "dd-mmm-yyyy hh:nn:ss")) as DOY from Table1;
but it fails. I've also tried CDate and DateValue in different combinations but it all fails.
Does anyone know how to get this to work?
UPDATE
The format function isn't doing anything. The text remains the same no matter how I try to format it.
Here's a datetime sample: 05-Dec-2008 13:40:01.955
Access can't cope with the milliseconds in your date strings.
Use Left() to exclude them and feed the resulting substring to CDate().
SELECT CDate(Left(gmt, 20)) AS date_from_string
FROM Table1;
Once you have a valid Date/Time value, you can use Year(<Date/Time value>) or DatePart("yyyy", <Date/Time value>) to extract the year. And DatePart("y", <Date/Time value>) will give you the day of the year.
Just solve this issue, here is my code for your reference:
update tablename
set date=cdate(format(left(gmt,4)&"-"&right(gmt,2),"yyyy-mm"))

MySQL DATE type value gets displayed with hours/minits/seconds, however its is not stored like that in the database

I have DATE type column in a MySQL database table with dates stored in it. I am using the YYYY-MM-DD format when I upload the dates to the database.
When I check the values they are stored in the correct and expected format (2005-01-02), however when I am attempting to read these values with my C++ program I am receiving something like this: 02/01/2005 00:00:00
I don't really understand the reason, I thought in the DATE type only the YYYYMMDD gets stored without the time. The other weird thing is the reformatting I thought YYYY-MM-DD is an accepted format.
Here is my code:
String^ getDateOfBirth = myReader->GetString("player_date_of_birth");
DateOfBirthLabel->Text = getDateOfBirth;
I am using Visual Studio 2013.
Any help would be appreciated.