What's wrong with this Postresql ALTER TABLE? - sql

I have a database where there is a column of varchar that I wish to convert to a timestamp. I'd like to do something like this, but keep getting a syntax error, please can someone advise?
ALTER TABLE MY_TABLE
ALTER COLUMN MY_COLUMN TYPE timestamp
USING to_timestamp(MY_COLUMN::double precision);
MY_COLUMN is of type VARCHAR(255) NOT NULL
Error reads:
Syntax error in SQL statement "ALTER TABLE MY_TABLE
ALTER COLUMN MY_COLUMN TYPE TIMESTAMP
USING[*] TO_TIMESTAMP(MY_COLUMN::DOUBLE PRECISION) "; SQL statement:
ALTER TABLE MY_TABLE
ALTER COLUMN MY_COLUMN TYPE timestamp
USING to_timestamp(MY_COLUMN::double precision) [42000-176] 42000/42000

It would appear that my Postgresql running within Grails doesn't support to_timestamp. As pointed out by #a_horse_with_no_name in the comments of the question, the code works. Thanks for pointing out sqlfiddle.com, I hadn't realised such a resource existed.

Related

Alter column length in SQL

ALTER TABLE tableName
MODIFY COLUMN columnName VARCHAR (256);
Error :
Incorrect syntax near 'VARCHAR'
The syntax for modifying a column definition is depending on used database. For Microsoft SQL Server for example, you would need to replace MODIFY COLUMN with ALTER COLUMN.
For better help you should specify your database type.

Updating current time for a column in SQL

So i am trying to store the time at which any changes were made to any row.
I was using the following query:
ALTER TABLE SPRD_MGMT_INP_INDEX_CHANGE_RATE
CHANGE "UPLOAD TIME"
"UPLOAD TIME" TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
I am getting the following error:
SQL compilation error: syntax error line 2 at position 0 unexpected 'CHANGE'. Please help
The syntax uses alter column and set, but the bigger issue is that you can't change the default of a column in Snowflake with the exception noted here:
https://docs.snowflake.com/en/sql-reference/sql/alter-table-column.html
In that page, it states the following:
Change the default for a column, unless the default is a sequence.
In that table for the action quoted above it has a checkmark by Unsupported.
You will need to create a new table with this default. You can then copy your rows from the original table using an insert from select.
You can specify the default in your new table like this:
create or replace table T1
(COL1 int, COL2 string, UPLOAD_TIME timestamp_tz default current_timestamp());

Failed to execute query. Error: String or binary data would be truncated in table xdbo.user_info', column 'uid'

I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE

How to change datatype of the column in derby database?

Iam trying to change the datatype of the column from integer(9) to Numeric(14,3).
but not able to change
i tried below query
alter table TableName alter ColumnName NUMERIC (14,3);
hope below one will help.
ALTER TABLE Table_Name ALTER COLUMN Column_Name SET DATA TYPE NUMERIC(14,3);

SQL Server Alter Table Query (Incorrect Syntax)

I'm stumped, and I know this is probably something very simple. I am trying to add two columns to an existing table.
I am receiving the following syntax error:
Incorrect syntax near 'PublishedDate' Expecting '(', or SELECT.
Here is my SQL:
ALTER TABLE my_table ADD (PublishedDate DATETIME, UnpublishedDate DATETIME)
Try without the parentheses:
ALTER TABLE my_table
ADD PublishedDate DATETIME, UnpublishedDate DATETIME
Here is a sqlfiddle with a demo for you to try.