I have a table which has a VARCHAR(MAX) column, and I need to change it to VARBINARY(MAX).
I tried using the command
ALTER TABLE TableName ALTER COLUMN ColumnName VARBINARY(MAX)
but I got the error
Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type varchar(max) to varbinary(max) is not allowed.
Use the CONVERT function to run this query.
The table has no data, so I can't understand why it's complaining about data conversion.
You cannot perform this conversion using an ALTER TABLE statement since converting from varchar(max) to varbinary(max) requires an explicit conversion. So you should follow these steps to alter your table:
Alter table with new column of VARBINARY(MAX)
If you have existing data in the VARCHAR(MAX) column, use update statement to add the data to the VARBINARY column
Alter table to drop the VARCHAR(MAX) column
Rename varbinary column to varchar name (per comment from #Ben Thul)
Convert Varchar to Int and then change Int to Binary.
Related
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
I accidentally created a column with the wrong type NVARCHAR (for storing password salts) and I want to convert it to NVARBINARY.
I tried
ALTER TABLE [dbo].[TableName]
ALTER COLUMN [ColumnName] [varbinary] (20) NOT NULL
GO
but it says
Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query.
Is there a way to do that? CONVERT seems to be for expressions only, not for alterations.
The only way by altering will be someting like:
Create Table a (id int,blb Nvarchar(10))
insert into a Values
(1,'Test'),
(2,N'Test2');
BEGIN Transaction
ALTER TABLE a
ADD blb_New [varbinary] (20) NULL
GO
UPDATE a
SET blb_new = CAST(blb AS varbinary(20))
GO
ALTER TABLE a
DROP COLUMN blb
GO
EXEC sp_rename 'a.blb_new', 'blb', 'COLUMN'
GO
COMMIT Transaction
Select *,CAST(blb as Nvarchar(20)) from a
Drop Table a
You may first convert all the values from NVARCHAR to NVARBINARY in the same column.
After converting all the values use Alter table
You may refer the following link:
Converting NVARCHAR(255) to DATE
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;
I have a SQL Server 2008 table which contains an external user reference currently stored as a bigint - the userid from the external table. I want to extend this to allow email address, open ID etc to be used as the external identifier. Is it possible to alter the column datatype from bigint to varchar without affecting any of the existing data?
Yes, that should be possible, no problem - as long as you make your VARCHAR field big enough to hold you BIGINT values :-)
You'd have to use something like this T-SQL:
ALTER TABLE dbo.YourTable
ALTER COLUMN YourColumnName VARCHAR(50) -- or whatever you want
and that should be it! Since all BIGINT values can be converted into a string, that command should work just fine and without any danger of losing data.
I have an existing column of data type varchar that I need to change to nvarchar, however I do not want to alter the existing column width of (5).
If I use the following statement
ALTER TABLE MYTABLE ALTER COLUMN MYCOLUMN NVARCHAR (5) NOT NULL
I end up with a column of nvarchar data type, but with a column width of (10)!
If I try the following statement without specifying a column width
ALTER TABLE MYTABLE ALTER COLUMN MYCOLUMN NVARCHAR (5) NOT NULL
I then end up with an nvarchar column with a width of (2)
How can I simply change the column data type from varchar to nvarchar without affecting the existing column width?
Thank you!
NVARCHAR is a UNICODE UCS-2 type and will be twice as wide as VARCHAR, by design.
Please have a very close read of SQL Server Books Online to see the difference between SQL data types. I've included a link.