Found 4378 Articles for MySQL

How MySQL evaluates if I store date along with time value in a column having DATE data type?

Ankitha Reddy
Updated on 20-Jun-2020 07:04:43

66 Views

As we know that the default format for MySQL DATE data type is “YYYY-MM-DD” and in this format, there is no possibility to store the time value. But still, if we store date along with time value in a column having DATE data type then MySQL will show a warning and store only the date value in that column. Following example will exhibit it −Examplemysql> Create table date_time_test(date DATE); Query OK, 0 rows affected (0.44 sec) mysql> Insert into date_time_test(date) values(‘2017-09-09 09:34:21’); Query OK, 1 row affected, 1 warning (0.03 sec) mysql> Select * from date_time_test; +------------+ | ... Read More

How can we use existing values of the rows to provide new values in the SET clause of UPDATE statement?

Swarali Sree
Updated on 20-Jun-2020 07:05:10

109 Views

Existing values of the row can be used to provide new values in the SET clause if that row matches the WHERE clause in an UPDATE statement. Following is the example to demonstrate it.ExampleSuppose we have a table named ‘tender’ as follows −mysql> Select * from tender; +-----------+---------+------+ | tender_id | company | rate | +-----------+---------+------+ | 200       | ABC     | 1000 | | 300       | ABD     | 6000 | | 301       | ABE     | 7000 | | 302       | ABF   ... Read More

What is the difference between MySQL NOW() and SYSDATE()?

Anvi Jain
Updated on 30-Jan-2020 06:12:14

2K+ Views

MySQL NOW() and SYSDATE() functions returns the current timestamp values. But the output of both of them depends upon the execution time. This creates the big difference between them.NOW() function returns a steady time that indicates the time at which the particular statement began to execute. In contrast, SYSDATE() function returns the accurate time at which the statement executes. Following example will show the difference between these functions −mysql> Select NOW(), SLEEP(5), NOW(); +---------------------+----------+---------------------+ | NOW()               | SLEEP(5) | NOW()               | +---------------------+----------+---------------------+ | 2017-10-31 09:57:36 | ... Read More

What is the importance of the order of Columns in the SET clause of UPDATE statement? Will it make big difference in result set returned by MySQL?

Ankith Reddy
Updated on 20-Jun-2020 07:08:26

190 Views

The order of columns in the SET clause of UPDATE statement is important because MySQL provides us the updated value on columns names used in an expression. Yes, it will make big difference in the result set returned by MySQL. Following is an example to make it clear −ExampleIn this example, we are having a table ‘tender’. First, we will write UPDATE statement by using ‘tender_id’ as the first and ‘rate’ as the second column in SET clause and then we will write UPDATE statement by using ‘rate’ as the first and ‘tender_id’ as the second column on table ‘tender’.mysql> ... Read More

How can we create a MySQL function to find out the duration of years, months, days, hours, minutes and seconds?

Govinda Sai
Updated on 30-Jan-2020 06:18:59

242 Views

Following is a MySQL function which calculates the duration in years, months, days, hours, minutes and seconds between two dates.mysql> DROP FUNCTION IF EXISTS Duration; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> DROP FUNCTION IF EXISTS Label123; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> DELIMITER // mysql> CREATE FUNCTION Duration( dtd1 datetime, dtd2 datetime ) RETURNS CHAR(128)    -> BEGIN    -> DECLARE yyr, mon, mmth, dy, ddy, hhr, m1, ssc, t1 BIGINT;    -> DECLARE dtmp DATETIME;    -> DECLARE t0 TIMESTAMP;    -> SET yyr = TIMESTAMPDIFF(YEAR, dtd1, dtd2);   ... Read More

What MySQL returns if specified format string is not as per accordance with the date string passed as arguments to STR_TO_DATE() function?

Nishtha Thakur
Updated on 30-Jan-2020 06:20:30

380 Views

If the specified format string and date string did not match then MySQL will return NULL value as output along with a warning. Following is an example to understand the same −mysql> Select STR_TO_DATE('20172810', '%Y, %d%m'); +------------------------------------+ | STR_TO_DATE('20172810', '%Y, %d%m') | +------------------------------------+ | NULL                               | +------------------------------------+ 1 row in set, 1 warning (0.00 sec)The query above returns NULL as output because the format string is having a comma (, ) after %Y but date string is not having any comma after 2017.mysql> Show Warnings\G ... Read More

What MySQL returns on passing an invalid string as an argument to STR_TO_DATE() function?

Giri Raju
Updated on 30-Jan-2020 06:06:03

135 Views

If we pass an invalid string as an argument to STR_TO_DATE() function then MySQL will return NULL as output along with a warning. Following is an example to understand the same −mysql> Select STR_TO_DATE('20173210', '%Y%d%m'); +-----------------------------------+ | STR_TO_DATE('20173210', '%Y%d%m') | +-----------------------------------+ | NULL                              | +-----------------------------------+ 1 row in set, 1 warning (0.00 sec)In the query above the string value is invalid because of wrong (32) day value. Hence it returns NULL values and a warning which is given below.mysql> Show warnings\G *************************** 1. row ***************************   ... Read More

How can we specify default values in MySQL INSERT statement?

Lakshmi Srinivas
Updated on 30-Jan-2020 05:58:13

1K+ Views

At the time of creation of a table, if any column is defined with default values then by using the keyword ‘DEFAULT’ in the INSERT statement, we can take default value for that column. For example, we have created a table ‘employee’ with a default value of column ‘DOJ’ as follows −mysql> Create table employee(id int, name varchar(20), doj date DEFAULT '2005-01-01'); Query OK, 0 rows affected (0.09 sec) mysql> Insert into employee(id, name, doj) values(1, ’Aarav’, DEFAULT); Query OK, 1 row affected (0.03 sec) mysql> select * from employee; +------+------------+---------------+ | id   | name     ... Read More

How can we use MySQL function STR_TO_DATE(Column, ‘%input_format’)?

Ramu Prasad
Updated on 30-Jan-2020 05:59:04

239 Views

STR_TO_DATE() function will convert a string value into datetime value and it would be according to a specific format string. Both string value and format string must be passed as arguments to the function. Following is the syntax of STR_TO_DATE() function.STR_TO_DATE(string, format)Here string is the value of string which needs to be converted to datetime value and format is the specified date format.The following example will return valid date from given string and according to the specified format.mysql> Select STR_TO_DATE('20172810', '%Y%d%m'); +-----------------------------------+ | STR_TO_DATE('20172810', '%Y%d%m') | +-----------------------------------+ | 2017-10-28                       ... Read More

How to use MySQL FROM_UNIXTIME() function to return datetime value in numeric format?

Sreemaha
Updated on 30-Jan-2020 05:59:45

152 Views

As we know that we can convert a time of datetime value to an integer by adding 0(+0) to them. In a similar way, we can convert the datetime value returned by FROM_UNIXTIME() function in numeric format. The following example will clarify it more −mysql> Select FROM_UNIXTIME(1555033470)+0 AS 'Date in Numeric Format'; +------------------------+ | Date in Numeric Format | +------------------------+ | 20190412071430.000000  | +------------------------+ 1 row in set (0.00 sec)After adding 0 (+0) to datetime value MySQL returns the numeric value up to 6 digits microseconds.

Advertisements