473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to create a computed column that is recalculated each time it is accessed

3
Hello!
Considering a two column table
-------------------------
| DOB | Age |
-------------------------
|yyyy-mm-dd | Xy-Yd-Zd |
-------------------------
| | |

Using a UDF, I would like to force calculate Age every time the record is accessed.

Sorry if it is not clear.
Thanks in advance!
Sep 8 '13 #1

✓ answered by Rabbit

Create a UDF if you need to reuse code. Create a view if you want to display data in a specific way. Combine the two if you need both. In your case, you definitely want a view. Whether or not you create a UDF to use in the view is up to you.

5 2129
Luuk
1,047 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. delimiter //
  2. CREATE PROCEDURE update_dob()
  3. BEGIN
  4.     UPDATE dob set age=datediff(dob,now());
  5. END//
  6.  
  7. delimiter ;
But this requires to run this procedure to update the values....
Expand|Select|Wrap|Line Numbers
  1.  call update_dob();
Sep 8 '13 #2
waqia
3
Thank you Luuk.
I was hoping that there was I proper calculated/computed column type in Mysql.
Sep 8 '13 #3
Luuk
1,047 Expert 1GB
maybe you can use a view
Expand|Select|Wrap|Line Numbers
  1. create view v_dob as select dob, datediff(dob,now()) age from dob;
  2.  
after doing "insert into dob (dob) values ('2013-09-01');"
Expand|Select|Wrap|Line Numbers
  1. [test@test]> select * from v_dob where dob='2013-09-01';
  2. +------------+------+
  3. | dob        | age  |
  4. +------------+------+
  5. | 2013-09-01 |   -7 |
  6. +------------+------+
  7. 1 row in set (0.00 sec)
  8.  
Sep 8 '13 #4
waqia
3
Thank you again.
But I definitely need to use a UDF or something that allows me to return the age in this format :
x_years-y_month-z_days
eg : 10Y-11M-29D

Cheers!
Sep 8 '13 #5
Rabbit
12,516 Expert Mod 8TB
Create a UDF if you need to reuse code. Create a view if you want to display data in a specific way. Combine the two if you need both. In your case, you definitely want a view. Whether or not you create a UDF to use in the view is up to you.
Sep 9 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: BCR | last post by:
I created a index on a computed column. I did not see any improvement in performance with a join to this column and also my inserts and updates to this table are failing. Any ideas? Chender
9
by: DMAC | last post by:
If i want to split a computed column into two or more columns based on the the length (its a varchar) of the computed column, how often will sql server determine what the computed column is?...
0
by: Jim Heavey | last post by:
Hello, I have created a computed column which concatenates a name and date. My problem is that if the 10 is not 10 characters, I get an extra character placed into the computed column. Here is the...
4
by: Bill Todd | last post by:
Is there any way to display the RowState of each row in a computed column in the DataTable? -- Bill
0
by: Han Holl | last post by:
Hi, Can anyone explain to me what I'm doing wrong: (The first select is to show that the function rubriek exists, and does work). I want to create an index on a computed column: palga=>...
6
by: jim_geissman | last post by:
Can I create an index on a variation of a column that isn't actually in the table? I have a ParcelNumber column, with values like 123 AB-670 12345ABC 000-00-040 12-345-67 AP34567890
2
by: Dot Net Daddy | last post by:
Hello, I want to assign a column a computed value, which is the multiplication of a value from the table within and a value from another table. How can I do that?
2
by: garridor | last post by:
I am working on a DB I created for a small employee scheduling software (in VB 2005) - to be used my parents in their non profit business. I would like to capture a time span between TimeIn and...
1
by: coder1234 | last post by:
I'm trying to create a computed column in a table in SQL Server 2005 and can't get the syntax right for the Formula. I want the computed column to be a bit data type which resolves to 1/0...
3
by: The Quiet Center | last post by:
If I want to count the number of records in a table GROUPed BY the first word in a string column, must I first compute the first word and store in a temporary table and then do my GROUP BY select?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.