This is my table structure
CREATE TABLE `stock_historic` (
`name` varchar(30) DEFAULT NULL,
`date` varchar(30) DEFAULT NULL,
`open` decimal(8,2) DEFAULT NULL
) ;
Parsing from an csv file , i have inserted data into the table this way
Insert Into stock_historic values('SRF', '20150930' , '11190.00');
Insert Into stock_historic values('SRF', '20151001' , '11456.00');
But from the front end the (Jquery date picker) date comes in this format
select * from stock_historic where symbol = "SRF.NS" and startDate = "2015-09-30" and endDate = "2015-10-01"
http://sqlfiddle.com/#!9/91ac8d/1
Please let me know how can i query the database in this case
Format the date of jQuery date picker, remove the dashes. For example:
$( "#datepicker" ).datepicker({
"dateFormat": "yymmdd"
});
I think it will be better formatting in javascript than in query, you can put it directly in your queries.
alter your table structure and give the date column another name (date is a reserved word) and an appropriate column type like timestamp or date
CREATE TABLE `stock_historic` (
`name` varchar(30) DEFAULT NULL,
`opendte` timestamp DEFAULT NULL,
`open` decimal(8,2) DEFAULT NULL
) ;
now you don't have to bother about formats as long as its not ambigous. All of your examples should work.
Related
I create table for hour
CREATE TABLE hour (
Name1 varchar(25) not null,
Datee Datetime not null DEFAULT (CURRENT_TIMESTAMP()),
Monthh date not null DEFAULT (MONTH(CURRENT_DATE()))
);
Mysql only give me 0000-00-00 not Name where i use button in php. What is wrong with this and how correct? In my opinion my phpadmin dont have a MONTH function
CREATE TABLE hour ( Name1 varchar(25) not null,
Datee DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
Monthh TINYINT NOT NULL DEFAULT (MONTH(CURRENT_TIMESTAMP))
);
fiddle
PS. HOUR is Reserved word - I'd recommend to rename a table.
Store full timestamp and it's month part in separate columns in same table is absolutely redundant. You can story only datetime and get it's month in select query:
CREATE TABLE Godzina (
Nazwa varchar(25) not null,
Randka Datetime not null DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO Godzina (Nazwa) VALUES ('TestName');
SELECT
Nazwa,
Randka,
MONTH(Randka) AS Miesiac
FROM Godzina;
Test it on SQLize.online
have a string field in DB which I need to convert to datetime, my problem is I need to convert date format from 03/01/2019 11:30 to 2011-01-26 14:30:00. I can do it with an individual record, but can't figure out how to convert all the records in the table.
I followed the advice here
but, all converted dates are 0000-00-00 00:00:00 presumably because mysql not able to guess input date format.
create table wms_lw.wms_lw (
site_id int null,
name varchar(255) null,
site varchar(255) null,
date varchar(255) null,
new_date datetime not null,
value double null,
date_2 varchar(255) null
);
You can convert your string in date as
select str_to_date('03/01/2019 11:30', '%d/%m/%Y %i:%s')
so from your table
select str_to_date(my_col, '%d/%m/%Y %i:%s') from my_table
once you have a date you can format as you prefer wit date_format() or add the datte time using date_Add()
Hi I have a log database table in mysql which captures a start date and time and an end date and time.
The start and now the stop time is inserted to the record.
I have a third field which is duration which I would like displayed as hh:mm:ss
The schema looks like this at present
CREATE TABLE IF NOT EXISTS `log` (
`uid` int(20) NOT NULL AUTO_INCREMENT,
`room` int(11) NOT NULL,
`start` datetime NOT NULL,
`stop` datetime NOT NULL,
`duration` datetime DEFAULT NULL,
`participants` int(3) NOT NULL,
`recorded` varchar(3) NOT NULL,
`rec_file` varchar(35) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Log File' AUTO_INCREMENT=106 ;
I'm trying to create a trigger which calculates the duration and writes it to the duration field as data is inserted
Currently the trigger I am trying to debug looks like this
BEGIN
SET NEW.duration = (TIMEDIFF (NEW.start,NEW.stop)) ;
END
The result is the duration field remains set to 0000-00-00 00:00:00.000000
Any suggestions on how to make this work are greatfully recived
Try to add the following :
Change duration type to TIME instead of DATETIME. According to documentation, the result returned by TIMEDIFF() is limited to the range allowed for TIME values.
This is the reason you are receiving all zeros currently.
I assume that stop time will always be later than start time, so I would write the trigger in the following way:
CREATE TRIGGER your_schema.insert_duration BEFORE INSERT ON your_schema.log
FOR EACH ROW SET NEW.duration = TIMEDIFF(NEW.stop, NEW.start);
The result will be in HH:MM:SS format for duration field.
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`user` varchar(9) NOT NULL,
`post` text NOT NULL,
`date` varchar(14) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM DEFAULT CHARSET=utf8;
table created
INSERT INTO `posts` ( 'user', 'post', 'date') VALUES
('she', 'dolphin', '???');
('me', 'chandra'), '???');
Instead of ??? I need current datetime in the following format:day.month. hour:min
For example for today and now: 03.01. 00:34
That a bad idea, because you are storing the data
in a format that can't be ordered
in a format that mySQL can't optimize in any meaningful way
without the year of the date
Use a proper mySQL DATETIME field instead, and format the output using DATE_FORMAT when you make the query:
SELECT DATE_FORMAT(`date`, "%d.%m. %H:%i") FROM `posts`;
I Wanted to initialize default date field in my table with CURRENT_DATE(); well BENCH gives me errors that is not possible i heard about triggers but it seems to be a little over complicatet for that problem so is there any way to make this in such way
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data date default CURRENT_DATE(),
check (Data >= now()),
....
);
You can initialize a TIMESTAMP column with this:
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP
or a DATETIME column (MySQL 5.6+):
Data DATETIME DEFAULT CURRENT_TIMESTAMP
but if you want to initialize a DATE column using MySQL 5.5 you need an INSERT TRIGGER:
CREATE TRIGGER setdate_before_insert BEFORE INSERT ON test
FOR EACH ROW
SET NEW.Data = CURDATE();
and maybe you need also an UPDATE trigger? Please see fiddle here.
Another way to go about this, if you do not mind changing the date type, would be to use a TIMESTAMP and initialize it with TIMESTAMP DEFAULT CURRENT_TIMESTAMP. Your definition would then become:
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP, <<<==== change this
check (Data >= now()),
....
);
If you have MySQL version 5.6.5 and above, you can use CURRENT_TIMESTAMP instead of CURRENT_DATE
See http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
So you can't do it with dates but you may be able to change your field to be a TIMESTAMP or DATETIME and Bobs your mothers brother!