Alter column length in SQL - 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.

Related

Not able to update column which was set as null using 'select into' statement in sybase

I am creating a temp table in sybase like below
select col1 = null, col2 =2 into #myTable
Here when I try to update col1
update #myTable set col1 = 'test'
I get error - "[Error Code: 257, SQL State: 42000] Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query."
Can anyone please help me fix it?
Assuming this is Sybase ASE (257 is a standard ASE system error number) ...
col1=null doesn't tell the database what the datatype of col1 should be so the database defaults the column's datatype to int.
When creating a table via select/into you need to insure each column is created with the desired datatype. For this particular instance try:
select col1=convert(varchar(10),null), col2=convert(tinyint,2) into #myTable
NOTES:
modify the convert() calls to reference the desired datatypes
when the new column is populated from another table's column(s) the source column(s) datatypes will be used in determing the datatype of the new column
Also keep in mind the following:
select col1='test' into #otherTable
The datatype for col1 will be determined from the initial data value; in this case the value 'test' tells the database you need to store 4 characters so the database will default the column's datatype to varchar(4). This should be fine as long as you never intend to insert anything longer than varchar(4) otherwise you'll need to provide a convert() with the initial select/into to explicitly state the column's datatype, eg:
select col1=convert(varchar(35),'test') into #otherTable
Assuming you get past the Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. error message your next bump-in-the road may occur if you try something like:
update #myTable set col2 = NULL
With the result being that you're presented with an error message similar to column does not allow nulls.
As with datatype determination, Sybase (ASE) will try to determine a column's NULLability in a few different ways:
if column is assigned a 'value' of NULL (as in the example: col1 = null) then ASE will configure the column to allow NULLs
if the column's value is being copied from another table then the source column's NULLability will be used in determining the new column's NULLability
if the query explicitly defines the column as NULLable (see example - below) then the column will be configured to allow NULLs
if the database option allow nulls by default is false (ASE default setting) then the column's NULLability will be set to 'not NULL'
when all else fails ...
if the database option allow nulls by default is true then the column's NULLability will be set to 'NULL'(able)
An example of explicitly defining the column to allow NULLs:
select col1 = convert(varchar(35) null,'test') into #otherTable

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);

What's wrong with this Postresql ALTER TABLE?

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.

alter data type of existing column from bigint to varchar in Apache derby

I am using Apache derby database v 10.9.1.0. There is one existing table Country-having column LawID of type bigint. It contains records having integer data only. Due to some business reason, I need to alter its data type from 'bigint' to 'varchar' . I tried following two ways to alter existing table. But both ways did not work.
a. first way
ALTER TABLE Country ADD COLUMN LawID_NEW VARCHAR(50);
UPDATE Country SET LawID_NEW = LawID;
ALTER TABLE Country DROP COLUMN LawID;
RENAME COLUMN Country.LawID_NEW TO LawID;
It shows message like :Columns of type 'VARCHAR' cannot hold values of type 'BIGINT'.
b. second way
ALTER TABLE Country ALTER LawID SET DATA TYPE VARCHAR(50);
It shows error message like : Invalid type specified for column 'LawID'. The type of a column may not be changed.
Any help related to correct alter query is highly appreciated, Thanks
I think the first method would work with this change:
UPDATE Country SET LawID_NEW = TRIM(CHAR(LawID));
ALTER TABLE tablename MODIFY columnname VARCHAR(20);
This works in mysql. Give it a try.
Or
ALTER TABLE table CHANGE columnname columnname VARCHAR(20);