1
Thank you guys, I was able to convert the nvarchar field to datetime simply as follows:
UPDATE tb_Processo SET DataParcelaAntiga = TRY_CAST(DataParcelaAntiga AS DATETIME)
1
1
Thank you guys, I was able to convert the nvarchar field to datetime simply as follows:
UPDATE tb_Processo SET DataParcelaAntiga = TRY_CAST(DataParcelaAntiga AS DATETIME)
You changed the type on your table ?
In reality this way you will probably convert into a wrong format, mainly because it will be considered the year as the first part before the /
and to convert VARCHAR
for VARCHAR
True Sorack! got an invalid value when converting direct.
1
Create a new column with the new type, update it with the converted values, delete the old column, create again as DATETIME
, update it with the values of the new column and delete this auxiliary column:
-- Cria a coluna auxiliar para recer um DATETIME
ALTER TABLE tb_processo ADD DataParcelaAntigaAUXILIAR DATETIME;
GO
-- Atualiza a coluna com o valor referente ao formato dd/MM/yyyy
UPDATE tp
SET tp.DataParcelaAntigaAUXILIAR = CONVERT(datetime, tp.DataParcelaAntiga, 103)
FROM tb_processos tp
WHERE ISNULL(tp.DataParcelaAntiga, '') <> '';
GO
-- Elimina a coluna antiga
ALTER TABLE tb_processo DROP COLUMN DataParcelaAntiga;
GO
-- Recria a coluna antiga com o novo tipo
ALTER TABLE tb_processo ADD DataParcelaAntiga DATETIME;
GO
-- Atualiza o valor com base na coluna auxiliar
UPDATE tp
SET tp.DataParcelaAntiga = tp.DataParcelaAntigaAUXILIAR
FROM tb_processos tp
WHERE ISNULL(tp.DataParcelaAntigaAUXILIAR, '') <> '';
GO
-- Elimina a coluna auxiliar
ALTER TABLE tb_processo DROP COLUMN DataParcelaAntigaAUXILIAR;
GO
The other way is to create the helper column, convert the values, delete the old one and rename the helper to get the same name as the old one:
-- Cria a coluna auxiliar para recer um DATETIME
ALTER TABLE tb_processo ADD DataParcelaAntigaAUXILIAR DATETIME;
GO
-- Atualiza a coluna com o valor referente ao formato dd/MM/yyyy
UPDATE tp
SET tp.DataParcelaAntigaAUXILIAR = CONVERT(datetime, tp.DataParcelaAntiga, 103)
FROM tb_processos tp
WHERE ISNULL(tp.DataParcelaAntiga, '') <> '';
GO
-- Elimina a coluna antiga
ALTER TABLE tb_processo DROP COLUMN DataParcelaAntiga;
GO
-- Renomeia a coluna "DataParcelaAntigaAUXILIAR" da tabela "tb_processo" para "DataParcelaAntiga".
EXEC SP_RENAME 'tb_processo.DataParcelaAntigaAUXILIAR', 'DataParcelaAntiga', 'COLUMN';
GO
Changes the name of a user created object in the current database. This object can be a table, index, column, alias data type or data type
CLR
user-definedMicrosoft .NET Framework Common Language Runtime
.
I did according to the script, but also get an incorrect value in the conversion fields with dates were with a minimum date: 1900-01-01 00:00:00.000
@Evandro can put an example of how the recorded date is?
added an image like this to the table in the database [1]: https://i.stack.Imgur.com/j0YcT.jpg
@Evandro adjusted the solution to encompass the cases that were with the incorrect conversion. Please check.
Thanks the second conversion worked perfectly, good job.
1
You need to create a new column to receive your converted data and then delete the old column, after that just rename the new column.
An important detail in your select is the filtering of the data, as you have EMPTY fields, this can generate default values in the conversion.
Then you need to filter your fields NULL
and the VAZIOS
to make your conversion work without the minimum dates: 1900-01-01 00:00:00.000
what causes this is your EMPTY fields, but as there is no way to convert a NULL to a date we will filter them too.
declare @tb_processos table
(
id int,
DataParcelaAntiga nvarchar(50)
)
insert into @tb_processos values
(1, '27/02/2017'),
(2, '28/02/2017'),
(3, ''),
(4, null)
select * , CONVERT(datetime, DataParcelaAntiga, 103) from @tb_processos
where DataParcelaAntiga is not null
and len(DataParcelaAntiga) > 0
Your update would look like this.
UPDATE P
SET P.DataAux = CONVERT(datetime, P.DataParcelaAntiga, 103)
from tb_processos as P
where P.DataParcelaAntiga is not null
and len(P.DataParcelaAntiga) > 0;
Browser other questions tagged sql database sql-server tsql
You are not signed in. Login or sign up in order to post.
I believe that the database will not allow direct conversion, you can create a temporary column, do the conversion for it, delete the varchar column and recreate it as datetime, and finally copy the data from the temporary column to the new column
– Rovann Linhalis
This may help, I do not understand English, https://stackoverflow.com/questions/29776360/convert-varchar-column-to-datetime-in-sql-server
– user60252
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-rename-transact-sql a step can be renamed to the original name.
– Motta