Found 6702 Articles for Database

MySQL DATE_FORMAT '%M' is used for short month?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

71 Views

The %M Date Format is not used for displaying short months like Jan for January, Feb for February, etc. You need to use DATE_FORMAT() function with %b format for short month. The syntax is as follows:SELECT DATE_FORMAT(yourColumnName, '%d-%b-%y') AS anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table DateFormatMonthDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> ShippingDate date,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command. The ... Read More

Add 30 days to a value in a MySQL table?

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

545 Views

To add 30 days to a value in the table, you can use ADDDATE() function with UPDATE command. The syntax is as follows:UPDATE yourTableName SET yourDateColumnName=ADDDATE(yourDateColumnName, INTERVAL 30 DAY);To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table Add30DayDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> ShippingDate date,    -> PRIMARY KEY(ID)    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into Add30DayDemo(ShippingDate) values('2019-02-04'); Query OK, 1 row affected ... Read More

SELECT not null column from two columns in MySQL?

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

8K+ Views

There are lots of ways to select NOT NULL column from two columns. The syntaxes are as follows:Case 1: Use IFNULL() function.The syntax is as follows:SELECT IFNULL(yourColumnName1, yourColumnName2) as anyVariableName from yourTableName;Case 2: Use coalesce() function.The syntax is as follows:SELECT COALESCE(yourColumnName1, yourColumnName2) as anyVariableName from yourTableName;Case 3: Use CASE statement.The syntax is as follows:SELECT CASE WHEN yourColumnName1 IS NOT NULL THEN yourColumnName1 ELSE yourColumnName2 END AS anyVariableName FROM yourTableName;Case 4: Use only IF().The syntax is as follows:SELECT IF (yourColumnName1 ISNULL, yourColumnName2, yourColumnName1) AS NotNULLValue FROM SelectNotNullColumnsDemo;To understand the above syntax, let us create a table. The query to create a ... Read More

In MySQL how to replace all NULL values in a particular field of a particular table?

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

215 Views

To replace all NULL values in a particular field of a particular table, use UPDATE command with IS NULL property. The syntax is as follows:UPDATE yourTableName SET yourColumnName=”yourValue’ WHERE yourColumnName IS NULL;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table Employee_Information_Table    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Salary int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query to insert record is as ... Read More

MySQL Query to remove all characters after last comma in string?

Ankith Reddy
Updated on 30-Jun-2020 07:40:45

3K+ Views

To remove all characters after the last comma in the string, you can use SUBSTRING_INDEX().If you do not know the location of the last comma, then you need to find the last comma dynamically using LENGTH().The syntax is as follows −UPDATE yourTableName set yourColumnName = SUBSTRING_INDEX(yourColumnName, ', ', LENGTH(yourColumnName) - LENGTH(REPLACE(yourColumnName, ', ', '')));To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table RemoveAllCharacters    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> FullInfo varchar(200),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 ... Read More

MySQL query to find a list of city names that do not start with vowels?

Arjun Thakur
Updated on 30-Jun-2020 07:43:05

11K+ Views

You can use DISTINCT with RLIKE operator to find a list of city names that do not start with vowels.The syntax is as follows −SELECT DISTINCT yourCityColumnName FROM yourTableName WHERE yourCityColumnName NOT RLIKE ‘ ^[AEIOUaeiou].*$’;To understand the above syntax, let us create a table. Here, we have a column for city names.The query to create a table is as follows −mysql> create table Employee_Information    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> EmployeeName varchar(20),    -> CityName varchar(20),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the ... Read More

How can I change root username in MySQL?

George John
Updated on 30-Jun-2020 07:45:45

3K+ Views

To change the root username in MySQL, you need to use UPDATE and SET command. The syntax is as follows −UPDATE user set user = ’yourNewUserName’ WHERE user = ’root’;To understand the above syntax, let us switch the database to MySQL using USE command.The query is as follows to switch the database.mysql> use mysql; Database changedNow list all the users from MySQL.user table. The query is as follows −mysql> select user from MySQL.user;The following is the output −+------------------+ | user             | +------------------+ | Manish           | | User2     ... Read More

Count values from comma-separated field in MySQL?

Ankith Reddy
Updated on 30-Jun-2020 07:51:30

3K+ Views

You can count values from comma-separated field using CHAR_LENGTH() method from MySQL. The syntax is as follows −SELECT *, (CHAR_LENGTH(yourColumnName) - CHAR_LENGTH(REPLACE(yourColumnName, ', ', '')) + 1) as anyVariableName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table CountValuesCommaSeparated    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> CommaSeparatedValue text,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (1.76 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into CountValuesCommaSeparated(CommaSeparatedValue) values('101, 104, ... Read More

Update an entire row in MySQL?

Arjun Thakur
Updated on 30-Jun-2020 07:54:16

2K+ Views

To update an entire row in MySQL, use UPDATE command. You need to know the primary key column. The syntax is as follows to update an entire row.UPDATE yourTableName SET yourColumnName1 = ’yourValue1’ ,yourColumnName2 = ’yourValue2’ ,    yourColumnName3 = ’yourValue3’ ,.......................N    WHERE yourPrimaryKeyColumnName = yourValue;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table UpdateEntireRowDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Age int,    -> Marks int,    -> PRIMARY KEY(Id)    -> ); Query OK, ... Read More

What are the difference ways to replace nulls values in MySQL using SELECT statement?

George John
Updated on 30-Jun-2020 07:57:57

901 Views

There are lots of options available to replace NULL values using select statement. You can use CASE statement or IFNULL() or COALESCE()Case 1 − Using IFNULL()The syntax of IFNULL() is as follows −SELECT IFNULL(yourColumnName, ’yourValue’) AS anyVariableName from yourTableName;Case 2 − Using COALESCE()The syntax of COALESCE() is as follows −SELECT COALESCE(yourColumnName, ’yourValue’) AS anyVariableName from yourTableName;Case 3 − Using CASE statementThe syntax of CASE statement.SELECT CASE WHEN yourColumnName IS NULL THEN ‘yourValue’ ELSE yourColumnName END AS anyVariableName FROM yourTableNameTo understand what we discussed above, let us create a table. The query to create a table is as follows −mysql> create ... Read More

Advertisements