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.
Related
In my datatable there is a column named 'journeydate' which is Varchar Type and its containing data as DD/MM/YYYY format, Now what I require at the moment is I want to convert varchar type to Date Type Column with Same data but in YY-mm-DD format.
How Can we do this without Loosing existing data.
Screenshot of Database Table:
Conversion:
update tbl set jdate=str_to_date(journeydate, '%d/%m/%Y');
You should do the conversion in three steps:
Create a column of type date (e. g. jdate)
fill the column with values from current journeydate column. (Use the conversion I posted above.)
You can now rename the two columns once you have established that none of your queries still relies on journeydate being of type varchar.
As an optional last step you can then delete the obsolete varchar column. I would keep it for a while in order to check for possible conversion errors.
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?
The Scenario: I have a table In Access that has a column of type text but contains numerical values that needs to be sorted in a descending order.
The Issue: I tried doing a query and specifying the order in the design view, but the results are being sorted based on the first digit and not the whole number.
When I tried to change the type of the column from text to numeric, I received a warning some data will be lost and also the sorting yielded wrong results.
The Question: Any suggestions about how to solve this issue?
Note: I am importing the data from an excel sheet where the column is of type standard.
I find no datatype NUMERIC in the Access SQL documentation. If data will be lost, you use a datatype that is too small for the converted values. Probably use FLOAT, a double precision floating point value. You could add a FLOAT column to your table and then UPDATE that field of the table from the text field. This will preserve your old values and lets you check for any non-converted values.
UPDATE myTable SET float_field=text_field;
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.
I have events saved in my database which are saved using the date format below.
'2013-09-12T12:14:18Z'
I am trying to get these events using the query below, which returns an empty result set.
SELECT * FROM events WHERE event_time BETWEEN '2013_09-12T12:13:16Z' AND '2013_09-12T12:15:16Z'
Is there a way to somehow search these records?
Since you've stored dates as strings, your query is basically an alphabetical lookup (just like a dictionary). As such, comparing 2013_ with 2013- cannot render the "expected" result.
Solutions:
Fix the column type
Convert column to actual date inside the query
Always use the same format: 2013-...Z