- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the maximum exam date using a user-defined variable in SQL
To get the maximum exam date with a user-defined variable, the code is as follows −
select date(max(yourColumnName )) into @yourVariableName from yourTableName;
To understand the above syntax, let us first create a table −
mysql> create table DemoTable2001 ( ExamDate date ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2001 values('2019-01-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2001 values('2018-12-31'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2001 values('2018-11-18'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable2001 values('2019-07-25'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2001;
This will produce the following output −
+------------+ | ExamDate | +------------+ | 2019-01-10 | | 2018-12-31 | | 2018-11-18 | | 2019-07-25 | +------------+ 4 rows in set (0.00 sec)
Here is the query to get the maximum date. We will first create a user-defined variable −
mysql> set @comingExamDate:=null; Query OK, 0 rows affected (0.00 sec) mysql> select date(max(ExamDate)) into @comingExamDate from DemoTable2001; Query OK, 1 row affected (0.00 sec) mysql> select @comingExamDate;
This will produce the following output −
+------------------+ | @commingExamDate | +------------------+ | 2019-07-25 | +------------------+ 1 row in set (0.00 sec)
- Related Articles
- Increment multiple Timestamp values by setting the incremented value in a user-defined variable in SQL
- Swap two Strings without using third user defined variable in Java
- Select into a user-defined variable with MySQL
- Perform MySQL SELECT INTO user-defined variable
- How can we store a value in user-defined variable?
- In MySQL, why a client cannot use a user-defined variable defined by another client?
- MySQL ORDER BY with numeric user-defined variable?
- MongoDB query to set user defined variable into query?
- Using User-Defined Variables in MySQL
- Set user-defined variable with table name in MySQL prepare statement?
- How Can we permanently define user-defined variable for a client in MySQL?
- How to pass a date variable in sql query in a JSP?
- Updating a MySQL table row column by appending a value from user defined variable?
- How to get the details of a user-defined function in a database from AWS Glue Data catalog using Boto3
- Using TreeMap to sort User-defined Objects in Java

Advertisements