converting nvarchar(50) to datetime - sql

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.

Related

SQL - Conversion failed when converting date and/or time from character string

I have 3 tables in the database that I'm working on. Out of 3, two of the tables have columns that include dates. When I checked the information schema of the columns I found that dates have the wrong data type. If you see the picture below, the highlighted columns should be stored as DATE data type.
So, I used the following query to change their data type from varchar to DATE:
ALTER TABLE [dbo].[Customer]
ALTER COLUMN DOB DATE;
ALTER TABLE [dbo].[Transactions]
ALTER COLUMN tran_date DATE;
The error that I get is:
Conversion failed when converting date and/or time from character string.
Please let me know how I can fix this error. Thanks!
What you can do is update the value using try_convert() first and then alter the column name. Note: This will set any invalid values to NULL.
update customer
set dob = try_convert(date, dob);
alter table customer alter column dbo date;
If you want to see the bad values, then before you change the table, run:
select c.*
from customer c
where try_convert(date, dob) is null and dob is not null;
You may have other ideas on how to fix the values.
You can't change from varchar to date or time or datetime by altering the column. Why? Because SQL Server does not know if the field contains '1/1/2020' or 'My dog is cute'. You will have to rebuild the table with the proper data types and then CAST() or CONVERT() the values to a true date time.
Underneath the hood, this makes more sense. A char/varchar uses one byte per character. nchar/nvarchar uses 2 bytes per character. A datetime is a number, not a character. This means you need a routine that changes this into the correct number. If you want to get deeper, the number is the number of ticks (nanoseconds) since midnight on January 1, 0001 in the Gregorian Calendar. More info.

tSQL - convert nvarchar(8) to time only

I am using Microsoft SQL Server Management Studio to manipulate Public transport system database.
I have a table in database named 'dbo.tblStop_times', two columns of which should be of data type time. At the moment, they are both nvarchar and they have data stored in a pattern like this - "07:39:00" (without quotes)
I had same question with date columns as well, but found a solution for that on stackoverflow just few hours back.
Below works fine for conversion of nvarchar column to date column.
ALTER TABLE tblCalendar ALTER COLUMN [start_date] DATE NOT NULL;
I am not sure if what I want is achievable or not, because the above mention conversion works just fine, I assume it might be possible.
What I have atm is - nvarchar(8), what I want it to be is sql time data type, something like hh:mm:ss [and if possible - without trailing nnnnnn - nanoseconds component]
You should be able to do:
ALTER TABLE tblStop_times ALTER COLUMN start_time TIME NOT NULL;
Here is a rextester.
EDIT:
If you don't have valid time values, then you have a problem. You should first look for the values:
select col
from tblStop_times
where try_convert(time, col) is null;
This will show you the values that cannot be converted. If you like, you can NULL them out so the alter will work:
update tblStop_times
set col = NULL
where try_convert(time, col) is null;

HashBytes with datetime2 field

I am creating a hash key using hashbytes on multiple columns to get performance gain which we are using right now in where clause.
alter table dbo.Table1
add HashKey AS CAST(hashbytes('MD5', PID+PNumber+CONVERT([varchar] (50),[DateStamp]) +CONVERT(VARCHAR(50), TransactionCount)+OCD+ONbr+TransactionID) AS VARBINARY(80)) PERSISTED
But one of the column in that is a datetime2 field which i am unable to add. While i was trying i am getting below error message
"Computed column 'HashKey' in table 'table1' cannot be persisted because the column is non-deterministic.".
From my research i found that datetime2 cannot be used as it is non-deterministic.
But i cannot change the format as i need to compare the value exactly as it is including all milliseconds.
Can anybody please give me a work around?.Any alternate solution will be appreciated.
I am not sure of implications..
But casting datetime to binary always gives new value.see below for Example..
select getdate(),cast(getdate()as binary)
2016-08-02 10:17:20.573 0x000000000000000000000000000000000000000000000000A65600A98EEC
2016-08-02 10:17:40.537 0x000000000000000000000000000000000000000000000000A65600A9A651
so try like..
select hashbytes('md5',cast(getdate()as binary))

Alter column from nvarchar to DateTime SQL Server

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.

Convert datatype of existing data from Char to DateTime with SQL

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.