Alter sql Table data format - sql

I have a Nvarchar column in a database table which contains date values. Date values are stored in two formats
2008-05-20
22/04/2011
Now I need to convert this column to a date column. When I'm try the following query:
set dateformat dmy
alter table tblDocumentRevision
alter column RevisionDate date
it returns an error:
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
The statement has been terminated.
Please help me to solve this

Update the date string in the table so they all have the same format (one that SQL server can cast from) before you change the type to DateTime.

Related

Change column datatype from NChar to Date

I need to change a column's datatype from nchar to date in SQL Server.
I have a column dealdate nchar(16) which stores data like this: '2020-08-13'
I need to change this column's datatype to DATE.
When trying to change, I get an error:
can't change DATE from nchar
I try to use this command
ALTER TABLE [dbLedger].[dbo].[tblPurchBook]
ALTER COLUMN DealDate DATE NULL;
Validate the date values first using try_convert() or try_cast():
select dealdate
from [dbLedger].[dbo].[tblPurchBook] pb
where try_convert(date, dealdate) is null and dealdate is not null;
If this returns any rows, then you need to fix the values before you can change the type.
One method is to set the bad values to NULL, which you can do with:
update [dbLedger].[dbo].[tblPurchBook] pb
set dealdate = null
where try_convert(date, dealdate) is null and dealdate is not null;
I see tried about it. but I have not solved.
"ALTER TABLE [dbLedger].[dbo].[tblPurchBook]
ALTER COLUMN DealDate DATE NULL;"
This command used MSSQL V18.6
I got an error on this.
"ERROR MESSAGE
Msg 241, Level 16, State 1, Line 1
문자열을 날짜 및/또는 시간으로 변환하지 못했습니다.
문이 종료되었습니다.
(cannot convert charator to date or time
did closed statement)
Completion time: 2020-08-19T11:38:06.5978013+09:00"
Is there other way?
Thanks

Why does MS SQL Server error on inserting the second row but not on the first row?

I have an SQL table that will let me insert some rows with a datetime field but not all rows and I can't see why not. The error I get is
'The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.'
To try and work this out I created a temporary table with 1 colum and tried to add the two datetimes that are causing the issue.
create table #DateTimeTest ( created datetime );
I then try inserting these two rows
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-12 05:46:00.00');
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-19 05:31:00.00');
The first row is inserted without any problems, the second-row errors, but I can't understand why.
(1 row affected)
Msg 242, Level 16, State 3, Line 10
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Completion time: 2020-07-15T11:28:43.6451779+01:00
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-19 05:31:00.00');
Apparently, your date format is YDM rather than YMD. You can fix this in several ways.
One method is to use an unambiguous date/time format. In your case, that would include a T:
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-12T05:46:00.00');
insert into dbo.#DateTimeTest (created) VALUES ('2020-06-19T05:31:00.00');
The reason this works is because the YYYY-MM-DDTHH:MI:SS format is the standard format for a date/time format in SQL Server -- unambiguously and not affected by dateformat. Similarly YYYYMMDD (note: no hyphens) is the standard, unambiguous format for a date constant in SQL Server, but YYYY-MM-DD is subject to dateformat.
A second method would be to set the dateformat to YMD:
set dateformat YMD;
Here is a db<>fiddle illustrating this.
A third method would be to explicitly extract the datetime from the string, using a function such as convert() -- and perhaps a few string operations as well.

sql type conversion

I have a column datedocumented in the format YYYYMMDD and of the datatype nvarchar. I want to change the data type to datetime and update the column name exdatedocumented and alter the table using ALTER .can anyone help in this.I have tried something like this
update dbo.table2
set [DateDocumented] = convert(datetime,CAST([DateDocumented] as datetime),130)
I ended up getting error
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated
.
You should be able to just change the column type:
alter dbo.table2 alter column DateDocumented datetime;
Your column is in a format suitable for conversion. If you wanted to use default formats instead, just do:
update table dbo.table2
set [DateDocumented] = convert(datetime, CAST([DateDocumented] as datetime));
This should also allow the column to be converted to a datetime.
1st change that column name then alter table
sp_RENAME 'dbo.table2.datedocumented', 'exdatedocumented' , 'COLUMN'
go
ALTER TABLE dbo.table2
ALTER COLUMN exdatedocumented datetime;
It seems we have two things going on here. Changing the type for a column. And changing the name for a column. Let's try to keep these separate so not to confuse things. I will adress the changing of type (from varchar to datetime):
First of all: Why datetime? Why note datetime2 (with whatever fractions of seconds you want, for instance datetime2(0))? The new types for date and time has been around for 10 years now!
Anyhow, you apparently have values in your table which are not valid dates! First thing you need to do is to find those rows and handle them. Lets say that you will change to datetime2(0), if not, then just change below to datetime instead:
SELECT *
FROM dbo.Table2
WHERE TRY_CAST(DateDocumented AS datetime2(0)) IS NULL

Bulk insert error, when loading date format

I'm receiving a lot of .csv files that I need to bulk insert into a SQL table.
In the csv file the date format is in YYYY-MM-DD and a seperate column for time which is in format HH:MM:SS.
"2016-11-24";"01:00:16"
In my table I created the two columns as a date datatype and a time datatype and using this piece of code to insert:
BULK
INSERT [dbo].[table_name]
FROM 'filepath'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)
But I get this error:
Msg 4864, Level 16, State 1, Line 37
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (date).
If I use a normal insert into statement it works...
First check your physical table date and time columns data type. It has datetime format, which means first you move all .csv file data to a temporary table with varchar datatype. After inserted in temporary table then move to physical table with use of datetime conversion.

The conversion of a date data type to a datetime data type resulted in an out-of-range value

I'm having a table which has one column and it's data type is Date
Column_name Type Computed Length
colunmname date no 3
How can I convert this datatype to Datetime.
I've tried below but it gives error:
ALTER TABLE tableName ALTER COLUMN columnname DATETIME
Error:
Msg 242, Level 16, State 3, Line 1 The conversion of a date data type
to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Can anyone please help me!
It worked like this for me
ALTER TABLE NAME_TABLE ALTER COLUMN NAME_COLUMN datetime2 not null