MySQL Articles - Page 290 of 402

MySQL query to extract last word from a field?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

3K+ Views

To extract last word from a field, use in-built SUBSTRING_INDEX() function. The syntax is as follows −SELECT SUBSTRING_INDEX(yourColumnName, ’ ‘, -1) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The following is the query to create a table −mysql> create table FirstWordDemo −> ( −> AllWords longtext −> ); Query OK, 0 rows affected (0.83 sec)Now insert some words in the table using insert command. The query is as follows −mysql> insert into FirstWordDemo values('This is the first MySQL Query'); Query OK, 1 row affected (0.11 ... Read More

Which MySQL type is most suitable for “price” column?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

24K+ Views

The best type for price column should be DECIMAL. The type DECIMAL stores the value precisely.For Example - DECIMAL(10, 2) can be used to store price value. It means the total digit will be 10 and two digits will be after decimal point.To understand the type DECIMAL, let us create a table.mysql> create table PriceDemo    −> (    −> ProductPrice DECIMAL(10, 2)    −> ); Query OK, 0 rows affected (0.60 sec)Now insert some records in the table in the form of price. The query to insert records is as follows −mysql> insert into PriceDemo values(12345.67); Query OK, 1 row ... Read More

Strip last two characters of a column in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:24

2K+ Views

You can strip last two characters with the help of SUBSTRING() and CHAR_LENGTH() methods. The syntax is as follows −select yourColumnName, SUBSTRING(yourColumnName, 1, CHAR_LENGTH(yourColumnName) - 2) AS anyVariableName from yourTableName;To understand the above syntax, let us create a table −mysql> create table LastTwoCharacters −> ( −> Words varchar(200) −> ); Query OK, 0 rows affected (0.71 sec)Now you can insert some records in the table with the help of select statement. The query to insert records is as follows −mysql> insert into LastTwoCharacters values('Hellooo'); Query OK, 1 row affected (0.23 sec) ... Read More

How to rearrange MySQL columns?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

707 Views

To rearrange the MySQL columns, check the column arrangement with the help of show create command. The syntax is as follows −show create table yourTableName;The syntax to rearrange the MySQL columns is as follows −alter table yourTableName change column yourColumnName yourColumnName dataType firstFor the same purpose, you can use the after keyword. The syntax is as follows −alter table yourTableName change column yourColumnName yourColumnName dataType after yourSpecificColumnName;Let us first check the column arrangement for the already created table “AddColumn” −mysql> show create table AddColumn; The following is the output −+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table     | Create Table       ... Read More

Mobile

How to create a Cumulative Sum Column in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:24

3K+ Views

To create a cumulative sum column in MySQL, you need to create a variable and set to value to 0. Cumulative sum increments the next value step by step with current value.Firstly, you need to create a variable with the help of SET. The syntax is as follows −set @anyVariableName:= 0;The syntax to create a cumulative sum column in MySQL is as follows −select yourColumnName1, yourColumnName2, ........N, (@anyVariableName := @anyVariableName + yourColumnName2) as anyVariableName from yourTableName order by yourColumnName1;To understand the above concept, let us create a table. The following is the query to create a table −mysql> create table ... Read More

How to get the creation date of a MySQL table?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

10K+ Views

To get the creation date of a MySQL table, use create_time from information_schema.tables. The syntax is as follows −SELECT create_time FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'yourDatabaseName' AND table_name = 'yourTableName';My table name is 'skiplasttenrecords' and database is ‘test’.Implement the above syntax for your database and table name. The query is as follows −mysql> SELECT create_time FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'test' −> AND table_name = 'skiplasttenrecords';The following is the output displaying the creation date −+---------------------+ | CREATE_TIME | +---------------------+ | 2018-11-29 15:47:14 | +---------------------+ 1 row in set (0.00 sec)

Calculate Age from given Date of Birth in MySQL?

Rishi Rathor
Updated on 30-Jul-2019 22:30:24

1K+ Views

To calculate age in MySQL from Date of Birth, you can use the following syntax −SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(yourColumnName) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(yourColumnName, 5)) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The following is the query to create a table.mysql> create table AgeCalculatesDemo −> ( −> YourDateOfBirth datetime −> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table with the help of insert command. These records are the DOBs through which we will calculate the age. The following is the ... Read More

Adding a day to a DATETIME format value in MySQL?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:24

462 Views

To add a day to a DATETIME format value, you can use DATE_ADD() function from MySQL.The syntax is as follows −select date_add(now(), interval 1 day) as anyVariableName;Now you can implement the above syntax in order to add a day to a datetime format.mysql> select date_add(now(), interval 1 day) as Adding1DayDemo;The following is the output −+---------------------+ | Adding1DayDemo | +---------------------+ | 2018-12-07 20:06:59 | +---------------------+ 1 row in set (0.00 sec)If you want to add a day only to a date, you can use curdate() function. The query is as follows −mysql> select date_add(curdate(), interval 1 ... Read More

Get all characters before space in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:24

1K+ Views

In order to get all characters before space in MySQL, you can use left() function from the MySQL. The syntax is as follows −select left(yourColumnName, LOCATE(' ', yourColumnName) - 1) 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 AllCharacterBeforeSpace −> ( −> FirstNameAndLastName varchar(200) −> ); Query OK, 0 rows affected (0.51 sec)Now you can insert some records in the table.The query to insert records is as follows −mysql> insert into AllCharacterBeforeSpace values('John Smith'); Query ... Read More

MySQL Select Date Equal to Today and return results for the same date?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

897 Views

To get today’s date, use in-built function CURDATE(). The CURDATE() gives only current date not time. With that, to get the records for the same day, you can try the following syntax −select yourColumnName1, yourColumnName2, ......, yourColumnNameN, DATE_FORMAT(yourDateColumnName, '%Y-%m-%d') from yourTableName WHERE DATE(yourDateColumnName) = CURDATE();To understand the above concept, let us create a table. The query to create a table is as follows. One of these columns will have datetime datatype to display dates −mysql> create table GmailSignIn −> ( −> UserId int, −> UserName varchar(200), −> DateOfSignIn ... Read More

Advertisements