I have two strings "0.31" and "0.0076" and they need to be stored in a decimal(10,2) column in MySQL. How do I do this conversion in ruby but not in mysql directly
try using CAST
SELECT CAST(colName AS DECIMAL(10,2))
FROM tableName
SQLFiddle Demo
use MySql conversion functions CAST or CONVERT . Read Here
Select CAST(columnName as DECIMAL(10,2))
or
Select CONVERT(columnName,DECIMAL(10,2))
OR you can do the job like
select format(columnname, 0) as formated from tablename where condition
Related
I am converting my SQL database to MYSQL and I have problem using cast in mysql, please help me to convert this SQL query to mysql
Here col1 is type of varchar (storing values(01,02,03,04))
select * from table1 order by cast(col1 as numeric)
Try using the data type SIGNED instead:
select *
from table1
order by cast(col1 as SIGNED)
SQL Fiddle Demo
How can I convert a string/varchar like '12-Mar-2013' to date like 2013-03-12?
I tried
SELECT STR_TO_DATE('12-Mar-2013','%Y-%m-%d');
SELECT DATE_FORMAT('12-Mar-2013','%Y-%m-%d');
but both return null.
My current database version is 5.5.7-rc.
Use STR_TO_DATE.
Try
SELECT STR_TO_DATE('12-Mar-2013','%d-%M-%Y');
-> 2013-03-12
This should do the trick
SELECT DATE_FORMAT(STR_TO_DATE('12-Mar-2013','%d-%b-%Y'), '%Y-%d-%m');
I have this query in mysql
SELECT *
FROM `calendar`
WHERE DATE_FORMAT(startTime, "%Y-%m-%d") = '2010-04-29'
How can i convert to Postgresql query?
Basically, the query in MYSQL which uses DATE_FORMAT() converts date into string. If you want to compare it with date, don't use DATE_FORMAT() but instead DATE(). Try this, in PostgreSQL, casting timestamp into date,
SELECT *
FROM "calendar"
WHERE "startTime"::date = '2010-04-29'
SQLFiddle Demo
other source
SELECT *
FROM calendar
WHERE starttime::date = '2010-04-29'
I am guessing this is relatively simple to do, but I am unsure of the syntax. I have date and time columns that I want to combine to a timestamp column. how would I query this using a select?
Or you could use the built-in TIMESTAMP(date,time) function.
So then you would do something like this say from an Orders table...
SELECT OrderNumber, TIMESTAMP(date,time) as OrderTS, SalesPersonID
FROM Orders
Mysql does not seem to have a constructor for datetime such as datetime('2017-10-26', '09:28:00'). So you will have to treat the component part as string and use string concatenation function (Note mysql does not have the || operator for string concatenation). If you want the datetime type, you will have to cast it.
concat(datefield,' ',timefield) as date
select cast(concat('2017-10-26', ' ', '09:28:00') as datetime) as dt;
If it possible to use built-in function, just use it.
Any way here is an example to find records between given timestamps.
SELECT `id` FROM `ar_time` WHERE TIMESTAMP(`cdate`,`ctime`) BETWEEN fromTimeStamp AND nowTimeStamp;
For 24hr time
TIMESTAMP(Date, STR_TO_DATE(Time, '%h:%i %p'))
SELECT * FROM tablename WHERE TIMESTAMP(datecol, timecol) > '2015-01-01 12:00:00';
O.P. did say SELECT but in case anyone wants to add a timestamp column:
ALTER TABLE `t` ADD COLUMN `stamp` TIMESTAMP;
UPDATE `t` SET `stamp` = STR_TO_DATE(CONCAT(`Date`, ' ', `Time`), '%m/%d/%Y %H:%i:%s');
Adjust format strings to taste.
concat('2021-12-31', ' ', '07:00:00')
it worked in an INSERT procedure.
I have a UNIX-type timestamp stored in an INT column in MySQL. What is the proper way to retrieve this as a MySQL DATETIME?
(I found the answer when re-scanning the MySQL Date functions, but didn't see the answer on SO. Figured it should be here.)
FROM_UNIXTIME()
select from_unixtime(column,'%Y-%m-%d') from myTable;
SELECT FROM_UNIXTIME(mycolumn)
FROM mytable
The function STR_TO_DATE(COLUMN, '%input_format') can do it, you only have to specify the input format.
Example : to convert p052011
SELECT STR_TO_DATE('p052011','p%m%Y') FROM your_table;
The result : 2011-05-00
This works for sure.
select from_unixtime(column) from table