Convert and Store Date and Time in other coloumn in MySQL - php

I have database in MYSQL with ID, Req_Order_No(Varchar) , Req_In_Time(DateTime), Req_Out_Time(DateTime)
The Sample row is like below:
1 W0CH546 2014-07-23 09:32:00 2014-07-24 01:42:00
The above Date and Time are in EST format. I want to convert both of them and store in IST format in other columns
I tried SELECT CONVERT_TZ('Req_In_Time','-05:00','+9:30');
But it returns NULL Values.
Please help. Do I need php also?

The quotes around Req_In_Time cause the error.
SELECT CONVERT_TZ(Req_In_Time,'-05:00','+9:30');
Also, you should never store time information in localtime.
Use UTC/GMT.
You can always convert it to the proper localtime when you display it.
Note: Of course you need to specify the table-name as well:
SELECT CONVERT_TZ(Req_In_Time,'-05:00','+9:30') FROM YOUR_TABLE_NAME;
So you add another column (e.g. column xxx) to YOUR_TABLE_NAME.
Then you update the values.
UPDATE YOUR_TABLE_NAME
SET xxx = CONVERT_TZ(Req_In_Time,'-05:00','+9:30')
BTW, to add the column:
ALTER TABLE YOUR_TABLE_NAME ADD COLUMN `xxx` datetime NULL ;

Related

date between in mysql not correctly working

when I fetch data from table "like date from 01/09/2017 to 30/09/2017" then it's okey..
BUT When I am trying to fetch data from date 01/09/2017 to 01/10/2017 then its only showing the data of DATE 01/10/2017(not previous month data i.e 01/09/2017)
I am using MySQL Database.
SELECT * FROM `tablename` where date between '01/09/2017' AND '01/10/2017'
If you are saving the value as DATE format it should work. If not (you are saving the data as VARCHAR you can convert it to date and get the correct results.
STR_TO_DATE('01/09/2017', '%m/%d/%Y')
You need to store dates as DATE type not VARCHAR or TEXT.
Also DB dates are in the format YYYY-MM-DD usually so you will need to adjust your query accordingly.
Due to speed trying to use STR_TO_DATE is a terrible idea, better to convert once and then use MySQL as intended.
Backup your data first and then I (think) the following will work
BEGIN;
ALTER TABLE `tablename`
ADD COLUMN `new_date` DATE;
UPDATE `tablename`
SET `new_date` = STR_TO_DATE(`date`, '%d/%m/%Y');
ALTER TABLE `tablename`
DROP COLUMN `date`;
ALTER TABLE `tablename`
CHANGE COLUMN `new_date` `date` DATE;
COMMIT;
Step By Step -
Add an extra column to store the data temporarily
Update the table and copy the current date column value (formatted
DB friendly date) into the new temp column.
Remove the old column
Change the column name to the previous name so all existing queries work.
Then your query is as simple as
SELECT * FROM `tablename` where date between '2017-09-01' AND '2017-10-01'
According to your example you have stored date as text so you need to apply STR_TO_DATE() to perform date operations
Try below query:
SELECT * FROM `tablename` where STR_TO_DATE(date,'%d/%m/%Y')between
STR_TO_DATE('01/09/2017','%d/%m/%Y') AND STR_TO_DATE('01/10/2017','%d/%m/%Y');

Add new column into mysql from another column from select statement

I have a datetime() column date_time in my database.
I need to convert this to date() and create a new temporary column date_field till the session ends.
I've tried this:
$query1 = SELECT date_time, date(date_time) as date_field FROM table_name....
This works and creates a temp column date_field with all the date values for the date_time field
Problem : This only works for this query. I also have another query which searches for date_time field and thus results into a DB error Unknown field 'date_field'.
$query2 = SELECT date_field FROM table_name
| This table doesn't have date_field as a column
Can this field be created so that I can use this for a time till the session ends ? as we do for temporary tables ?
Note: I can't use ALTER to add new column here due to code limitations.
Any ideas ?
Thanks!
In the first query, you dont really create any column, you just return a a column (date_time), run some function on it (date()) and give it an alias name (date_field). This happens only in the result set, and abvioysly cant be used outside of this query.
What you need to do is simply use the same in the second query: instead of select date field you need select date(date_time) as date_field

Insert users join date into the database

How would I go about adding the current date into the database (MySQL) when a user registers? I know I would need to add a row in the database (Join_date or something), what would I set that to when creating that row? timestamp? varchar?
I want to make it so when the user submits the registration form it adds the current date to the join date row.
Any help would be greatly appreciated.
Thanks.
Suppose structure is
field type
++++++++++++++++++++++
id INT
username varchar(10)
password varchar(10)
date datetime
Then use below query
INSERT INTO Table values (1,'username','password',NOW())
Hope this helps you...
Column type should be datetime
and while inserting row, use NOW() in query
Add a datetime column, and use NOW()doc to set the column when inserting a record.
This was asked many times.
See this question: Registration date . Its solution can be used well; it will automatically insert date.
Add a column TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Make that field either MySQL internal date/time type (see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html) and use CURRDATE (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html) to fill it, or simply make it int and fill it with POSIX time with time().
Personally I find the Unix timestamp better since it's easier to operate and it doesn't depend on the type of db.
You can use a code like this
INSERT INTO users (LastName, FirstName, DateColumn)
VALUES ("firstname", "lastname", NOW())
NOW() would insert the current date in the DateColumn field
try this:
INSERT INTO users (`LastName`, `FirstName`, `DateColumn`)
VALUES ("Fname", "LNamee", NOW())
assuming that you are using mysql, you can use field type DATETIME or TIMESTAMP.
usually I use DATETIME for created ("join date") fields and TIMESTAMP for lastupdated field, since it can be (easily) populated transparently by the RDBMS on update.
To get the current date you can use mysql function NOW().
for example:
INSERT INTO Users (`firstname`, `regdate`) VALUES ('herbie', NOW());

Getting the result of a MySql query order by date when the date field can contain strings differents than dates

I have a date field on my Sql table that is actually a text field, thats because there sometimes I save dates like 10/28/2011 and sometimes strings like present.
It is possible without touching the table structure, and maybe just with the sql query having the result correct organanized by date? Where present is the max value and then the dates in decreasing order.
If you really can't sort the data type out on those string date columns then this might help:
select t.*,
case when date_end = 'present' then curdate()
else convert(concat(substr(date_end,7,4),'-',substr(date_end,1,2),'-' ,substr(date_end,4,2)),date) end as "realDate"
from myTable t
order by "realDate" desc;
Right. I'm off to go and say 100 Hail Marys to the MySQL database god now. Ugh.
Best way would be :
fix the db and add proper date fields (add proper date or datetime field and update values from text fields and format them to be dates on the fly)
keep your present status in a text field and you can use that as a extra condition
I know you asked for a way to sort it correctly as a text column, but I really don't think that's the right way to this.
I agree with #stivlo. It is very easy to update the database to deal with this is a better way. You can either:
Convert the column to a real date, and use NULL to represent "present" instead of the string "present".
Or, if you need NULL reserved for some other meaning, you can convert the column to a real date, and add additional boolean flag columns to specify when the dates are "present".
Either way, you should definitely convert those text fields to real dates.
select job_desc, data_begin, data_end from table where data_end = 'present'
union
select job_desc, data_begin, cast(data_end as date) data_end from table where data_end <> 'present' order by data_end desc
Not sure if syntax is completely correct, so you might want to check the mySql 'union' syntax
Another solution might be using coalesce for 'present':
select cast(coalesce(data_end,now()) as date) data_end from table order by data_end desc
this way the column gets interpreted as dates and 'present' gets replaced with the present date

Issue in fetching record from database

I have a field name "date" datatype "char" and value in thid field is "04-08-2011 04:47:08 EDT"
but when i want to fetch the records from our data base basis on date like
select * from table_name where date="04-08-2011 04:47:08 EDT"
but i dont get any result for this query,any one can help me
Try using the LIKE statement, because if your field has X chars and you insert X-3 chars, the 3 left chars are filled with spaces, so modify your query to be like:
select * from table_name where date LIKE "04-08-2011 04:47:08 EDT%"
EDIT: date is a field type in MySQL, so this query isn't valid. If your field is 'date', you need to put it between ``, so the query will be like this:
select * from table_name where `date`="04-08-2011 04:47:08 EDT"
If you're using the datatype char then any characters that are under the set length of the column will be right padded with spaces.
e.g.
char(30) would pad like so:
04-08-2011 04:47:08 EDT <-- spaces up to arrow
Use a date datatype or varchar (if you must).
In response to comment:
Have you run this query from phpMyAdmin? If so, does it return any rows, if not, try.

Categories