Found 4219 Articles for MySQLi

Convert DateTime Value into String in MySQL?

George John
Updated on 30-Jul-2019 22:30:24

2K+ Views

To convert the DateTime value into string in MySQL, you can use the DATE_FORMAT() function. The syntax is as follows −select date_format(yourColumnName, ‘%d %m %y’) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The query to create a table is as follows −mysql> create table DateAsStringDemo -> ( -> YourDateTime datetime -> ); Query OK, 0 rows affected (0.57 sec)Inserting the date with the help of curdate() method. The query to insert date is as follows −mysql> insert into DateAsStringDemo values(curdate()); Query OK, 1 row affected ... Read More

How to check if field is null or empty in MySQL?

Arjun Thakur
Updated on 26-Jun-2020 13:10:51

3K+ Views

To check whether a field is null or empty in MySQL, use the IF() function in MySQL. The syntax is as follows −SELECT IF(yourColumnName IS NULL or yourColumnName = '', 'NULLId', yourColumnName) as anyVariableName from yourTableName;To understand the above syntax, let us create a table. The following is the query to create a table −mysql> create table NullAndEmptyDemo -1> ( -> Id varchar(200) -> ); Query OK, 0 rows affected (0.66 sec)Let us now insert records into the table with the help of insert command. The query to insert records in the table is as follows. We have added null ... Read More

How to change the default charset of a MySQL table?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

696 Views

To change the default charset of a MySQL table, you can use the below syntax. The syntax is as follows −alter table yourTableName convert to character set yourCharsetName;Let us create a table and apply the above syntax to change the default charset. The query to create a table −mysql> create table CharsetDemo -> ( -> Id int, -> Name varchar(200), -> Age int -> ); Query OK, 0 rows affected (0.73 sec)Now you can change the charset of a table. The following is the query ... Read More

MySQL Server port number?

George John
Updated on 30-Jul-2019 22:30:24

691 Views

If you will install MySQL on your system, then you will get the default MySQL server port number i.e. 3306.To know the MySQL server port number, you can use the following query. Here, we have used the SHOW VARIABLES command. The query is as follows −mysql> SHOW VARIABLES WHERE Variable_Name = 'port';The following is the output −+---------------+-------+ | Variable_Name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.04 sec)

Select into in MySQL?

Chandu yadav
Updated on 26-Jun-2020 13:12:11

1K+ Views

To do select into in MySQL, use CREATE TABLE SELECT command. The syntax is as follows −CREATE TABLE yourTableName SELECT *FROM yourOriginalTableName;To understand, let us first create a table −mysql> create table SelectIntoDemo -> ( -> Id int, -> Name varchar(200) -> ); Query OK, 0 rows affected (0.50 sec)Let us insert some records into the table with the help of insert command. The query is as follows −mysql> insert into SelectIntoDemo values(1, 'Bob'), (2, 'Carol'), (3, 'David'); Query OK, 3 rows affected (0.15 sec) Records: 3 Duplicates: 0 Warnings: 0Displaying all records with the help of select statement. The ... Read More

MySQL SELECT last few days?

Arjun Thakur
Updated on 26-Jun-2020 13:14:03

589 Views

To select last few days, use DATE_ADD() function in MySQL. The syntax is as follows −select date_add(curdate(), interval - anyIntgegerValue day);Or you can DATE_SUB() from MySQL.select date_sub(curdate(), interval anyIntgegerValue day);Or you can use the following syntax −select curdate() - interval anyIntgegerValue day;Here is the example of all syntaxes shown above to select last few days.Case 1 − Use of DATE_ADD() functionThe query is as follows −mysql> select date_add(curdate(), interval -6 day);Here is the output −+-------------------------------------+ | date_add(curdate(), interval -6 day) | +-------------------------------------+ | 2018-11-20                          | +-------------------------------------+ 1 row ... Read More

View stored procedure/function definition in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

2K+ Views

To view stored procedure/function definition in MySQL, you can use show command. The syntax is as follows −SHOW CREATE PROCEDURE yourProcedureName;To understand the above syntax, you can create a procedure and check that definition. Let us create a stored procedure −mysql> delimiter // mysql> create procedure AllRecords()    -> begin    -> select *from student;    -> end // Query OK, 0 rows affected (0.24 sec)You can call the stored procedure with the help of call command. The query is as follows −mysql> delimiter ; mysql> call AllRecords();The following is the output −+------+-------+ | id   | Name  | +------+-------+ ... Read More

Calculate age based on date of birth in MySQL?

George John
Updated on 30-Jul-2019 22:30:24

11K+ Views

Calculate Age based on date of birth with the help of DATE_FORMAT() method in MySQL. Firstly, get the current date time with the help of now() method and you can place your date of birth in DATE_FORMAT().The syntax is as follows −SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(now(), 'yourDateofbirth')), '%Y')+0 AS anyVariableName;Apply the above syntax to calculate age from yourDateofbirth. In the above syntax, replace yourDateofbirth with your date of birth. The query is as follows −SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(now(), '2010-11-25')), '%Y')+0 AS Age;The following is the output −+------+ | Age | +------+ | 8 | +------+ 1 row in set (0.00 sec)Let ... Read More

How to implement ternary conditional operator in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

2K+ Views

A ternary conditional operator looks like ?: in programming language like C, C++, Java etc. The syntax is as follows −(yourCondition) ? statement1:statement2;In the above syntax, if yourCondition becomes true then statement1 will evaluate and if yourCondition becomes false then statement2 will evaluate.But the above syntax does not work in MySQL. We can use IF() function from MySQL for the same purpose.Let us see an example −Case 1mysql> select if(3 > 5, 'Condition is true', 'Condition is not true') as ConditionalResult;The following is the output in which second statement evaluates since is 3 isn’t more than 5 −+-----------------------+ | ConditionalResult ... Read More

Is there a way to know your current username in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

7K+ Views

Yes, you can use the method CURRENT_USER() to know the current username in MySQL.The above method returns the username that can be used to authenticate the client connection.The query is as follows −mysql> select CURRENT_USER();The following is the output −+----------------+ | CURRENT_USER() | +----------------+ | root@% | +----------------+ 1 row in set (0.00 sec)Or you can use USER() method from MySQL. The query is as follows −mysql> select user();Here is the output −+----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)

Advertisements