MySQL - Date Column Format - mysql

I am familiar with DATE_FORMAT function which can display the the record from my date field in the format specified. However, I would like to create a table with a date field that only accepts my format.
Here's what I have done so far:
CREATE TABLE test_table (
id INT AUTO_INCREMENT,
f_name VARCHAR(40) NOT NULL,
l_name VARCHAR(25) NOT NULL,
date_hired DATE NOT NULL
);
Inserting a record with a date_hired value of '2013-03-01' will be inserted as '1/03/2013 12:00:00 AM' which is far from my expected result (I would like the format the way it was inserted). Any feedback? Did I miss something?
Thanks,
Michael

You can't change the format during table create, you can change the format of date for displaying user by using you programming logic like if you are using PHP as your server side language the you can convert it your desired format.

Related

Mysql Stored procedure that accepts a date range from user and displays a list of open bugs

I am newbie to MySQL especially to writing stored procedures and functions. Faced lots of errors in writing code that accepts user input and date time manipulation. I have created table called 'BUGS' as below...
create table 'BUGS' (
ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
TITLE varchar(20) NOT NULL,
SEVERITY INT NOT NULL check(SEVERITY>0 && SEVIRITY<5),
OPENDATE datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
CLOSEDATE datetime default NULL
)ENGINE=MyISAM DEFAULT CHARSET=latin1;
I want to write below two things.
Block of code that accepts user input(here TITLE, SEVERITY, OPENDATE, CLOSEDATE) and insert one row.
Stored procedure that accepts a date range from user and displays a list of open bugs(for which opendate between the given date range and closedate beyond date range)
Can somebody help me with the code?
You can write procedure like this :
1 . At first call procedure from your php file(if you have it) like this :
$report1=$db->query("call procedure_name('".$opendate."','".$closedate."')");
2 . Write this code in procedure file :
CREATE DEFINER = 'root'#'localhost'
PROCEDURE db_name.procedure_name(IN opendate varchar (15), IN closedate VARCHAR (15))
BEGIN
//write here mysql query
END

Convert Varchar containing date to DATETIME

I have a database with 6k+ rows and don't want to have to manually convert each date to DATETIME, they are currently in varchar.
They are in the UK format, DD/MM/YYYY.
Currently the date is in a column named datetime which is varchar(12)
I want to convert it to a datetime column named date_new.
How can I do this using an SQL statement
Solved using
UPDATE table SET date_test = STR_TO_DATE( DATETIME, '%d/%m/%Y' )

SQL Server : storing date and time in separate columns

I am using SQL Server 2008 :
I want to keep date and time separated
I want both of them to have default value like NOW() when record is created
How can I control format of date and time
So far I have this code :
CREATE TABLE [Table1]
(
[RECORD_ID] int IDENTITY (1, 1) NOT NULL,
[INPUT_ID] int NOT NULL,
[DATE] date DEFAULT GETDATE(),
[TIME] time(7) DEFAULT GETDATE(),
PRIMARY KEY ([RECORD_ID])
)
I just don't know how to set time condensed data type to time(0). Is rest of my code ok, or there is a better approach ?

Storing time in MySQL

I have data in the format of both "2013-01-17 18:46:47 -0800" and "1358477089" ...I'm wondering what is the best way to store this in a mysql db, that allows me to select results within a certain month, week, day etc.. using mysql's own functions.
Currently my create table code is like this.. the "timestamp" needs changing.
visible
CREATE TABLE IF NOT EXISTS `votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`from` varchar(32) NOT NULL,
`username` varchar(32) NOT NULL,
`address` varchar(16) NOT NULL,
`timestamp` varchar(32) NOT NULL,
PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Best way is to use MySQL built-in DATETIME type.
MySQL offers lots of function which will allow you to select results within a certain month, week, day, whatever you need.
See great list of functions here:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
As hek2mgl and other guys mentioned, there is also TIMESTAMP.
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored.
I preffer and advice you to use DATETIME.
If you use a timestamp your field should be an "integer" not a varchar. This provides better perfomance (for example if you use an index for this column).
If you do not need to have dates before 1970 I would suggest to use a timestamp, not a datetime. It is easier to use.
PHP
$timestamp = date('U');
MySQL
INSERT INTO table SET timestamp = UNIX_TIMESTAMP()

MYSQL: How to convert VARCHAR column containing dates to a DATE column?

I have a large table of birthdays that I want to convert from a varchar column to a date column.
The table SQL is:
CREATE TABLE `birthdays` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uid` bigint(20) DEFAULT NULL,
`birthday_date` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
The birthday date is stored in one of two ways: strings like "05/26/1994" or sometimes "03/14" (for people who don't want to reveal their birth year).
What is the best way to create another table and store the birthdays in a date column? Is it possible to do this just using MySQL (and avoid using PHP or some other intermediary)?
I have found a STR_TO_DATE function in MySQL. Should I use this?
Thanks!
SELECT IF(birthday_date RLIKE '[0-9]{2}/[0-9]{2}/[0-9]{4}',STR_TO_DATE(birthday_date,'%m/%d/%Y'),STR_TO_DATE(birthday_date,'%m/%d'));
This will result in dates like 0000-03-14 for rows that have no year entered. Your server needs to be configured to allow invalid dates though (see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html )
If you convert your column to DateTime then you will not be able to store dates like "03/14" in which year is missing. So instead I suggest to keep this as it is and probably have another column for storing the dateTime if you really need that.
Also have internal trigger to convert the datestrings from varchar column to dateTime column.
yes, you have to use the method STR_TO_DATE for your purpose.