SQL Date format in schema definition - mysql

For "date1" attribute of my table I am trying to insert date of the format
YEAR-MONTH-DAY HOUR:MIN:SEC
Do I have ot define this format in the schema?
All I have done is
date1 DATE NULL
I know I can use DATE_FORMAT(), but I think only for retrieving/querying purposes.
What about recording data in the table?

if you are trying to insert a date with the time you need to instantiate it with datetime
date1 DATETIME NULL
SEE DOCS
alternatively you can store it as a TIMESTAMP. both store in that format

Your schema is fine (though you'll need DATETIME if wanting to preserve the time component), there aren't formatting options for how a DATETIME type is stored.
As long as the data you're inserting has a valid datetime format (the one you posted is fine) then you don't need to do anything else. Some non-standard formats will require a fix prior to insert.
The DATE_FORMAT() function is for displaying a DATE/DATETIME in a chosen format, but how the data is stored is defined solely by the data type.

Related

how to get the date from left to right in mysql?

what to do, that I get the Date like that 08-06-2017 and not 2017-06-08 ? in the table of mysql
Because I send the date_value to server like that 08-06-2017
Don't do it, it's going to be a mess. First of all the mysql date, date time and timestamp types do not support being stored in different formats. You could however select them with a different formatting, if you wanted to (https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format). If you absolutely have to store dates in a specific format, you would have to use text or varchar as type. But then your next problem is going to be ordering and grouping over date fields like that.
My recommendation would be to just stick with the mysql date format and do transformations between the different formats in whatever MVC framework you're using inside the controller or view. That's what they're for. This also leaves you open for the possibility to use different date time formatting based on user locale.

How to convert datetime string of different formats in a valid datetime in SQL 2008

I have a table in SQL database in which there is an NVARCHAR(MAX) column. In it, the data is stored as 'Datetime1&Datetime2&Datetime3&Datetime4...'(& separated dates. I know this is not correct way to store data but I have to support an older system).
Now the issue is that this table is used in multiple sites and hence the data coming in this column could come in different formats. Means, for one site, the datetime could be in the format '22-06-2015 13:00:00' and for some other site, the datetime could be in the format '2015-06-22 13:00:00' or '2015-22-06 13:00:00'.
I have to break the data from this column and store individually each value as a datetime in a new row in another table. This has to be datetime only as I need to do some date operations like comparison, Datediff, Dateadd etc.
So, in my Stored Procedure, if I select value as convert(datetime,[date],121), then it gives error if the string is in any other format than 121(yyyy-mm-dd hh:mm:ss.mmm).
So how can I address this problem that this datetime string could be converted to actual datetime without any issue of a particular format or system format?

Retrieve data with different datatype

I am storing data in my table with char datatype but there are date values also.
The table has only 3 columns
Name Field Content
The problem comes when i try to retrive dates from the data and have them in ascending or descending order since mysql treats them as strings instead of dates the output isnt quite correct.
Is there anyway i could retrieve the dates telling mysql to treat them as dates and not strings?
thats probably because it is not a datetime datatype in the database.. its a string.. use the STR_TO_DATE() function to convert it to a date.

Change datetime format within CREATE TABLE

I hope you might be able to give some advice. I'm trying to create a table within SQL Server 2008 and I need to update my DateTime column to a different format.
CREATE TABLE #History (Tag nvarchar(512), User nvarchar(40), DateTime datetime, Quality int)
However I need to make the DateTime column return a DateTime value in the following format:
yyyy-MM-dd THH:mm:ss
Thanks in advance for any advice you can give.
There's no dateformat there's only the binary data!
Your date data is persisted as bunch of 0 and 1 and got no format at all.
when you retrieve that data, let's say in a select the query analyzer ill show you a representation of that data, by default a ISO formated datetime.
If anything consuming that result set needs a specif date format you can use cast/convert or set dateformat (and language) to achieve that.
See Damien comment for a good example.

Mysql wrong datetime format

I am inserting date from Android app in this format 2013-11-30T00:00:00.000+01:00 but when I see the stored data, it shows like this: 2013-11-30 00:00:00.
I tried different structures - timestamp and datetime, it's always the same. Why is it changing, or what am I doing wrong? Thanks
If you check out the reference guide for The DATE, DATETIME, and TIMESTAMP Types, you'll see both DATETIME and TIMESTAMP store the data in the format YYYY-MM-DD HH:MM:SS.
If you want to store it in the original format then you can use CHAR instead.