Found 6705 Articles for Database

Select last day of current year in MySQL?

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

1K+ Views

In order to get last day of current year, you can use LAST_DAY() from MySQL. The syntax is as follows−SELECT LAST_DAY(DATE_ADD(CURDATE(), INTERVAL 12-MONTH(CURDATE()) MONTH));Let us implement the above syntax to know the last day of current year−mysql> SELECT LAST_DAY(DATE_ADD(CURDATE(), INTERVAL 12-MONTH(CURDATE()) MONTH));This will produce the following output −+-------------------------------------------------------------------+ | LAST_DAY(DATE_ADD(CURDATE(), INTERVAL 12-MONTH(CURDATE()) MONTH)) | +-------------------------------------------------------------------+ | 2019-12-31 | +-------------------------------------------------------------------+ 1 row in set (0.00 sec)

IF() function in a MySQL Select statement?

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

434 Views

The IF() function returns a value based on a condition.The syntax is as follows−SELECT IF(yourCondition, yourMessageIfConditionBecomesTrue, yourMessageIfConditionBecomesFalse) from yourTableName; Let us first create a table: mysql> create table DemoTable    (    Value int    ); Query OK, 0 rows affected (0.60 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(1000); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(2000); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(500); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(1100); Query OK, 1 row affected (0.16 sec)Display all records ... Read More

How do you fill in or pad a column with zeros using a MySQL query?

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

496 Views

You can use ZEROFILL for column to fill in or pad with zeros. Let us first create a table−mysql> create table DemoTable    (    Number int    ); Query OK, 0 rows affected (0.58 sec)Following is the query to add zerofill attribute for Number column−mysql> alter table DemoTable change Number Number int(10) zerofill not null; Query OK, 0 rows affected (1.13 sec) Records: 0 Duplicates: 0 Warnings: 0Insert records in the table using insert command −mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.53 sec) ... Read More

Add data to existing data in a MySQL Database?

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

815 Views

You can use CONCAT() function for this. Let us first create a table −mysql> create table DemoTable    (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(100)    ); Query OK, 0 rows affected (0.43 sec)Insert records in the table using insert command −mysql> insert into DemoTable(UserName) values('John'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(UserName) values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserName) values('Robert'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+----------+ ... Read More

Is there a MAX function for rows and not for columns in MySQL?

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

154 Views

Yes, you can use GREATEST() from MySQL to check maximum from rows (not columns). Let us first create a table −mysql> create table DemoTable    (    Value1 int,    Value2 int,    Value3 int    ); Query OK, 0 rows affected (0.58 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(190, 395, 322); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output−+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 190 | 395 ... Read More

Find size of text stored in a specific MySQL column?

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

248 Views

You can use length() from MySQL to find the size of text stores in a specific column. Let us first create a tablemysql> create table DemoTable    (    CustomerName longtext    ); Query OK, 0 rows affected (0.67 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output−+--------------+ | CustomerName | +--------------+ | Robert       | +--------------+ 1 row in set (0.00 sec)Here is the query to find the size ... Read More

MySQL query to select ENUM(\'M\', \'F\') as \'Male\' or \'Female\'?

Venu Madhavi
Updated on 04-Feb-2025 16:21:04

2K+ Views

In MySQL, the ENUM data type is used to limit column values to a specified set of options which is useful for categories like colors, status, and gender. Sometimes, it is helpful to display these ENUM values as more descriptive like converting 'P' to "pass" and 'F' to "Fail". The IF() function in MySQL makes this easy by allowing conditional logic within SQL queries. The example below will demonstrate how to use the IF() function to convert ENUM values into user-friendly. Example To understand first let us create a table, DemoTable, which contains an ENUM column UserGender to store ... Read More

How to select objects where an array contains only a specific field in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

334 Views

Let us first create a collection with documents −> db.arrayContainOnlySpecificFieldDemo.insertOne( ...    { ...       "StudentName":"John", ...       "StudentAge":21, ...       "StudentTechnicalSubject":["C", "Java", "MongoDB"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc4921dac184d684e3fa26a") } > db.arrayContainOnlySpecificFieldDemo.insertOne( { "StudentName":"Carol", "StudentAge":23, "StudentTechnicalSubject":["MongoDB"] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc49237ac184d684e3fa26b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayContainOnlySpecificFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc4921dac184d684e3fa26a"),    "StudentName" : "John",    "StudentAge" : 21,   ... Read More

Get attribute list from MongoDB object?

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

To get attribute list from MongoDB object, you can use for loop to extract key and value for document. Let us create a collection with documents −>db.getAttributeListDemo.insertOne({"StudentId":101, "StudentName":"John", "StudentAdmissi onDate":new ISODate('2019-01-12'), "StudentSUbjects":["MongoDB", "Java", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdfcc9ac184d684e3fa269") }Display all documents from a collection with the help of find() method −> db.getAttributeListDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbdfcc9ac184d684e3fa269"),    "StudentId" : 101,    "StudentName" : "John",    "StudentAdmissionDate" : ISODate("2019-01-12T00:00:00Z"),    "StudentSUbjects" : [       "MongoDB",       "Java",       "MySQL"    ] }Following is the ... Read More

Check for Existing Document in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

144 Views

You can use findOne() for this. Following is the syntax −db.yourCollectionName.findOne({yourFieldName: 'yourValue'});Let us create a collection with documents −> db.checkExistingDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf90dac184d684e3fa265") } > db.checkExistingDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf912ac184d684e3fa266") } > db.checkExistingDemo.insertOne({"StudentName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf916ac184d684e3fa267") } > db.checkExistingDemo.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdf91bac184d684e3fa268") }Display all documents from a collection with the help of find() method −> db.checkExistingDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdf90dac184d684e3fa265"), "StudentName" : "John" } { "_id" : ObjectId("5cbdf912ac184d684e3fa266"), "StudentName" : "Carol" } ... Read More

Advertisements