How would I delete all the decimals from a column in SQL Server 2008?
If I have a column X_Coord and had three rows with the value, how would I trim it so that there are NO decimals after the last whole number?
For example, let's say my table is called RCMP, and the column is below:
X_Coord
---------------
- 5588790.77000
- 5588873.79000
- 5588943.71000
How would I remove the decimals in a single query?
I tried ROUND, but that ends up making the values appear as ie 5588790.00000.
I want it to appear as: 5588790.
Cast the decimal data type to an integer.
SELECT CAST(x_coord AS INT)
FROM dbo.RMCP
Edit: I have updated the code to reflect how to change the data type. This is a big impact change so be very careful. I would urge you to test this in development.
if object_id('#Demo') is not null
drop table #Demo;
go
create table #Demo(x_coord decimal(12,5))
insert into #Demo values(5588790.77000),(5588873.79000),(5588943.71000)
alter table #Demo
ALTER COLUMN x_coord INT NULL
select *
from #Demo
GO
--or this works
if object_id('#Demo') is not null
drop table #Demo;
go
create table #Demo(x_coord decimal(12,5))
insert into #Demo values(5588790.77000),(5588873.79000),(5588943.71000)
alter table #Demo
add new_x_coord INT NULL
UPDATE #Demo SET new_x_coord = CAST(x_coord AS INT)
GO
--********************** dont drop anything until you confirm the data is good!!!!!!!!!!!!! and test this in development *************************************
ALTER TABLE #Demo
DROP COLUMN x_coord
exec sp_rename 'dbo.#Demo.new_x_coord','x_coord','COLUMN'
select *
from #Demo
GO
Related
I need to change column type from tinyInt(used as bool) to Varchar, without loosing data.
I have found many answers on stack-overflow but all of them are written in postgres and I have no idea how to rewrite it in Mysql.
Answers for this problem on stack-overflow looks like that:
ALTER TABLE mytabe ALTER mycolumn TYPE VARCHAR(10) USING CASE WHEN mycolumn=0 THEN 'Something' ELSE 'TEST' END;
How would similar logic look like in Mysql?
The syntax you show has no equivalent in MySQL. There's no way to modify values during an ALTER TABLE. An ALTER TABLE in MySQL will only translate values using builtin type casting. That is, an integer will be translated to the string format of that integer value, just it would in a string expression.
For MySQL, here's what you have to do:
Add a new column:
ALTER TABLE mytable ADD COLUMN type2 VARCHAR(10);
Backfill that column:
UPDATE mytable SET type2 = CASE `type` WHEN 0 THEN 'Something' ELSE 'TEST' END;
If the table has millions of rows, you may have to do this in batches.
Drop the old column and optionally rename the new column to the name of the old one:
ALTER TABLE mytable DROP COLUMN `type`, RENAME COLUMN type2 to `type`;
Another approach would be to change the column, allowing integers to convert to the string format of the integer values. Then update the strings as you want.
ALTER TABLE mytable MODIFY COLUMN `type` VARCHAR(10);
UPDATE mytable SET `type` = CASE `type` WHEN '0' THEN 'Something' ELSE 'TEST' END;
Either way, be sure to test this first on another table before trying it on your real table.
I have 50 columns in the MySQL table. I want to sum all these columns and make a new column into the same table (sum50).
This should be stored permanently into the MySQL table whenever I update or insert new data.
I know how to sum up while performing the query but it does not store into the table permanently.
CREATE TRIGGER `name`
AFTER INSERT ON `table`
FOR EACH ROW
UPDATE `table` SET `table`.`coulms` = `table`.`col1`+.... `table`.`col50`+
I am trying the above trigger, but not working.
Should I have a blank column inserted into the table and perform trigger? Which trigger would be correct?
Instead of the trigger, add a generated column to your table:
alter table tablename add(sum50 int generated always as (col1 + col2 + ...) stored);
See a simplified demo.
I want to achieve the following use the following command to add a column to an existing table:
ALTER TABLE foo ADD COLUMN bar AFTER COLUMN old_column;
Can this option take substantially longer than the same command without the AFTER COLUMN option, as follows?
ALTER TABLE foo ADD COLUMN bar;
Will the first command use a greater amount of tmp table space during execution to perform the action?
Context: I have a very large table (think over a billion rows) and I want to add an additional column using the AFTER COLUMN option, but I don't want to be penalized too much.
Here's what I would do:
CREATE TABLE newtable LIKE oldtable;
ALTER TABLE newtable ADD COLUMN columnname INT(10) UNSIGNED NOT NULL DEFAULT 0;
I don't know the type of your column. I give an example with INT. Now here you can specify WHERE you want to add this new column. By default it will add it at the end unless you specify the AFTER keyword, if you provide it, you will have to specify in the order you will insert otherwise you need to put it at the end.
INSERT INTO newtable SELECT field1, field2, field3 /*etc...*/, newcolumn = 0 FROM oldtable;
OR, if you added it between columns:
# eg: ALTER TABLE newtable ADD COLUMN columnname INT(10) UNSIGNED NULL AFTER field2;
INSERT INTO newtable SELECT field1, field2, newcolumn = 0, field3 /*etc...*/ FROM oldtable;
You can add a where clause if you want to do them in batch.
Once all the records are there
DROP TABLE oldtable;
RENAME TABLE newtable to oldtable;
Create another table and alter the new table. ( like Book Of Zeus did )
And using ALTER TABLE newtable DISABLE KEYS and ALTER TABLE newtable ENABLE KEYS before and after the inserting query can make it faster. ( like below )
CREATE TABLE newtable ....;
ALTER TABLE newtable ....;
ALTER TABLE newtable DISABLE KEYS;
INSERT INTO newtable ....;
ALTER TABLE newtable ENABLE KEYS;
DROP TABLE oldtable;
While the other answers are useful as examples of the syntax required to add columns to a table, the answer to the actual question was provided by N.B.:
You'd get more CPU usage since records would have to be shifted.
From the memory usage point of view - it'd be the same with AFTER
COLUMN option and without it.
In most cases, a tmp table is created. There are MySQL engines that support hot schema changes (TokuDB being one) that don't create
the tmp table and waste tons of resources.
However, if you're doing this with MyISAM or InnoDB - I'd say that
"AFTER COLUMN" option will take slightly more time due to record
shifting.
– N.B.
Is it possible in mysql to create a table with a column that combines two column values? something like this:
create table test1 (
number1 int,
number2 int,
total int DEFAULT (number1+number2)
);
or like this :
CREATE TABLE `Result` (
`aCount` INT DEFAULT 0,
`bCount` INT DEFAULT 0,
`cCount` = `aCount` + `bCount`
);
It is not possible to do that exactly, but you can create a view based on a query that combines them:
CREATE VIEW `my_wacky_view` AS
SELECT `number1`, `number2`, `number1` + `number2` AS `total`
FROM `test1`;
I would avoid actually storing the combined data in a table, unless you're going to be running lots of queries that will reference the combined data in their WHERE clauses.
You can create a trigger on the table so MySQL calculates and automatically inserts that column value every time an INSERT happens on your test1 table. Make the table:
create table test1 (
number1 int,
number2 int,
number3 int
);
Then create a Trigger
CREATE TRIGGER triggername AFTER INSERT
ON test1
FOR EACH ROW
UPDATE test1 SET NEW.number3=NEW.number1+NEW.number2
MySQL documentation on triggers:
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Make sure to add the ON UPDATE trigger as well if you expect UPDATES to happen to the rows.
I had this issue as well. From Edgar Velasquez' answer here, and the answer to this question, I stumbled upon this incantation:
CREATE TRIGGER insert_t BEFORE INSERT
ON test1
FOR EACH ROW
SET NEW.number3=NEW.number1+NEW.number2;
CREATE TRIGGER insert_t_two BEFORE UPDATE
ON test1
FOR EACH ROW
SET NEW.number3=NEW.number1+NEW.number2;
This works for me on MySQL 5.6.22.
Little fix :
CREATE TRIGGER triggername BEFORE INSERT
ON test1
FOR EACH ROW
SET NEW.number3=NEW.number1+NEW.number2
How can i explode field value of table in select query ?
for e.g. i have 1 field in table named "coordinates" which contains latitude , longitude.
Now i want to use this latitude and longitude in select query.
Can i separate this values and use it in select query ?
Firstly, the comments are correct: this is a violation of normal form. Always store separate data in separate columns - it will make your life easier.
If you try to write a select statement that parses the coordinates field and tries to filter on one or both halves, you will have a query that runs SUPER slowly, since an index on that column will not function. Instead, I would recommend writing a query to split that column into two, such as the following:
alter table `your_table`
add column `coordinate_x` int null;
alter table `your_table`
add column `coordinate_y` int null;
update `your_table`
set `coordinate_x` = substring(`coordinates`,1,locate(',',`coordinates`))
,`coordinate_y`= substring(`coordinates`,locate(',',`coordinates`)+1);
alter table `your_table`
drop column `coordinates`;
alter table `your_table`
modify column `coordinate_x` int not null;
alter table `your_table`
modify column `coordinate_y` int not null;
You could then index coordinate_x and coordinate_y to make your select statement run quickly.