i have one field which contain data like 4563******3245. when i execute my sql query it is inserted successfully. but in mysql database it is showing only 4563
my sql query is:
insert into mytable ('myfield') values ('4563******3245');
Can any one tell me where is the problem.
thank you.
insert into mytable ('myfield') values ('4563******3245');
it is working fine
make sure your column type should be varchar
You can't store text in a column of a number data type. You have to change your data type to char(14).
ALTER TABLE your_table MODIFY myfield CHAR(14);
If your running that particular query, and only 4563 is being inserted, it would suggest your column type for "myfield" is set as some variant or length of "int" rather than char(22) (CCN's are not always 16 digits, some can be 20 or 22).
you should probably switch the column type to make sure the data inserts correctly to something like
ALTER TABLE mytable MODIFY myfield char(22);
Related
I have a database with a column named "price" - within this column there are prices rounded to 5 decimal places, e.g 62.083333 - I would like to run a command to change every value within "price" column to just 2 decimal places, e.g 62.08
Is there a simple command to run on this column? We have over 36,000,000 values so would prefer not to have to update them all manually! :)
Would this command be the one?
SELECT CONVERT(DECIMAL(20,2),price)
Here's a similar question: mass update mysql table decimal value and change column field type
You can use
ALTER TABLE '<name>' CHANGE COLUMN '<name>' DECIMAL(20,2)
Simply alter table and modify data type
ALTER TABLE [TableName] MODIFY COLUMN [ColumnNameHere] decimal(20,2)
Try ,
UPDATE table_name SET price = round(price,2) where your
conditions;
I've a date column in table1 filled with character data that looks as follows:
InDate:
2022015
2012015
2122014
2112014
2102014
2092014
I've converted that data to following format:
InDate:
02-02-2015
02-01-2015
02-12-2014
02-11-2014
02-10-2014
02-09-2014
by using the following command:
update table1
set InDate = DATE_FORMAT(str_to_date(InDate, '%d-%m-%Y'), '%d-%m-%Y');
But obviously this only changed the look of the string data in the InDate column. Now, this still didn't suffice my situation and I needed the column itself to be in Date type.
So, I created a new empty table called table2 with the same structure as table1 (except for InDate column, which I now declared as Date type) and tried copying the old data (which is having InDate in my required format (02-02-2015)).
INSERT INTO table2
SELECT *
FROM table1;
But I'm getting the following error message:
Error Code: 1292. Incorrect date value: '02-02-2015' for column 'InDate' at row 1
I need this column to be in Date type with the data intact. This table has millions of records that I need to work with. I really need to get around this error.
Please help!
This code block maybe help use :
Not update table1.
insert into table2
(indate)
select date_format(str_to_date(indate
,'%d-%m-%Y')
,'%d-%m-%Y')
from table1;
Please try this.
INSERT INTO table2(`InDate`)
SELECT CONCAT('\'',InDate,'\'')
FROM table1;
Hope this helps.
For example i have table with a different field names(column), lets say 5 columns and all of them are empty. And i wanted to insert data in one specific column. Is it possible? I'm looking for example of this, but unlucky to find one. Most of insert into statements examples required all columns to be filled. If possible, can you give me the correct syntax? I'm sorry if i'm lacking research or it's already been asked, it's ok if you will redirect me to the link.
If you want insert on column3, leaving empty the other:
INSERT INTO table_name (column1,column2,column3,column4,column5)
VALUES ("","","VALUE","","");
The other part of program would UPDATE the other columns:
UPDATE table_name
SET column1=value1,column2=value2,column4=value4,column5=value5
WHERE some_column=some_value;
The documentation on how to construct an INSERT INTO statement is here: INSERT INTO Statement (Microsoft Access SQL).
But basically, you just need to be explicit about which columns you want to insert values for, and omit the other ones, like this:
INSERT INTO table (colname) VALUES ('colvalue')
What happens to the fields you omit? The documentation says:
When you do not specify each field, the default value or Null is inserted for missing columns.
I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image.
I need to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate my string in the query statement.
Thanks in advance.
I would keep the id and imgname independent of each other and combine the two on SELECT when needed. If the need is frequent enough, create a view.
Have a look at LAST_INSERT_ID() function. If performance is not an issue, INSERT regularly, and then UPDATE using LAST_INSERT_ID(), like:
UPDATE table SET name = CONCAT(name, "-", LAST_INSERT_ID()) WHERE id = LAST_INSERT_ID();
I am looking for a way to change the datatype of a column. Currently, in my database, the date columns types were defined as varchar and I need to convert them back to the date type.
Any idea how to do it?
You will need to adapt this based your your exact table structure but something like;
CREATE TABLE temp (startdate varchar(255), stuff varchar(255));
INSERT INTO temp
SELECT startdate,stuff
FROM mytable;
TRUNCATE TABLE mytable;
ALTER TABLE mytable ALTER COLUMN startdate DATETIME NOT NULL;
INSERT INTO mytable
SELECT CAST(startdate AS DATETIME), stuff FROM temp;
DROP TABLE temp;
First, create the new column with type data
Next, run update query, to populate the new column with the value of the old one, applying any conversion if needed
Next, drop the old column
Finally, rename the new column to the old one
Create a new DATE column with a temporary name
Populate the new column with an UPDATE query that makes use of STR_TO_DATE()
If everything's right, remove the VARCHAR column and rename the DATE column.
Mysql default date format is : YYYY-MM-DD . If your try to insert the date otherwise, as you actually did, the date will be inserted with these values : 000-00-00, giving you a hint to the acceptable date format for mySql.
Wanna share this for SQL server users. For me this method is much convenient and safer.
In your Table create new column "NewDate" (temporarily or name whatever you want).
Make sure no invalid Datetime format in the Table you want to convert. Try those formats here: https://www.w3schools.com/sql/trysqlserver.asp?filename=trysql_func_sqlserver_cast3 <--- you need to check thoroughly otherwise there would be an error executing the command below.
Execute this command:
UPDATE myTable
SET NewDate = CAST(OldDate AS datetime)
WHERE (OldDate <> '') AND (OldDate IS NOT NULL) --to make sure you cast only what is needed otherwise there would be an error.
You can now delete the old column i.e. "OldDate".
Finally you can drag and drop the new table you've created to the slot where you just deleted the old column in the table design.
If the field of your column is VARCHAR and stored date as DD-MM-YYYY then we have to convert the date in YYYY-MM-DD format by following PHP code.
$cd = array();
$cd1 = array();
$cdf = array();
$getdata = mysqli_query($link,"SELECT columnname FROM tablename");
while($row=mysqli_fetch_array($getdata))
{
$cd = $row['columnname'];
$cd1 = strtotime($cd);
$cdf = date('Y-m-d',$cd1);
mysqli_query($link,"UPDATE tablename SET columnname =
REPLACE(columnname,'$cd','$cdf')");
}
After running this PHP code, in your MySQL table change the datatype of your column to 'DATE'.
It works for me without losing or truncate data.