SQL | Change date to date+time - sql

So I have been trying to fix this problem and other Stackoverflow don't seem to do it for me
I have a column called "entered" with the datatype: "date". I would like to change the datatype from date to date+time. All post are saying to use:
ALTER TABLE (table)
ALTER COLUMN (column) timestamp
But then I get the following error:
ERROR: syntax error at or near "timestamp"
SQL state: 42601
Character: 44
For the record, the table currently has data and I cant simply copy and drop it

The syntax you posted should work for SQL Server / MS Access.
In Oracle SQL, I have used the below SQL syntax to change the data type of a column. I am not sure if this was one of the other suggestions that did not work. Hope this works for you.
alter table
your_table_name
modify
(
your_column_name datetime
);

Related

How do I change column data type in Redshift?

I have tried changing the column data type in Redshift via SQL. Keep getting an error:
[Amazon][Amazon Redshift] (30) Error occurred while trying to execute a query: [SQLState 42601] ERROR: syntax error at or near "TABLE" LINE 17: ALTER TABLE bmd_disruption_fv ^
Unable to connect to the Amazon Redshift server 'eceim.master.datamart.eceim.sin.auto.prod.c0.sq.com.sg'. Check that the server is running and that you have access privileges to the requested database
The first sql query works. I have tried writing the Alter Table script before the Select lines but it did not work too.
`
*Extract selected columns and renaming them for easier reference
*/
select ID, Completion_Time AS Date_Reported, Name2 AS Name, Contact_Info_for_updates AS Contact_Info,
Your_operation_line AS Operation_Line, Aircraft_Registration_SMU_SMT_etc AS Aircraft_Reg,
Designation_trade_B1_B2_ACT_AST_AAT AS Trade, Choose_your_Issue AS Issue, Manpower, Material, Equipment_GES,
Information, Tools, State_details_here_SVO_number_too AS Issue_Details, Time_wasted_on_due_to_issue AS Time_Wasted,
State_additional_comments_suggestions AS Additional_Comments, Stakeholders, Status
from bmdm.bmd_disruption_fv
/*Change colum data type
*/
ALTER TABLE bmd_disruption_fv
{
ALTER COLUMN ID TYPE INT
}
`
Several things are causing issues here. First the curly brackets '{}' should not be in the alter table statement. Like this:
alter table event alter column eventname type varchar(300);
Second, and likely more importantly, you can only change the length of varchar columns. So changing a column type to INT is not possible. You will need to perform a multistep process to make this change to the table.

Error while trying to mask a column in SSMS

I'm trying to mask some information in my table, when I use this :
ALTER TABLE Person
ALTER COLUMN DoB add masked with (Function = 'default()')
I get those two error :
Incorrect syntax near the word 'masked'
and
Incorrect syntax near the keyword 'with'. If the statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
That's literally the same code as the Microsoft documentation, but for some reason it doesn't work
If somebody could explain me what is the problem, that would help me a lot
To mask a column using the ALTER statement the datatype of the column must be included in this statement. The ADD keyword is also not necessary. I don't know what the datatype of the DoB column from your example is but it will need to be changed as below. From the name I'm guessing this is a datetime column, but if it's not the matching datatype will need to be substituted in.
ALTER TABLE Person ALTER COLUMN DoB DATETIME MASKED WITH (FUNCTION = 'default()')
Update:
This error was a result of using SQL Server 2012 when this feature was released in SQL Server 2016.

psql column doesn't exist but it does

I am trying to select a single column in my data table using raw SQL in a postgresql database from the psql command line. I am getting an error message that says the column does not exist. Then it gives me a hint to use the exact column that I referenced in the select statement. Here is the query:
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
Here is the error message:
ERROR: column insider_app_ownershipdocument.transactiondate does not exist
SELECT insider_app_ownershipdocument.transactionDate FROM in...
HINT: Perhaps you meant to reference the column "insider_app_ownershipdocument.transactionDate".
I have no idea why this is not working.
(Postgres) SQL converts names automatically to lower case although it support case-sensitive names. So
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
will be aquivalent to:
SELECT insider_app_ownershipdocument.transactiondate FROM insider_app_ownershipdocument;
You should protect the column name with double quotes to avoid this effect:
SELECT insider_app_ownershipdocument."transactionDate" FROM insider_app_ownershipdocument;

Incorrect syntax near 'for' SQL Server

I want to add a new Column to an existing Table that has already Data in it.
The Column should be NOT NULL.
Because of that i want to set a Default Value. But when i do it throws the following exception:
"Incorrect syntax near 'for'"
ALTER TABLE Semester ADD SIDNew uniqueidentifier NOT NULL
CONSTRAINT DF_SIDNew DEFAULT '00000000-0000-0000-0000-000000000000' FOR SIDNew
I already had a look at How to set a default value for an existing column
and Incorrect syntax near the keyword 'FOR' but none of them helped me.
Simply lose the part FOR SIDNew. You are adding a new column with a default constraint. You are not adding a new default constraint to an existing column.
The FOR keyword is only required if you're adding default values to Azure SQL Data Warehouse or Parallel Data Warehouse. If you're using normal relational SQL Server (on-premise or Azure) then you can just write it without the FOR
ALTER TABLE Semester ADD SIDNew uniqueidentifier NOT NULL
CONSTRAINT DF_SIDNew DEFAULT '00000000-0000-0000-0000-000000000000';
https://msdn.microsoft.com/en-gb/library/ms190273.aspx

Create table as select * from sql server with 'ntext' datatype

I have a db-link from an oracle 11 database to a SQL server database. I am trying to perform a ctas from a table in the SQL server database, however for every table with a column of datatype ntext I get the error:
ORA-00997: illegal use of LONG datatype.
How can I solve this issue? Even if I was willing to forget about those specific columns, I have about 300 tables to perform the same action on.