every day Update the age depends on DOB automatically on each row in sql - sql

I want to update the age of employees present in the database according to Date of birth (Field name is DOB),but it should update automatically after application start.

It seems like you want this field to be a part of your table, rather than in a view, as suggested by #jarlh in the comments. So why not change it into a computed column instead?
ALTER TABLE [YourTableName]
DROP COLUMN [Age];
ALTER TABLE [YourTableName]
ADD [Age] AS ([Your Age Formula]);

Use trigger to update the age field and make it run on the midnight of the day

Related

Can I add a column to a temporary table that stores part of a date (year) from another column?

I want to select all columns from a table into a #temp1 table based on criteria.
In addition, I want to add an additional column that is the year portion only of a captured date field. I want the original whole date and add a separate column of just the year part of each date.
I tried to alter the #temp1 table and add a column called EnteredYear.
I then tried an update with a set EnteredYear = DATEPART(year, EnteredDate), but I am getting a syntax error.
Searching learn.microsoft.com and StackOverflow, but I haven't hit upon the right syntax yet.
If I understand correctly, you can try to use the computed column which needs to ALTER TABLE because it is a schema change.
ALTER TABLE #Temp ADD EnteredYear AS DATEPART(year, EnteredDate)
sqlfiddle
Temp tables work almost like persisten tables. You manipulate them wit INSERT, UPDATE DELETE statements. So if you have already added your new column you could write:
UPDATE #temp1 set EnteredYear = DATEPART(year, EnteredDate)

add not null column take values from other column

I have an existing table say, Items with Id | CreateDate (as DateTime) columns; want to add Year int not null, take the default year from the CreateDate column.
The Year value will be futer updated to different value, but just set it as default value when the column Year is added, because want to add non-nullable column to Year.
Is that possible?
If you want to add a new column and initialize it with the values from the other one, you can do:
-- add the column (as nullable)
alter table mytable add created_year int;
-- update the values on existing rows
update items set created_year = year(created_date);
-- make the column not nullable
alter table mytable alter column created_year int not null;
Don't bother with another column -- unless you will feel the need to change the value of the column in the future. Instead, just use a computed column:
alter table t add created_year as (year(created_date));
This is automatically calculated when you query the table, so it is always accurate, both for new rows and old rows.
Presumably, created_date is never updated.

Changing the data type "varchar'' of a column to "DATE" in SQL/ORACLE

I have a table in a database created in oracle 10G. It contains a column of type 'VARCHAR' and stores date as string in this format-> 'dd-mon-yyyy' eg: '12-aug-2008'. Now I want to change the datatype of this column from VARCHAR to DATE. but when i perfrom this query->
ALTER TABLE sales_order
MODIFY COLUMN delivery_date DATE;
I get following error
ORA-00905: missing keyword
I have also tried :
ALTER TABLE sales_order
ALTER COLUMN delivery_date DATE;
I got the error :
ORA-01735: invalid ALTER TABLE option
However when i try to add a fresh column with DATE datatype it works fine.
example :
ALTER TABLE sales_order
ADD delivery DATE;
So, can anybody suggest me a way to change the datatype without deleting the column and its data.
It's the first one, with a slight modification:
ALTER TABLE sales_order MODIFY (delivery_date DATE);
But I'm not sure that will work for those particular datatypes and it also may not work depending on the current data.
You may find it necessary in that case to:
create a new column X of date type.
populate X based on the old column (may need several passes of data fix-ups to work).
delete old column.
rename X to old column name.
Although its a pretty old question, I'll put my solution here for people seeking for a solution:
Here's my solution and it works perfectly.
ALTER TABLE `sales_order` CHANGE `delivery_date` `delivery_date` DATE;
Thank you
modify a column then syntax is:-
alter table table_name modify column_name datatype;
but when you modify the column datatype column must be empty
Thanks for the hints! This got it for me.
alter table Table_Name
Alter column Column_Name datatype
GO
I too was needing to change from a VARCHAR to a date. I am working in SQL 2008 R2. I have found that if I bring in dates as a char or varchar and then change the type to date or datetime, I can catch time/date problems more easily.
THIS STATEMENT WILL FAIL IF YOUR DATA HAS ANY BAD DATES. (I break the date down into sections to find the bad date so I can correct and then I can alter the column type.)
Alternatively, you could create a new column, in which the data type is DATE. then pass the data in your varchar as a date .then drop your initial column and finally rename your new column to what it was initially...code below.
ALTER TABLE my_table ADD (new_col DATE);
UPDATE my_table SET new_col=TO_DATE(old_col,'MM/DD/YYYY');
ALTER TABLE my_table DROP (old_col);
ALTER TABLE my_table RENAME COLUMN new_col TO old_col;
alter table employee add (DOB varchar(10));
if you add a column with datatype varchar and if you want to modify the datatype of DOB then you can use this command ->
alter table employee modify(DOB date);
Now the table is modified.

Calculate the age and Insert it to an already column via SQL Server

I have a table in database which is already has the data, but now I added new column called Vol_Age and it has NULL value. I want that column filled by calculating Age, the data comes from Vol_Date_of_Birth column, I need a query that calculate the age and insert it to the Vol_Age column.
which we can use Insert or Update, and how we can do it?!
Please forgive me for my English.
Thanks
Keep in mind that since you added the column to the database, any values within that column will retain their value. As time goes on, you will need to keep updateding the values within the database.
Another approach would be to remove the column and make it a computed column instead. This way, the age is caculated at query time rather than from your last update.
ALTER TABLE tbname ADD Vol_Age AS DATEDIFF(YEAR, Vol_Date_Of_Birth, GETDATE())
Try using the DateDiff function:
SELECT DATEDIFF(year,GETDATE(),Vol_Age)
you can use this:
UPDATE Personal_Info SET vol_Age = (SELECT (DATEDIFF(YEAR, Vol_Date_Of_Birth,GETDATE()) + CONVERT(REAL,(DATEDIFF(month, Vol_Date_Of_Birth,GETDATE()) % 12))/12) AS AGE
FROM Personal_Info)
I got answers from different places and merged the answers to get this:
UPDATE Personal_Info SET vol_Age = (select convert(int,DATEDIFF(d, Vol_Date_of_Birth, getdate())/365.25));

populate a column with a value based on value of other column alter table sql

I have a table 'Data' with a column 'Date'
I need to add another column called flag and populate it with 0 if the date is less than 2 years from current date and populate it with 1 if the date is more than 2 years from current date.
I did it by adding column using alter table and using update set statement as below
alter table data add flag INTEGER constraint flag_value check (flag in(0,1));
Is there a way to do this using just one alter table statement without using update set?
Is there a way to do this using just one alter table statement without using update set?
Not in sqlite, which does not support computed columns and there is no way to run a default UPDATE via an ALTER TABLE statement.
You could use the view approach from the linked question, or do as you've done and issue separate ALTER TABLE and UPDATE statements.