MySQL DATE_FORMAT Function: Using Date and Time Format Characters

Rishi Rathor
Updated on 29-Jan-2020 06:13:43

163 Views

We can use both the format characters together in DATE_FORMAT() function. The following example will clarify this −mysql> SELECT DATE_FORMAT(NOW(), 'The time is %a %h:%i:%s:%f %p'); +-----------------------------------------------------+ | DATE_FORMAT(NOW(), 'The time is %a %h:%i:%s:%f %p') | +-----------------------------------------------------+ | The time is Sun 06:35:06:000000 AM                  | +-----------------------------------------------------+ 1 row in set (0.00 sec)Above query is using date format character ‘%a’ along with other time format characters.Following is another example in which both format characters are used together −mysql> SELECT DATE_FORMAT(NOW(), 'The date & time is %a %D %M %Y %h:%i:%s:%f %p'); ... Read More

Apply EXTRACT Function on Dates in MySQL Table

Vrundesha Joshi
Updated on 29-Jan-2020 06:10:46

213 Views

We can apply EXTRACT() function on the dates stored in MySQL table in the following way −The following query is showing what dates are entered in table ‘testing’mysql> Select * from testing; +-------------+---------------------+ | StudentName | Dateofreg           | +-------------+---------------------+ | Ram         | 2017-10-28 21:24:24 | | Shyam       | 2017-10-28 21:24:30 | | Mohan       | 2017-10-28 21:24:47 | | Gaurav      | 2017-10-29 08:48:33 | +-------------+---------------------+ 4 rows in set (0.00 sec)Now, we can apply EXTRACT() function, to obtain the value of the year, on ... Read More

Get Total Number of Seconds from MySQL DATETIME Instance

Jennifer Nicholas
Updated on 29-Jan-2020 06:06:20

364 Views

The MySQL DateTime instance can be converted into seconds with the help of UNIX_TIMESTAMP() function in the following way −mysql> Select UNIX_TIMESTAMP('2017-05-15 04:05:30') AS 'NUMBER OF SECONDS'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ |        1494801330 | +-------------------+ 1 row in set (0.00 sec)Above query will convert the given datetime instance into total number of seconds.mysql> Select UNIX_TIMESTAMP(NOW()) AS 'NUMBER OF SECONDS'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ |        1509248856 | +-------------------+ 1 row in set (0.00 sec)Above query will convert the current DateTime instance into a total number of seconds.mysql> ... Read More

MySQL ALTER TABLE Command for Adding Comments on Columns

Anjana
Updated on 29-Jan-2020 06:05:12

2K+ Views

We can use ‘COMMENT’ keyword with ALTER TABLE command while modifying the column to add comments on columns. For example if we want to add comment in column ‘id’ of table ‘testing’ then following query will do it −mysql> ALTER TABLE testing MODIFY id INT COMMENT 'id of employees'; Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0With following query it can be checked in the comment field of a column.mysql> Show full columns from testing\G *************************** 1. row ***************************      Field: id       Type: int(11)  Collation: NULL       Null: NO ... Read More

Output of MySQL EXTRACT Function with Date Values

Nitya Raut
Updated on 29-Jan-2020 06:03:30

124 Views

When we try to extract hour value from a date, then EXTRACT() function will give the output 0 with a warning as shown in the below-given example −mysql> Select EXTRACT(Hour from '2017-10-20'); +---------------------------------+ | EXTRACT(Hour from '2017-10-20') | +---------------------------------+ | 0                               | +---------------------------------+ 1 row in set, 1 warning (0.00 sec) mysql> Show Warnings; +---------+------+----------------------------------------------+ | Level   | Code | Message                                      | +---------+------+----------------------------------------------+ ... Read More

Display Time in User-Specified Format in MySQL

Nancy Den
Updated on 29-Jan-2020 05:57:48

108 Views

We can also use DATE_FORMAT() function to display the time in other formats. In this case, there would be two arguments of this function, first would be the time and second would be the format string.The following example will change the current time in specified format −mysql> SELECT DATE_FORMAT(NOW(), 'The time is %h:%i:%s %p'); +-----------------------------------------------+ | DATE_FORMAT(NOW(), 'The time is %h:%i:%s %p') | +-----------------------------------------------+ | The time is 06:02:28 AM                       | +-----------------------------------------------+ 1 row in set (0.00 sec)The following example will change the given time in specified format ... Read More

Add Comments to a Column in Existing MySQL Table

Samual Sam
Updated on 29-Jan-2020 05:56:20

3K+ Views

It can be done by using ‘COMMENT’ keyword while modifying the column with ALTER TABLE command. For example if we want to add comment in column ‘id’ of table ‘testing’ then following query will do itmysql> ALTER TABLE testing MODIFY id INT COMMENT 'id of employees'; Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0With following query it can be checked in the comment field of a column.mysql> Show full columns from testing\G *************************** 1. row ***************************      Field: id       Type: int(11)  Collation: NULL       Null: NO       ... Read More

Change Value of an Instance of a Row in MySQL Table

Chandu yadav
Updated on 29-Jan-2020 05:47:24

328 Views

UPDATE command along with WHERE clause can be used to change the value of an instance of a row. Basically, MySQL will change the value on the basis of the condition given in the query. Following example can demonstrate itSuppose we want to change the name from ‘Ram’ to ‘Mohit’ in the ‘testing’ table given below −mysql> Select * from testing; +----+---------+ | Id | Name    | +----+---------+ | 1  | Harshit | | 2  | Lovkesh | | 3  | Ram     | | 4  | Gaurav  | +----+---------+ 4 rows in set (0.00 sec)Now ... Read More

Add Columns with Default Values to an Existing MySQL Table

Vikyath Ram
Updated on 29-Jan-2020 05:42:28

3K+ Views

While adding columns to an existing table with the help of ALTER command we can specify the default value also.SyntaxAlter table table-name ADD (column-name datatype default data);ExampleIn the example below, with the help of ALTER Command, column ‘City’ is added with default value ‘DELHI’ to the table ‘Student’.mysql> Alter table Student ADD(City Varchar(10) Default 'DELHI'); Query OK, 5 rows affected (0.33 sec) Records: 5 Duplicates: 0 Warnings: 0Now from DESCRIBE command, we can check the default value of ‘City’ column.mysql> describe Student\g +---------+--------------+------+-----+---------+-------+ | Field   | Type         | Null | Key | Default ... Read More

Alternative Keywords to MODIFY for MySQL Column Modification

Ayyan
Updated on 29-Jan-2020 05:39:50

297 Views

We can use keyword CHANGE to modify the column/s of an existing table. With CHANGE keyword we can change the name of the column and its definition both. Its syntax would be a bit different from the syntax of ALTER TABLE with MODIFY keyword.SyntaxAlter table table_name CHANGE old_columnname1 new_columnname1 datatype, CHANGE old_columnname2 new_columnname2 datatype… CHANGE old_columnnameN new_columnname datatype);ExampleIn the example below, with the help of CHANGE keyword in ALTER Command, the name and size of the columns ‘City’ and ‘RollNo’ have been modified.mysql> Alter table Student CHANGE Rollno Id int, CHANGE City Place Varchar(10); Query OK, 5 rows affected (0.40 ... Read More

Advertisements