SQL : datatype as trunc(sysdate) - sql

I was trying to create a table with a column's data type as trunc(sysdate).
Is that possible?
When I tried it , I got below error
SQL Error: ORA-00902: invalid datatype
I am trying this because I want to make sure data inserted into that column doesn't have timestamp.

Just create a trigger
CREATE TRIGGER schema.trigger_name
BEFORE INSERT OR UPDATE
ON schema.table_name
FOR EACH ROW
new.column_name = trunc(column_name);

No that is not possible.
Trunc() is a function that truncates date to a specific unit of measure.
The DATE datatype stores point-in-time values (dates and times) in a
table. The DATE datatype stores the year (including the century), the
month, the day, the hours, the minutes, and the seconds (after
midnight).

Related

Create a Trigger to change values before insert in Informix

We have a table with a date field. Also there are some old apps we can't modify whose inserts that date field with string ("yyyy-mm-dd") (Yeah, SQL Injectión Party!!). Now it's needed to change that field to a Datetime year to second. This makes that old apps fail.
create table test(
testDate DATETIME YEAR TO SECOND
);
insert into test values("2018-12-12")
Error: A field in a datetime or interval value is incorrect or an illegal operation specified on datetime field.
We are trying to make a trigger to concatenate " 00:00:00" to the string if its length is 10. That should correct the problem.
It's that even posible?
Seems to me that if I use a before insert trigger, I can't modify the insert values. But if I use for each row instead, the trigger is not launched because the Error is throwed first.
Any Idea?

Oracle add a virtual column which adds years

I'm trying to add a virtual column of date type which adds five years to another date column:
ALTER TABLE AU_Ventes ADD (
DateVente date NOT NULL,
DateFinGarantie date As(ADD_MONTHS(DateVente, 60))
);
But I get the error: "%s: invalid identifier". Perhaps I can't use the ADD_MONTHS function in an ALTER TABLE. What can I do to accomplish what I want to do?
Thanks.
Although you can define a virtual column in a create table statement, where you are obviously creating it at the same time as the column it is based on, you can't define both in the same alter table statement. You have to do it in two steps:
ALTER TABLE AU_Ventes ADD DateVente date NOT NULL;
ALTER TABLE AU_Ventes ADD DateFinGarantie date
As(ADD_MONTHS(DateVente, 60));
Not sure that's an intentional restriction, but it's how it works.
Trivial SQL Fiddle to show this working.

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.

Milliseconds from GETUTCDATE not stored in datetime field

I have stored procedure that inserts data into table. One column in the table is datetime and is used for storing the time stamp of row insert:
INSERT INTO myTable (Field1, Field2, Field3) VALUES (1, 2, GETUTCDATE());
Field3 is datetime column. When I select data from that table with simple SELECT * FROM myTable query, all datetime values are shown with .000 value for milliseconds.
If I execute SELECT GETUTCDATE(), the milliseconds are displayed: 2013-10-16 18:02:55.793
Why milliseconds are not stored/displayed in the date time column on SELECT?
You have to be doing something somewhere to change this to smalldatetime or something because it works fine. I just created a new table, inserted data like you showed, queried the table and I have the milliseconds.
I have been unable to find anything where you can set the precision at the server level so it must be in your code.
Which datetime type are you using? To store the date with precision up to a millisecond, you need to use DATETIME2 if you're using SQL Server 2008 or higher.
'DATETIME' gives a precision of about 1/300th of a second.
'SMALLDATETIME' has accuracy of 1 minute.
Source: http://msdn.microsoft.com/en-us/library/ff848733.aspx
As Steve suggested, issue was not related with server. There is a trigger on this table, that trigger does milliseconds rounding on insert.

Is it possible to get last update time of the row in sql

Is it possible to get last updated time and date of the row using MYSQL server.
Well there is no inbuild feature exists with MySQL. Though you can get the same effect by adding a timestamp column:
ALTER TABLE NAMEYOURTABLE
ADD COLUMN last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
using above to create timestamp with name last_update column will make it pretty much automatically managed and updated. Now you can select from NAMEYOURTABLE the last updated row based on the timestamp.