Mysql database data format in .net - mysql

Hi I will use mysql db in my project. The database using with php now. I will use with asp.net. Tables have data column and date like this : 1289053800.
How can I convert this value to .net datetime.
Regards

It seems your date time values are stored in UNIX_TIMESTAMP format.
You can use FROM_UNIXTIME on them to convert to regular datetime type.
select
date_cloumn -- that has 1289053800 kind of values
, from_unixtime( date_cloumn ) as dt_f_ut
, date_format( from_unixtime( date_cloumn ), '%d.%m.%Y %h.%i' ) as dt_f_ut_12hr
, date_format( from_unixtime( date_cloumn ), '%d.%m.%Y %H.%i' ) as dt_f_ut_24hr
from table_name
;
Using .Net, you may need no changes but just read:
a DateTime on dt_f_ut or
a String on dt_f_ut_12hr or
a String on dt_f_ut_24hr or
into desired variables.
Demo # MySQL 5.5.32 Fiddle
Refer to:
FROM_UNIXTIME()
Format UNIX timestamp as a date
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
Return a UNIX timestamp

Related

Covert varchar(150) to datetime in sql workbench

I have a column with values '2015-02-14 12:23 AM' declared as varchar(150), I tried using to_date, convert and cast but not able to change the format. I would need this to filter on specific month/year/day. Thanks for the help
PS: Mysql instance is running on RDS through amazon AWS - not sure if its relevant
I suggest storing dates on proper date data type.
to_date is an Oracle function.
In MySQL, you can use STR_TO_DATE to converts a string to date then use Date_format to give the format you need
SELECT DATE_FORMAT(STR_TO_DATE(dt,'%Y-%m-%d %h:%i %p'),'%m/%Y/%d')
from test;
Demo
You can use substring with concat without the need of converting to date format:
select concat(substring(dt,6,2),'/', substring(dt,1,4),'/',substring(dt,9,2)) as my_date
from test;
Demo
SELECT CAST( SUBSTRING_INDEX( '2015-02-14 12:23 AM',
' ',
2
)
AS DATETIME
)
returns the datetime value. If you need to use this value in datetime context then you may remove CAST, it will be performed implicitly:
SELECT TIMESTAMPDIFF ( MINUTE,
SUBSTRING_INDEX( '2015-02-14 12:23 AM',
' ',
2
),
'2015-02-14 12:34'
)

Convert a MySQL timestamp to actual datetime format ina MySQL query

I have following query and data saved in a MySQL database with a timestamp column. I have to display the timestamp as the actual data and time format like 1333007294 it should be displayed as 03-MAR-2012 12:48:14 PM. I have tried to use MySQL using from_unixtime but it displays the datetime like this 3/29/2012 12:00:00 am. I need to get the format like 03-MAR-2012 12:48:14 PM here is my query.
SELECT DATE( FROM_UNIXTIME( `timestamp` ) ) AS timestamp
from vlasteventwdevice
order by 1 desc
Try like this:
FROM_UNIXTIME(`timestamp`, '%d-%b-%Y %r')
You can make the result upper case later in your application code

How to convert timestamp to datetime in MySQL?

How to convert 1300464000 to 2011-03-18 16:00:00 in MySQL?
Use the FROM_UNIXTIME() function in MySQL
Remember that if you are using a framework that stores it in milliseconds (for example Java's timestamp) you have to divide by 1000 to obtain the right Unix time in seconds.
DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%Y-%m-%d %H:%i:%s') as "Date" FROM `orders`
This is the ultimate solution if the given date is in encoded format like 1300464000
To answer Janus Troelsen comment
Use UNIX_TIMESTAMP instead of TIMESTAMP
SELECT from_unixtime( UNIX_TIMESTAMP( "2011-12-01 22:01:23.048" ) )
The TIMESTAMP function returns a Date or a DateTime and not a timestamp, while UNIX_TIMESTAMP returns a unix timestamp
You can use
select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;
For in details description about
from_unixtime()
unix_timestamp()
SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
This work for me

Parse Date from MM/DD/YYYY to a format MYSQL can work with

Currently working with a MLS data dump in which all their dates are formatted in MM/DD/YYYY , Trying to get the min/max age
MIN( DateDiff( NOW(), idx_common.list_date ))) AS listage_min,
MAX( DateDiff( NOW(), idx_common.list_date ))) AS listage_max
Obviously this does not work because idx_common.list_date is in said format.
Any ideas?
Use str_to_date to convert idx_common.list_date.
MIN( DateDiff( NOW(), str_to_date(idx_common.list_date,'%m/%d/%Y') ))) AS listage_min,
MAX( DateDiff( NOW(), str_to_date(idx_common.list_date,'%m/%d/%Y') ))) AS listage_max
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date contains the answer you seek.
However - please consider using real DATETIME columns instead of strings in your database, if at all possible.
use java or c# or something to split the string of the date by the / and reorder the date into the format expected by mysql before you run the mysql query.

compare string to date on mysql

In MySQL 4.0.21-standard, I have a table with a date, saved as a string.
I want to compare this string with a date in my request.
SELECT FE_CLIENT.*
FROM FE_CLIENT
WHERE D_DATFINPUBLI < '2010/06/03'
How can I cast my column date_deb to a date for compare?
Assuming MySQL (if not, retag your question)
Use the MySQL STR_TO_DATE function to put the '2010/06/03' to a DATETIME value.
SELECT FE_CLIENT.*
FROM FE_CLIENT
WHERE D_DATFINPUBLI < STR_TO_DATE('2010/06/03','%Y/%m,%d');
Do the same thing for D_DATFINPUBLI if it's not already a DATETIME format.
EDIT:
SELECT STR_TO_DATE( D_DATFINPUBLI, '%d/%m/%Y %h:%i' ) DD, FE_CLIENT . *
FROM FE_CLIENT
WHERE STR_TO_DATE( D_DATFINPUBLI, '%d/%m/%Y %h:%i' ) < STR_TO_DATE( '04/06/2010', '%d/%m/%Y' )
AND D_CDSTATUPUBLI <> 'EXP'
ORDER BY D_NIDPUBLI
Just format your string to proper format before query execution
or, if you want it strictly with mysql,
WHERE D_DATFINPUBLI < replace('2010/06/03','/','-')
EDIT:
D_DATFINPUBLI field must be of date type and have format of 2010-06-03