Convert varchar to numeric in vertica - database

Is It possible to change varchar(10) column to data type numeric(10,4) using alter statement.
I tried it with alter command but getting an arror:
cannot convert column from varchar(10) to numeric(10,4)

You can't convert directly from VARCHAR to a numeric type, but you can work around the restriction by creating a temporary (numeric) column, populating it based on the current column, and then renaming it. When populating it, you'll need to make sure that the value is actually a number, for example that it does not contain commas as separators, punctuation like $, etc.
The documentation gives this example, where the original price column has values like '$50.00':
ALTER TABLE sales ADD COLUMN temp_price NUMERIC(10,2) DEFAULT
SUBSTR(sales.price, 2)::NUMERIC;
ALTER TABLE sales ALTER COLUMN temp_price DROP DEFAULT;
SELECT MAKE_AHM_NOW();
ALTER TABLE sales DROP COLUMN price CASCADE;
ALTER TABLE sales RENAME COLUMN temp_price to price;

Related

How to change datatype VARCHAR to DATETIME?

My current Database column's datatype is varchar, but I want to alter it to datetime. I use this sqlserver code to alter it, but failed.
ALTER TABLE Logbook
ALTER COLUMN Company_Date datetime NOT NULL;
Result:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
The easiest option here might be to use TRY_CONVERT:
ALTER TABLE Logbook ADD COLUMN Company_Date1 datetime;
UPDATE Logbook
SET Company_Date1 = TRY_CONVERT(datetime, Company_Date);
ALTER TABLE Logbook DROP COLUMN Company_Date;
sp_rename 'Logbook.Company_Date1', 'Company_Date', 'COLUMN';
The strategy here is to create a new datetime column Company_Date1 which is a bona fide datetime column. Then, we update it using TRY_CONVERT against the text values in Company_Date. Note that should the conversion not be possible, there would be no error, but instead a NULL would be returned. Finally we drop the original text Company_Date column and rename Company_Date1 to Company_Date.
you have some bad data in column one way you can use isdate function before alter column such as
update logbook set company_date=null where isdate(company_date)=0
after that alter column
another way select the list of bad data and correct it then alter column

is it impossible to modify size of column having char type in Derby?

I wanna modify size of column having CHAR type in Derby.
- existing size of the column = CHAR(2)
- the size i want = CHAR(3)
ALTER TABLE Test
ALTER LOG SET DATA TYPE CHAR(4)
But, if i try that, "only VARCHAR, CLOB, or BLOB Type is possible" is printed...
SO, is it impossible to modify size of column having char type in Derby?
You can't use a simple 1-line ALTER TABLE ... ALTER COLUMN statement to change a CHAR data type; see https://db.apache.org/derby/docs/10.13/ref/rrefsqlj81859.html
You can, however, use a more complicated 4-step process:
ALTER TABLE ... ADD COLUMN ... to add a new CHAR(3) column
UPDATE ... SET ... to set the new column's values from the old column's values.
ALTER TABLE ... DROP COLUMN ... to drop the CHAR(2) column
RENAME COLUMN to rename the CHAR(3) column to have the same name as the old CHAR(2) column.
Naturally, make a backup of your database first before running commands like these.

Alter Column: option to specify conversion function?

I have a column of type float that contains phone numbers - I'm aware that this is bad, so I want to convert the column from float to nvarchar(max), converting the data appropriately so as not to lose data.
The conversion can apparently be handled correctly using the STR function (suggested here), but I'm not sure how to go about changing the column type and performing the conversion without creating a temporary column. I don't want to use a temporary column because we are doing this automatically a bunch of times in future and don't want to encounter performance impact from page splits (suggested here)
In Postgres you can add a "USING" option to your ALTER COLUMN statement that specifies how to convert the existing data. I can't find anything like this for TSQL. Is there a way I can do this in place?
Postgres example:
...ALTER COLUMN <column> TYPE <type> USING <func>(<column>);
Rather than use a temporary column in your table, use a (temporary) column in a temporary table. In short:
Create temp table with PK of your table + column you want to change (in the correct data type, of course)
select data into temp table using your conversion method
Change data type in actual table
Update actual table from temp table values
If the table is large, I'd suggest doing this in batches. Of course, if the table isn't large, worrying about page splits is premature optimization since doing a complete rebuild of the table and its indexes after the conversion would be cheap. Another question is: why nvarchar(max)? The data is phone numbers. Last time I checked, phone numbers were fairly short (certainly less than the 2 Gb that nvarchar(max) can hold) and non-unicode. Do some domain modeling to figure out the appropriate data size and you'll thank me later. Lastly, why would you do this "automatically a bunch of times in future"? Why not have the correct data type and insert the right values?
In sqlSever:
CREATE TABLE dbo.Employee
(
EmployeeID INT IDENTITY (1,1) NOT NULL
,FirstName VARCHAR(50) NULL
,MiddleName VARCHAR(50) NULL
,LastName VARCHAR(50) NULL
,DateHired datetime NOT NULL
)
-- Change the datatype to support 100 characters and make NOT NULL
ALTER TABLE dbo.Employee
ALTER COLUMN FirstName VARCHAR(100) NOT NULL
-- Change datatype and allow NULLs for DateHired
ALTER TABLE dbo.Employee
ALTER COLUMN DateHired SMALLDATETIME NULL
-- Set SPARSE columns for Middle Name (sql server 2008 only)
ALTER TABLE dbo.Employee
ALTER COLUMN MiddleName VARCHAR(100) SPARSE NULL
http://sqlserverplanet.com/ddl/alter-table-alter-column

Convert char columns to nvarchar, in order to change the codepage (language encoding) for data already in the table?

I have a table that was imported from another source as a column with the char datatype, but the field has international characters. I would like to use nvarcvhar in my table. How can I achieve this?
I already tried updating the columns (using an alter table statement, or "cast as") but the info stored did not get converted.
Thanks
like this
ALTER TABLE TableName ALTER COLUMN ColumnName NVARCHAR(size)
example
CREATE TABLE test (bla VARCHAR(50))
GO
ALTER TABLE test ALTER COLUMN bla NVARCHAR(50)
Make sure that you prefix the string with N when doing the insert
INSERT test VALUES (N'漢語')
SELECT * FROM test
output
bla
--------------------------------------------------
漢語
(1 row(s) affected)
Whatever you do, don't use the SSMS designer/wizard, it will recreate the whole table, see here for example When changing column data types use ALTER TABLE TableName ALTER Column syntax, don't drop and recreate column
alter table your_table alter column your_column nvarchar(length)
SQLFiddle example
Your data is currently in EUC-CN, masquerading as CP1252. It is not lost.
You have several approaches available for conversion to Unicode (look at Converting SQL databases section for overview). In case of SQL Server, you can create extended stored procedures for conversion from EUC-CN to Unicode. This will work but it is not exactly easy (in this case use code page 51936 for your data).
With some luck, depending on what particular characters occur in your data, and what language packs you have installed, you might be able to convert as if from code page 936 like this:
ALTER DATABASE mydatabasename COLLATE Chinese_PRC
If this succeeds, do the same for every column you are going to convert:
ALTER TABLE mytablename ALTER COLUMN mycolumnname
varchar(4000) COLLATE Chinese_PRC NOT NULL
And only then convert them to NVARCHAR.

Converting Geography data type column to a NVARCHAR

I have a column in my database of data type Geography which I want to convert to a varchar.
I am using the following query to make do this ...
-- 1: Issue with Microsoft.SqlServer.Types.dll.
ALTER TABLE RetailerStore ALTER COLUMN Location VARCHAR(50)
However, I get the error message
Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type geography to varchar is not allowed. Use the CONVERT function to run this query.
Has anybody come across this and if so do they know a way to resolve it?
There is no implicit conversion. Is there data in the column? If so, set it all to NULL before attempting to change the column type. Alternatively, you can drop and recreate the column.
Note that there is an explicit conversion from geography to a text form - you can use the STAsText to convert the spatial type to a text representation.
Using ALTER TABLE to DROP and ADD:
ALTER TABLE RetailerStore DROP COLUMN Location;
ALTER TABLE RetailerStore ADD Location VARCHAR(50) NULL;
Drop the column from your table first:
ALTER TABLE RetailerStore DROP COLUMN Location;
And then add that column again with the desired datatype and range like given below:
ALTER TABLE RetailerStore ADD Location VARCHAR(100) NULL;

Resources