
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to cast and update a numeric value from string column only where applicable in MySQL?
You can use the CEIL() function from MySQL. Let us first create a table. Here, we have taken the first column as VARCHAR −
mysql> create table DemoTable -> ( -> Value varchar(20), -> UpdateValue int -> ); Query OK, 0 rows affected (1.08 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value) values('100'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Value) values('false'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Value) values('true'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Value) values('1'); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------------+ | Value | UpdateValue | +-------+-------------+ | 100 | NULL | | false | NULL | | true | NULL | | 1 | NULL | +-------+-------------+ 4 rows in set (0.00 sec)
Following is the query to cast and update a numeric value from string column only where applicable −
mysql> update DemoTable -> set UpdateValue=ceil(cast(Value AS char(7))); Query OK, 4 rows affected (0.18 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------------+ | Value | UpdateValue | +-------+-------------+ | 100 | 100 | | false | 0 | | true | 0 | | 1 | 1 | +-------+-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Update only a single column value in MySQL
- How to select only non - numeric values from varchar column in MySQL?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- Update a column value, replacing part of a string in MySQL?
- How to select distinct value from one MySQL column only?
- Replace only a specific value from a column in MySQL
- How to update MySQL column with random value?
- How to separate string and a numeric value in R?
- MySQL query to select rows where column value is only 0, group by another column?
- How do you select from MySQL where last value in a string = x?
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- How to prepend a string to a column value in MySQL?
- How to add a string before each numeric value in an R data frame column?
- Updating only a single column value in MySQL?
- Java Regex to extract maximum numeric value from a string

Advertisements