Change Date Format from '-' to '/' - mysql

How to update or change date format "%Y-/%m-/%d" to "%Y/%m/%d" in my sql. After refreshing it goes to normal '-' format.

just use select DATE_FORMAT(yourdatecolumn, "%Y/%m/%d")
example
select DATE_FORMAT('2018-08-01', "%Y/%m/%d")
2018/08/01

From https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html :
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. However, it will recognize the (input) format to a date field in any of 'YYYY-MM-DD' or 'YY-MM-DD' or 'YYYY/MM/DD', 'YYYY^MM^DD' and 'YYYY#MM#DD' or even YYYYMMDD.
If you want to display a date in a different format (e.g. your chosen format of YYYY/MM/DD), then you need to change the format in the query string. Consider this example below
I have a table with 3 fields id, Date and Amount. If I run the query
SELECT id, Date, Amount FROM `dtdata` ORDER BY `dtdata`.`id` ASC
I get the output as shown:
If I want to display the date differently (i.e. your required format), I can use the following Query:
SELECT id, DATE_FORMAT(Date, "%Y/%m/%d") AS NewDate, Amount FROM `dtdata`
to get the output as

Related

Extract Numeric Date Value from short Date Format in SQL

I am trying to convert short date format to numeric date format in SQL.
Here are the dates I am trying to convert:
16-Mar-20
18-Mar-20
12-Mar-20
I have seen many methods to re-format dates based on numeric numbers but I am working with a dataset which has varied date-types. To ensure smoothness, I wanted to know what's the way to convert these numbers to numeric date. Note that even if I extract the day month and year individually I could multiply them with respective values to get the dates. Here's what I have done: (Although I have tried multiple things but this is the one which has yielded me the closest result, Note that my end goal is to convert the date into numeric value)
FROM_UNIXTIME(`Start Date`, 'dd-MMM-yy') AS 'Date Numeric'
Here Start Date is formatted in the way I have mentioned above. (14-Mar-20).
UNIX_TIMESTAMP is what you need, but first you have do transform your data to a Date
UNIX_TIMESTAMP(STR_TO_DATE(`Start Date`, '%d-%b-%y'))
With
SELECT UNIX_TIMESTAMP(STR_TO_DATE( "14-Mar-20", "%d-%b-%y"))
You get
UNIX_TIMESTAMP(STR_TO_DATE( "14-Mar-20", "%d-%b-%y"))
1584144000
If you want the Unix timestamp, use UNIX_TIMESTAMP():
UNIX_TIMESTAMP(STR_TO_DATE(`Start Date`, 'dd-MMM-yy'))

Convert String Field to Date value in a different format with an SQL select query

I have a table with a column start_date. Unfortunately, the datatype of the column is VARCHAR. Values in this column are either null or a string date in any format.
I have a requirement to extract the data from this table to a CSV file using BCP command. But while extracting, start_date value is expected to be in YYYYMMDD format in the output file.
The problem here is for some records, start_Date will be in DD/MM/YYYY format and for some others it may be in another format. For example : YYYY-MM-DD.
Is there any way so that I can convert the start_date value to the format YYYYMMDD irrespective of the actual format using the select query itself.
select convert(DATE,'13/12/2019',112) as 'AliasName'
This function will help u in fetching the data in the format of YYYYMMDD irrespective of how the format of ur input column is..This function uses 3 arguments 1.THE DATA TYPE WHICH U WANT TO CONVERT 2. YOUR INPUT COLUMN WHICH U WANT TO CONVERT 3.STYLE (style number is: Each format has its own style number for example if you give style number as 103 it will convert to dd/mm/yyyy or if 112 it will convert to YYYYMMDD ..there are many style You will find it if u search about convert function) . Hope this will help!
Hope this helps!!
select
*,
(CASE WHEN [Date] like '%/%' then TRY_CONVERT(datetime,[Date],103)
else [Date] END) as [New_DATE]
from TableName;

How can i do a count of two columns in mysql?

i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?
Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.
Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal
WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.

SQL Change date format from yyyy-mm-dd to dd-mm-yyyy

I have created MySQL table :
CREATE TABLE EMP(
EMPID INTEGER NOT NULL (5),
SURNAME VARCHAR(25),
SAL INTEGER(5),
JON VARCHAR(25),
START_DATE DATE,
END_DATE DATE,
DEPNO INTEGER(5)
);
with following records:
INSERT INTO EMP
(EMPID,SURNAME,SALARY,JOB,START_DATE,END_DATE,DEPNO)
VALUES
('1','Vorosila','500000','COO','20150101',null,'1');
however I need to change date format from 2015 01 01 to 01 01 2015
Can anybody show me or tell me how to do that ?
THANK YOU IN ADVANCE
DATE values do not have a "format", they are objects that represent instants in time (or entire days, but still independent of formatting).
Formats are applied on input and output, so you just need to apply the correct format, which you can find in the MySQL manual, to the SELECT statement.
You cannot change the default date format in mysql.
I once hoped for the default date to be editable so I wouldn't have to jump through these hoops to get the date I actually wanted, mysql even has a date format system variable, but it is unused. Date Format Mysql - link
What you should really do is store it as the default format Year-Month-Date and then convert it on select.
The first thing I'd suggest is having your date columns as date types, which would give your dates the following format '2015-01-01'.
If you do this then you can use DATE_FORMAT - link - the second value in the DATE_FORMAT function allows you to customise the returned date, and there are many different thing you can do with this if you look at the link:
SELECT
DATE_FORMAT(`START_DATE`,'%d-%m-%Y')
AS `START_DATE`
FROM ...
The other option you have is to store your dates in the format that you already want as a char or varchar column.
HOWEVER, as should be obvious, this column will not be treated as storing dates, and so will not give you the correct comparisons in a where clause when using > < BETWEEN or the correct ordering in an order by clause. It is after all just a string of numbers in this case.
However you can then use STR_TO_DATE - link if you did need to use a where or order by on this column to change it back to a date within the query - in this case the second value is the custom format of your 'dates' in the column. Keep in mind with a where you will need to compare it with the correct mysql format as shown below:
SELECT
`START_DATE`
FROM table
WHERE STR_TO_DATE(`START_DATE`,'%d-%m-%Y') BETWEEN '2015-01-01' and '2016-01-01'
In MySQL you can change the format of a date using DATE_FORMAT method which is similar to to_char in Oracle.
DATE_FORMAT(SYSDATE(), '%DD-%MM-%YYYY');
For more information about specifiers check this thread http://www.sqlines.com/oracle-to-mysql/to_char_datetime
You can do what you probably want by creating a view and referring to that instead of the (underlying) table.
CREATE VIEW emp_view AS
SELECT empid,
surname,
sal,
jon,
date_format(start_date, '%d-%m-%Y') as start_date,
date_format(end_date, '%d-%m-%Y') as end_date,
depno
FROM emp;
Note that this changes the type of the date columns to varchar, so comparisons will no longer work as expected:
SELECT * FROM emp_view WHERE start_date > '01-12-1924'; // fails!

Change datatime to date dd/mm/yy

Im trying to query some data and one of them is a datetime format. I want that is shows dd/mm/yy with no time on it directly form the select. Is this possible?
My query is and the join_date is datetime that i need to be changed to short date:
$query = mysql_query("SELECT id,username,join_date,is_active FROM members",$con)
or trigger_error(mysql_error());
This query goes directy in a Json output array. So i want to convert it directy form the query.
Use the MySQL DATE_FORMAT function for this:
SELECT id, username, DATE_FORMAT(join_date, '%d/%m/%y') AS join_formatted, is_active
FROM members
In this example, the column name for the formatted date will be join_formatted, and its type will be VARCHAR.
The format string returns the date as dd/mm/yy as requested, but I'm personally more comfortable when the date includes the full century. To get the full century, use uppercase Y in the format string: %d/%m/%Y.
try this :
"SELECT id, username, DATE_FORMAT(join_date,'%d/%m/%y'), is_active FROM members"
Use DATE_FORMAT:
SELECT id, username, DATE_FORMAT(join_date, "%d/%m/%y") AS date FROM members;
%Y Year, numeric, four digits
%y Year, numeric (two digits)
For details about date format link