How to find Date format in MySQL - mysql

I am looking to simply find out what is the current Date format that I have in m MYSql Database. This should be easy but I am not finding the command or function to do this. I simply want to query for date format and have it return what it is currently set at, basically; DD:MM:YY or YYYY:MM:DD or whatever the format currently is set at for a particular DB.
Thank You

By default, the date format is YYYY-MM-DD. However you can convert any date with the CONVERT()-function. Make sure to look this function up.
You'll have lots of possibilities.

Related

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

I have a large dataset with employees' time entries. The current date format is MM/dd/yyyy. However, I need to convert all the dates into yyyy-MM-dd format.
I have tried the following:
Update human_resources.timekeeping
Set Actual_Date = str_to_date(Actual_Date,'%d-%m-%Y');
Got the errror messsage Error Code: 1411. Incorrect datetime value: '' for function str_to_date.
My SQL version is 5.7.18-log.
I tried to view SQL mode using SELECT ##sql_mode; and I got NO ENGINE SUBSTITUTION.
I have tried to retrieve the value like shown below and it was working fine.
Converting varchar mm/dd/yy to date format yyyy-mm-dd
However, updating the data would not work. I need to update the actual records, not insert new records.
Hope someone can help me regarding this. Thank you in advance!
EDIT: The data type for Actual_Date is VARCHAR.
Apologies if my explanation may be a bit confusing. But I am using this data set to display and filter time entries in a gridview. When I am filtering dates, say for example (01/15/2022-01/25/2022), data from 2021 is also being displayed. When I tried to manually change the format of some of my data in sql to yyyy-MM-dd, my code seemed to be working fine. The problem is there are a lot of data in this table, which is why manually updating the format is impossible. What is the first thing that I need to do? I'm sorry this is all still a bit confusing for me.
My apologies if you have already taken the following things into consideration but I thought them worth mentioning.
Given that you say this is a "large dataset" I assume this is a table that is currently in use. Does the existing application rely on the Actual_Date being in that string format? Does it rely on a fixed number of columns in the table? Some poorly written applications can be very brittle when it comes to changing underlying data structure.
You may want to consider creating a copy of the current table, modifying the structure of the copy, and replacing the original with a view with the same columns and formats as the original. This way you get improved data but reduce risk to existing application.
In the title and first line of your question you state that the current format is MM/dd/yyyy
Update human_resources.timekeeping Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y');
Your separator is / not -
%d-%m-%Y >> %d/%m/%Y

How to format a esriFieldTypeDate parameter in a json url query to an ESRI REST server

I'm trying to query this API, filtering by the RptDt field, which is type esriFieldTypeDate.
The basic query looks like this:
https://dhsgis.wi.gov/server/rest/services/DHS_COVID19/COVID19_WI_V2/MapServer/11/query?where=1%3D1&outFields=*&outSR=4326&f=json
It's easy to filter a numeric variable like POS_CUM_SUM like so:
"https://dhsgis.wi.gov/server/rest/services/DHS_COVID19/COVID19_WI_V2/MapServer/11/query?where=%20(POS_CUM_CP%20%3D%200%20OR%20POS_CUM_CP%20%3D%2010)%20&outFields=*&outSR=4326&f=json"
I can't figure out how to format the minimum and maximum arguments for the date field RptDt.
because the attributes of RptDt are formatted as unix timestamps: 1642255200000, 1642428000000. But that returns error code 400.
https://dhsgis.wi.gov/server/rest/services/DHS_COVID19/COVID19_WI_V2/MapServer/11/query?where=%20(RptDt%20%3D%20'1642255200000'%20OR%20RptDt%20%3D%20'1642428000000')%20&outFields=*&outSR=4326&f=json
Then, I noticed that the field length for RptDt is 8, so I tried rounding the unix timestamp to 8 digits (16422552, 16424280), but that also gives error code 400.
I tried using YYYYMMDD format (20210101 to 20211231), this didn't give an error, but the response has no features. Most of the dates do fall this period.
https://dhsgis.wi.gov/server/rest/services/DHS_COVID19/COVID19_WI_V2/MapServer/11/query?where=%20(RptDt%20%3D%20%2720210101%27%20OR%20RptDt%20%3D%20%2720211231%27)%20&outFields=*&outSR=4326&f=json
I haven't been able to find a solution in the ArcGIS REST APIs documentation. Does anyone understand what's missing from my queries?
The correct format is 'YYYY-MM-DD'. This query works. The relevant bit is where=RptDt>'2022-01-01'.
https://dhsgis.wi.gov/server/rest/services/DHS_COVID19/COVID19_WI_V2/MapServer/11/query?where=RptDt>'2022-01-01'&outFields=*&outSR=4326&f=json
Thanks to JamieKelly1 at the Esri Community Forum for answering this question.
For anyone else stumbling upon this one after getting the -2147220985 (which btw means bad SQL query), the full url I used had to contain Date before the actual date like so Date'2015-02-02' not just '2015-02-02'. Also, you cannot use epoch timestamp in miliseconds in the query builder (at least not for open data dc).
Example url:
https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/ServiceRequests/MapServer/6/query?where=%20(ADDDATE%20%3D%20Date'2015-02-02'%20OR%20ADDDATE%20%3D%20Date'2015-02-03')%20&outFields=*&outSR=4326&f=json
To search between two dates:
https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/ServiceRequests/MapServer/6/query?where=ADDDATE BETWEEN DATE '2015-01-09 00:00:00' AND DATE '2015-01-09 12:00:00'&outFields=*&outSR=4326&f=json

Remove unnecessary string and Update date format in MySql

Basically my date in MySql table looks like this
2001-04-16
The format is actually day-month-year. Unfortunately 20 is added to the date. So finally i should update this date. This should become
01-04-2016
How to update my all date format in my table
Don't change this in the database, change it when you need to display it. The ISO date format is what's used internally and should be kept that way, it's the most native, efficient format, and it sorts correctly.
You can select them out:
SELECT DATE_FORMAT(date_column, '%d-%m-%Y') FROM table_name
Using the DATE_FORMAT() function.
Even better is to do this in your application layer, whatever that is, using a date formatting function tuned to the user's locale.
If you've got a bunch of bad data in your database you need to convert, you can re-write it with the DATE_FORMAT() function to strip out the mistakes.
For example:
UPDATE table_name SET date_column=DATE_FORMAT('20%d-%m-%y', date_column)
For mysql, MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. so you can fetch date in whatever format you want from database, please see https://dev.mysql.com/doc/refman/5.6/en/datetime.html for more info.
you can use DATE_FORMAT() function :
SELECT DATE_FORMAT('%d-%m-%Y', date) as desired_date FROM table_name where 1

Importing .csv and converting to dates in mysql

I am trying to import a CSV file with date information in the format MMM-YY (e.g., Jan-92). My ultimate goal is to have the information in a date format in SQL. The format doesn't matter that much to me, but I was thinking something like dd-mm-yy (e.g., 16-01-92). I've tried a lot and looked around the forum, but can't figure it out.
Right now, I am loading the data from the CSV into a column "period_month" as a VARCHAR field.
Then, my conversion code looks like
UPDATE PERIODS
SET period_month = DATE(str_to_date(period_month,'%M-%Y'));
I end up with a field of VARCHAR type in the format yyyy-mm-dd, with the dd field set to '00'. (e.g., 1992-01-00)
I really just want this to be in a date format that I can export to another program like R for analysis.
Any help appreciated.
try
UPDATE PERIODS
SET period_month =DATE(str_to_date('Jan-92','%M-%Y')+1)
or
UPDATE PERIODS
SET period_month =DATE(str_to_date('period_month','%M-%Y')+1)
use date_format function of mysql query

Change Date Format in SQL Server 2008

I have a table where users used to enter random date formats like
dd/MM/Year or MM/dd/Year.
But I want to update all those dates to this format Year/MM/dd
I have made a cursor and changed data by CHAR INDEX and sub string..
but is there any easy way to change the date format to the one i want ?
and also how can I make my default database date format = Year/mm/dd
only database not server?
You can convert it into different styles.
for example:
select convert(varchar(15),getdate(),103)
will give today's date in dd/mm/yyyy.
Full list of converting styles supported by MSSQL is here