The data is present in the column in nvarchar format, I need to change the data type of that column to Datetime.
There is another problem the data saved has 2 formats one is dd/mm/yyyy and other is mm/dd/yyyy. How can I achieve this?
I have tried the following but obviously I have got an error.
Used
UPDATE SalesRegister
SET followup = PARSE(FollowUpDate AS datetime);
Error:
Error converting string value '20/07/2014' into data type datetime using culture ''
Can someone guide me with this query? I was thinking one more way of doing this which can be first converting the format in mm/dd/yyyy or dd/mm/yyyy and than apply the alter statement. But I don't know this either.
Related
I am trying to use an Update SQL query to set a series of values in a table, however I am facing an issue whereby I keep getting the error message
Conversion failed when converting the varchar value
'&[NAS_APIConfig.ProcessID_FS]&' to data type smallint.
Here is an example of a query I am using:
UPDATE tblNAS_APIConfig
SET ProcessID_FS = '&[NAS_APIConfig.ProcessID_FS]&',
ScheduleType = '&[NAS_APIConfig.ScheduleType]&'
The data is being stored in a collection within Blue Prism and was pulled from a different database with a data type of smallint. I'm guessing that when the data is being pulled into Blue Prism it is getting cast as a varchar data type. What is the correct syntax to use the UPDATE SET query while casting the item as smallint?
Thanks
I want to convert a column in the format mm/dd/yyyy to datetime, but when I do I get the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I found in other posts that this means that some dates don't make sense, such as 10/35/2021. I tried to find the wrong dates by slicing the varchars to get the dates with SUBSTRING(date, 3, 2) but it turns out some dates are in the form m/d/yyyy, so when I slice I get something like 1/.
I have no idea how to find the wrong dates, and how to (even though there are wrong dates) convert everything to datetime.
Thanks!
If some of your data is in MM/dd/YYYY and some M/d/yyyy this really makes for a bit of a mess. I would likely do something like this:
--Add a new varchar column (yes varchar) to save a copy of your bad data
ALTER TABLE dbo.YourData ADD BadDate varchar(10);
GO
--Change the data you have to the ISO format yyyyMMdd and store the bad data in the BadDate column
UPDATE dbo.YourData
SET DateColumn = CONVERT(varchar(8),TRY_CONVERT(date,DateColumn,101),112),
BadDate = CASE WHEN TRY_CONVERT(date,DateColumn,101) IS NULL THEN DateColumn END
WHERE DateColumn IS NOT NULL;
GO
--Change data type of your data column
ALTER TABLE dbo.YourData ALTER COLUMN DateColumn date NULL;
GO
--You can view your bad data with:
SELECT BadData
FROM dbo.YourData
WHERE BadData IS NOT NULL;
If you just want to locate rows with values that are obviously bad dates - disregarding any ambiguities - just use try_convert and check for NULLs
eg
with dates as (
select * from (values('01/02/2021'),('02/01/2021'),('33/02/2021'),('01/13/2021'))v(d)
)
select *
from dates
where Try_Convert(date, d) is null
A long time ago I created a database, and completely forgot to set the column to Date and now looking at the data, I want to extract, it looks like 2006-05-06.
How would I run a SQL statement to convert it into the correct format (dd/MM/yyyy) 06/05/2006, I'm running with the British format "103".
What I was planning on doing, I've already added a second column (s_batch_convert2) to the database, hoping to convert into that and then delete the original column (s_batch_convert), renaming the new column to the old one.
UPDATE s_service_repairs
SET s_batch_convert2 = TRY_CONVERT(Date, s_batch_convert, 103)
Am I along the right lines?
You should convert your existing column to a bona fide date. It seems to have the right format:
alter table s_service_repairs alter column s_batch_convert date;
Then you can add a computed column for the format you want:
alter table s_service_repairs s_batch_convert_mmddyyyy as ( try_convert(varchar(10), s_batch_convert, 103) );
I am using SQL Server 2005 and am trying to match in a Dimension table the date field with data:
2012-01-06 00:00:00.000 as a datetime
to a staging table with data that comes in as a nvarchar(50)
2012-01-06 15:53:12.040
for example. I have tried all of the converts and casts that I can find on the searches but am getting an "Arithmetic overflow error converting expression to data type datetime." error message.
I have updated all of the time values manually in the staging table to have 00:00:00.000 time stamps but still the problem is there.
Can anyone help please?
Are you just trying to change the data type of the entire column?
If so, try this
ALTER TABLE table_name
ALTER COLUMN column_name datetime
More info.
If you are trying to just modify for a query, you can try this:
SELECT convert (...look at link below for options... )
Look here for more info.
I am relatively new to SQL Server and I am trying to update the datatype of about 3000 records from a Char to Datetime so I can use the DATEDIFF function. I created a view that achieves the conversion but what I think I need to do is alter the data in the origin table.
SELECT
CONVERT(datetime, CONVERT(char(8), TRANS_ACCOUNTINGDATE_ALLCAMPAIGNS_2010_ALLPROCESSINGACCOUNTS_ALL))
FROM Accounting;
What I think I need to do is an alter table and iterate over each row performing the conversion. Trying to change the data type using the GUI is not working for me.
Any help would be appreciated.
Thanks
The datatype is an attribute of the COLUMN, not just of the data inside the column. You can't put datetime data into a char field - that's the purpose of data types!
You need to add a new field and run an UPDATE statement to populate it with your converted data. Then you can drop the original field and rename your new one to the original name.