Hi i want to alter my table from nvarchar to float when my table contain values
i'm trying this code:
alter table MY_TABLE alter column PRICE float
But i get error Msg 8114, Level 16, State 5, Line 1 Error converting
data type nvarchar to float.
You have a problem, because some value cannot be converted to a float.
So, get rid of that value first:
update my_table
set price = try_convert(float, price);
Then the conversion will work:
alter table MY_TABLE alter column PRICE float;
I should note: float is a bad data type for a "price" column. It should really be a numeric or money type.
In SQL Server 2008, use isnumeric() instead:
update my_table
set price = NULL
where isnumeric(price) = 0
First Update the bad records
Update MY_TABLE set PRICE = Replace(Replace(Price,',','.'),' ','')
Then run the alter
alter table MY_TABLE alter column PRICE float;
To Insert into new table
Insert into table_name (Price,col1,..)
Select Replace(Replace(Price,',','.'),' ',''),col1,col2,..
From MY_TABLE
FLOAT is a approximate datatype will not store exact value
Review Data that can't be converted
Select * from MY_TABLE where IsNumeric(PRICE)=0
If this data can be destroyed or has no value otherwise edit where necessary
Update MY_TABLE Set PRICE = null where IsNumeric(PRICE)=0
Alter table MY_TABLE alter column PRICE float
Related
I have the following table:
SELECT * FROM [dbo].[Dimension_Game_Developer]
Table Schema:
Sample Data of select statement output:
I am trying to convert the string date as Year only date using the following:
update [dbo].[Dimension_Game_Developer]
SET [Year_of_Release] = CAST([Year_of_Release] as DATE)
How would I make that change? Perhaps convert is better the CAST for this?
You need to cast Year_of_Release as int as shown below.
update [dbo].[Dimension_Game_Developer]
SET [Year_of_Release] = CAST(Convert(Varchar(4), [Year_of_Release]) as int)
You can also convert it to float.
update [dbo].[Dimension_Game_Developer] SET [Year_of_Release] =
CAST([Year_of_Release] as float)
To solve the issue permanently you should updated the datatype of column Year_of_Release to int and insert the value in the integer further.
To change the column datatype:
ALTER TABLE [dbo].[Dimension_Game_Developer] ALTER COLUMN Year_of_Release int;
to format values without the need for cast and substring
use the function Call Format o try_cast
example:
DECLARE #d DATETIME = '10/01/2011';
declare #tab table (StartDate date, year varchar(6),val1 int,val2 int)
insert into #tab(StartDate,year,val1,val2) values('2014-02-15','2015.0',0,0)
insert into #tab(StartDate,year,val1,val2) values('2015-02-15','2016.0',0,0)
insert into #tab(StartDate,year,val1,val2) values('2016-02-15','2017.0',0,0)
insert into #tab(StartDate,year,val1,val2) values('2017-02-15','2018.0',0,0)
update #tab SET [val1] = FORMAT ( StartDate, 'yyyy'),[val2] =try_cast(year as decimal(18,0))
select * from #tab
these two functions can be found in the sql server documentation
try DatePart(year,CAST([Year_of_Release] as DATE)) Year
Why does this keep happening? The first 2 data types are Int. The 'Games' is DateTime. The last is VarChar. Why is it not executing successfully
CREATE TABLE JaretSchedule
(
FieldID int Primary,
TeamID int Foreign,
Games DateTime,
TeamsPlaying varChar
);
INSERT INTO [dbo].[JaretsSchedule]
([FieldID]
,[TeamID]
,[Games]
,[TeamsPlaying])
VALUES('1', '1', '2012-01-20 12:00:00', 'Roadrunners v.s. Cheetahs');
Error Message: String or binary data would be truncated. The statement has been terminated
Because ,you have exceeded the max size of field,
ALTER TABLE JaretsSchedule ALTER COLUMN TeamsPlaying nvarchar(1000);
The default length of varchar when used as a column data type is 1.
varchar [ ( n | max ) ] Variable-size string data
. . .
When n isn't specified in a data definition or variable declaration
statement, the default length is 1. If n isn't specified when using
the CAST and CONVERT functions, the default length is 30.
char and varchar (Transact-SQL)
So you need so set a max length for the column. EG
use tempdb
go
drop table if exists JaretsSchedule
go
create table JaretsSchedule
(
FieldId int,
TeamID int,
Games datetime,
TeamsPlaying varchar(255)
)
go
insert into JaretsSchedule(FieldId,TeamID,Games,TeamsPlaying)
values (1,1,'2012-01-20 10:00:00','Roadrunnersv.s.Cheetahs')
I have a table with a column A.
I would like to change the datatype from string to float.
My column A looks like this:
A
143
1,440
19,630
12
...
When I try to run my code:
ALTER TABLE [Table1]
ALTER COLUMN [A] FLOAT
I always get this error:
Error converting data type varchar to float.
It is clear in error that varchar value is not converting into float. Since , is not allowed in int, float, decimal type values, so first need to replace your , .
Instead of this you may try this.
update table set A = Replace( A, ',', '' )
Or
update table set A = Replace( A, ',', '.' )
Whichever condition suits you better.
After that convert your column.
ALTER TABLE [Table1]
ALTER COLUMN [A] FLOAT
I'm writing data on the Sql Server database.
When data is written, a trigger runs.
TRIGGER
ALTER TRIGGER Alert ON records AFTER INSERT AS
BEGIN
DECLARE #tempmin decimal = 0
DECLARE #current_max_idAlarm int = (SELECT MAX(IdAlarm) FROM alarms)
DECLARE #maxidAlarm int
DECLARE #temp decimal = (SELECT s.lim_inf_temp from sensores s JOIN inserted i ON s.idSensor=i.idSensor )
-- Insert into alares from the inserted rows if temperature less than tempmin
SET IDENTITY_INSERT alarmes On
INSERT alarmes (IdAlarm, desc_alarm,date, idRecord)
SELECT
ROW_NUMBER() OVER (ORDER BY i.idRecord) + #current_max_idAlarm, 'Temp Error', GETDATE(), i.idRecord
FROM
inserted AS i
WHERE
i.Temperature < #temp
end
INSERT
insert into record values ('2014-05-26' ,'14:51:47','Sensor01','---','48.6','9.8');
Whenever I try to record this type of data: '---'
Gives the following error:
Msg 8114, Level 16, State 5, Procedure Alert, Line
Error converting data type varchar to numeric.
I know it is to be in decimal type (DECLARE #temp decimal - TRIGGER), but moving to the type varchar to trigger stops working correctly.
Does someone can help me resolve this error please?
Thank you all.
You are trying to insert --- inside a numeric column, you simply can't do that.
You have mainly 2 options:
Change the data type of the destination column
Choose a different value to insert (like NULL)
I have the following stored procedure, I have two tables here, Movie and Director. Both need to be updated when a new movie has been created or added. How do you handle FKs in stored procedures? The FK in this case is director_id. It is a primary key in Director but a FK in Movie Do I need to specify it twice like so? I am getting conflict errors
CREATE PROCEDURE Book_Book_Creation
#Book_id_arg DECIMAL(12),
#author_id_arg DECIMAL(12),
#type_id_arg DECIMAL(12),
#title_arg VARCHAR(64), -
#copyright_arg DECIMAL(4),
#dauthor_id_2_arg DECIMAL(12),
#author_fname_arg VARCHAR (64),
#author_lname_arg VARCHAR (64)
AS
BEGIN
INSERT INTO Book(Book_id, author_id,genre_id, title, copyright)
VALUES (#author_arg, #author_id_arg, #type_id_arg, #title_arg, #copyright_arg);
INSERT INTO Author(author_id, author_fname, author_lname)
VALUES (#director_id_2_arg, #director_fname_arg, #director_lname_arg)
END;
EXECUTE Book_Book_Creation 32,32,1,'Tempting',2013,32,'Taylor','Mendez';
Basically, you just need to do this:
insert into the Director table first
get the newly inserted ID from that table (assuming that the Director_Id column is your primary key and is of type INT IDENTITY)
then insert into the Movie table with that new ID
Something like this:
DECLARE #NewDirectorID INT
INSERT INTO Director (Director_id, Director_fname, director_lname)
VALUES (#director_id_2_arg, #director_fname_arg, #director_lname_arg)
SELECT #NewDirectorID = SCOPE_IDENTITY()
INSERT INTO Movie (Movie_id, director_id,genre_id, title, copyright)
VALUES (#movie_id_arg, #NewDirectorID, #genre_id_arg, #title_arg, #copyright_arg);
I don't see why you would pass in the director's ID as a parameter - twice!
Try this one -
ALTER PROCEDURE dbo.Movie_Movie_Creation12
#movie_id_arg DECIMAL(12),
#director_id_arg DECIMAL(12),
#genre_id_arg DECIMAL(12),
#title_arg VARCHAR(64),
#copyright_arg DECIMAL(4),
#director_fname_arg VARCHAR (64),
#director_lname_arg VARCHAR (64)
AS BEGIN
INSERT INTO dbo.Director (Director_id, Director_fname, director_lname)
SELECT #director_id_arg, #director_fname_arg, #director_lname_arg
INSERT INTO dbo.Movie (Movie_id, director_id,genre_id, title, copyright)
SELECT #movie_id_arg, #director_id_arg, #genre_id_arg, #title_arg, #copyright_arg
END
EXECUTE dbo.Movie_Movie_Creation12
#movie_id_arg = 32
, #director_id_arg = 32
, #genre_id_arg = 1
, #title_arg = 'Argo'
, #copyright_arg = 2012
, #director_fname_arg = 'Ben'
, #director_lname_arg = 'Affleck'