How to create MySQL function to create a new column of quarter by processing existing column of date? -


i have column date shown below:

+-----------+ |   date    | +-----------+ |  20140611 | |  20150119 | |  20150922 | |  20160310 | +-----------+ 

it's string in yyyymmdd format.

i want create new column named quarter shown below:

+-----------+-------------+ |   date    |   quarter   | +-----------+-------------+ |  20140611 |  q2-2014    | |  20150119 |  q1-2015    | |  20150922 |  q3-2015    | |  20160310 |  q3-2016    | +-----------+-------------+ 

where quarter indicates quarter in corresponding date falls.

this example purpose, want know how can write function can applied on single column of table create new column after pre-defined processing.

use quarter function

select `date`, quarter(`date`) quarter my_table ; 

otherwise if looking user defined function can use

create function hello_world(my_text text)     returns text     language sql -- element optional , omitted         begin      return concat('hello ', my_text);    end;     $$   delimiter ; 

then

select hello_world('earth'); 

return hello earth

http://dev.mysql.com/doc/refman/5.7/en/create-function-udf.html


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -