Use MySQL STR_TO_DATE Function for Column Input Format

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

419 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

Specify Default Values in MySQL INSERT Statement

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

2K+ 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

Retrieve INT Column Value as MySQL TIMESTAMP

Srinivas Gorla
Updated on 30-Jan-2020 05:53:44

221 Views

We can use FROM_UNIXTIME() function to retrieve the value, as MySQL TIMESTAMP, stored as INT in the column of a table.For example, we have a table called ‘test123’ which has a column named ‘val1’. In this column, we stored the integer values as follows −mysql> Select * from test123; +------------+ | val1       | +------------+ |     150862 | | 1508622563 | |  622556879 | | 2147483647 | +------------+ 4 rows in set (0.00 sec)Now with the help of the FROM_UNIXTIME() function, we can retrieve the column integer values in the form of MySQL TIMESTAMP data.mysql> Select ... Read More

Avoid Using Group Functions Without GROUP BY in MySQL

karthikeya Boyini
Updated on 30-Jan-2020 05:50:13

273 Views

It is because without GROUP BY clause the output returned by MySQL can mislead. We are giving following example on the ‘Student’ table given below, to demonstrate it −mysql> Select * from Student; +------+---------+---------+-----------+ | Id   | Name    | Address | Subject   | +------+---------+---------+-----------+ | 1    | Gaurav  | Delhi   | Computers | | 2    | Aarav   | Mumbai  | History   | | 15   | Harshit | Delhi   | Commerce  | | 20   | Gaurav  | Jaipur  | Computers | +------+---------+---------+-----------+ 4 rows in set (0.00 sec) mysql> ... Read More

Count Value Repetitions Using GROUP BY in SQL

Ankith Reddy
Updated on 30-Jan-2020 05:48:55

190 Views

We can use COUNT(*) and GROUP BY clause to find out the repetition of a value in the column. Following is the example, using COUNT(*) and GROUP BY clause on ‘Name’ column of table ‘Student’, to demonstrate it −mysql> select count(*), name from student group by name; +----------+---------+ | count(*) | name    | +----------+---------+ | 1        | Aarav   | | 2        | Gaurav  | | 1        | Harshit | +----------+---------+ 3 rows in set (0.00 sec)The result set of above query shows that which value is repeated ... Read More

0-1 Knapsack Problem using Branch and Bound in C++

Arnab Chakraborty
Updated on 30-Jan-2020 05:48:08

2K+ Views

The idea is to implement the fact that the Greedy approach provides the best solution for Fractional Knapsack problem.To check whether a particular node can give us a better solution or not, we calculate the optimal solution (through the node) implementing Greedy approach. If the solution calculated by Greedy approach itself is more than the best so far, then we can’t obtain a better solution through the node.Complete Algorithm is given below −Sort all items according to decreasing order of ratio of value per unit weight so that an upper bound can becalculated implementing Greedy Approach.Initialize maximum profit, such as ... Read More

Offload Time and Date Handling in MySQL

Prabhas
Updated on 30-Jan-2020 05:41:10

228 Views

We can offload the time/date handling to MySQL with the help of DATE_FORMAT() function. The date and time would be offloaded on the basis of format units passed as arguments to the function.For example, when we pass the date format units as arguments to MySQL DATE_FORMAT() function then MySQL offloaded only the date as follows −mysql> Select DATE_FORMAT("2017-10-22 13:03:45", "%Y %M %D")AS 'OFFLOADED DATE'; +-------------------+ | OFFLOADED DATE    | +-------------------+ | 2017 October 22nd | +-------------------+ 1 row in set (0.00 sec)Whereas, when we pass the time format units as arguments to MySQL DATE_FORMAT() function then MySQL offloaded only ... Read More

Use TIME_FORMAT Function to Offload Time/Date Values in MySQL

Priya Pallavi
Updated on 30-Jan-2020 05:39:56

149 Views

TIME_FORMAT() function can be used in a similar fashion as DATE_FORMAT() function but it can only be used for offloading time values. MySQL returns a NULL value if TIME_FORMAT() function is used for offloading date values.For example, when we pass the time format units as arguments to MySQL TIME_FORMAT() function then MySQL offloaded only the time as follows −mysql> Select TIME_FORMAT("2017-10-22 13:03:45", "%h %i %s %p")AS 'OFFLOADED TIME'; +----------------+ | OFFLOADED TIME | +----------------+ | 01 03 45 PM    | +----------------+ 1 row in set (0.00 sec)Whereas, when we pass the date format units as arguments to MySQL TIME_FORMAT() ... Read More

Fetch Month and Day from a Given Date in MySQL

usharani
Updated on 30-Jan-2020 05:37:11

190 Views

It can be done inflowing two ways −(A) With the help of EXRACT() function - EXTRACT() function can fetch any part from MySQL TIMESTAMP value. Following is the example of fetching month and day from a given date.mysql> Select EXTRACT(Month from '2017-10-22') AS 'MONTH'; +-------+ | MONTH | +-------+ |    10 | +-------+ 1 row in set (0.00 sec) mysql> Select EXTRACT(day from '2017-10-22')AS 'DAY'; +------+ | DAY  | +------+ |   22 | +------+ 1 row in set (0.00 sec)(B) With the help of MONTH() or DAY() function -  Rather than passing month and day as one of ... Read More

Use 2-Digit Year Value in MySQL DATEDIFF Function

Govinda Sai
Updated on 30-Jan-2020 05:34:10

306 Views

We can use 2-digit year value either in single date expression or in both date expressions used as argument/s in MySQL DATEDIFF() function.For example, the query below is using 2-digit year value in first date expression and other is having 4-digit year value.mysql> Select DATEDIFF('18-10-22', '2017-10-22'); +-----------------------------------+ | DATEDIFF('18-10-22', '2017-10-22') | +-----------------------------------+ |                               365 | +-----------------------------------+ 1 row in set (0.00 sec)And the query below is using 2-digit year value in both date expressions.mysql> Select DATEDIFF('18-10-22', '17-10-22'); +---------------------------------+ | DATEDIFF('18-10-22', '17-10-22') | +---------------------------------+ |   ... Read More

Advertisements