Found 4219 Articles for MySQLi

Convert hex string to number in MySQL?

karthikeya Boyini
Updated on 30-Jun-2020 13:51:10

1K+ Views

Use the CONV() method to convert hex string to number −select CONV(yourColumnName, 16, 10) AS anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> HexString varchar(100) -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('A'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('F'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('B'); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable values('ABC'); Query OK, 1 row affected (0.11 sec)Display ... Read More

Extract the middle part of column values in MySQL surrounded with hyphens and display in a new column?

Sharon Christine
Updated on 30-Jun-2020 13:52:20

175 Views

Use the SUBSTR() method to extract the middle part of column values surrounded with hyphens, for example, “11-84848-11”.Let us first create a table −mysql> create table DemoTable -> ( -> Number varchar(100), -> Number1 varchar(100) -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Number) values('11-84848-11'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Number) values('22-99999-22'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------+---------+ | Number ... Read More

How to set a string with hyphen and numbers in MySQL varchar?

Sharon Christine
Updated on 30-Jun-2020 13:53:58

3K+ Views

To set a string with hyphen and numbers, you need to use single quotes. For example, 'Customer-1234-899', 'Customer-9383-901', etc.Let us first create a table −mysql> create table DemoTable -> ( -> CustomerId varchar(100) -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Customer-1234-899'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Customer-8373-900'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Customer-9383-901'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTableOutputThis ... Read More

Change multiple columns in a single MySQL query?

Sharon Christine
Updated on 30-Jun-2020 13:54:52

462 Views

For this, use UPDATE and REPLACE() in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(100),    -> StudentCountryName varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentCountryName) values('John', 'US'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql select *from DemoTable;OutputThis will produce the following output −+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ |       ... Read More

MySQL query to get the next number in sequence for AUTO_INCREMENT field?

karthikeya Boyini
Updated on 30-Jun-2020 13:55:48

917 Views

Let us first create a table −mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+----+ | Id | +----+ | 1 ... Read More

MySQL query to remove special characters from column values?

karthikeya Boyini
Updated on 30-Jun-2020 13:56:49

1K+ Views

Let us first create a table −mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('STU#123'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('STU#567'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('STU#98494'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-----------+ | StudentId | +-----------+ | STU#123 | | STU#567 ... Read More

MySQL query to fetch record by year

Sharon Christine
Updated on 30-Jun-2020 13:34:59

334 Views

To fetch record by year, use the YEAR() method in MySQL −select *from yourTableName where year(yourColumnName)=yourYearValue;Let us first create a table −mysql> create table DemoTable -> ( -> CustomerName varchar(100), -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', '2019-01-21'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('Robert', '2018-02-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David', '2016-04-01'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL query to create table dynamically?

Sharon Christine
Updated on 30-Jun-2020 13:36:25

2K+ Views

For this, you can use stored procedure. Let us create a table dynamically with two columns i.e. StudentId as int, whereas StudentName as varchar −mysql> DELIMITER $$ mysql> CREATE PROCEDURE creatingDynamicTableDemo(yourTableName VARCHAR(200))    -> BEGIN    ->    SET @name = yourTableName;    ->    SET @st = CONCAT('    '>       CREATE TABLE IF NOT EXISTS `' , @name, '` (    '>       `StudentId` int UNSIGNED NOT NULL AUTO_INCREMENT,    '>       `StudentName` varchar(20) NOT NULL,    '>    PRIMARY KEY (`StudentId`)    '>    )    '> ');    -> PREPARE ... Read More

Searching for an integer value in a varchar field in MySQL?

Sharon Christine
Updated on 30-Jun-2020 13:37:17

176 Views

To search for an integer value in a varchar filed, you can use CASE statement.Let us first create a table. Consider, we have a list of email-ids −mysql> create table DemoTable -> ( -> Title varchar(100) -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('9John@example.com'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('3Carol@gmail.com'); Query OK, 1 row affected (0.45 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------------+ | Title ... Read More

MySQL query to get the count of all the elements in the field?

karthikeya Boyini
Updated on 30-Jun-2020 13:40:15

71 Views

For this, use the COUNT() method. Let us first create a table −mysql> create table DemoTable -> ( -> ProductName varchar(100) -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Product-1'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Product-2'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-3'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Product-3'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('Product-2'); Query OK, 1 row affected ... Read More

Advertisements