Change date format for multiple records SQL Query - sql

I'm having old system which is developed in MS Access and I've a requirement to convert it a web application.
In the old application we have a MS Access Database and we need to import it to SQL Server.
Now there are many tables which have datetime columns with datatype as below:
Access: LongText
SQL Server: nvarchar(MAX)
I've successfully imported the MS Access database to SQL Server but now I'm stuck in datetime conversion issue.
There is a table which have around 86000 records and we need to convert it column to datatype datetime.
currently column have nvarchar(MAX) datatype with
"dd-mm-yyyy" and here is the screenshot for the same:
https://www.screencast.com/t/OP9edlsTdbR
I've tried to fix it by doing google but nothing work as expected.

convert your all string values to appropriate style : 105 : dd-mm-yyyy
ALTER TABLE dbo.TableName ALTER COLUMN DateNeed DATETIME NULL[NOT NULL]
GO
UPDATE TableName SET DateNeed = CONVERT(DATETIME, DateNeed , 105)

Related

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;

Date Format Issues SQL Server 2012

I have an application which imports a DAT file into some SQL Server tables.
The DAT file sometimes contains incorrect date formats.
Example:
DateCreated = 000100893
The DateCreated column in SQL Server is a datetime type.
The application fails when importing the data due to incorrect format and I have to manually null out these values so I can re-import.
Is there a way in SQL to have restrictions on the datetime column? For example, if the data is not in the correct format, automatically null out the column? I cannot change the datetime datatype for that column because most of the time the dates are correct and I am using this column for other calculations.
You are looking for the TRY_PARSE function:
SELECT TRY_PARSE(DateCreated AS datetime)
It returns null if DateCreated can't be cast into datetime
According to TRY_CONVERT (Transact-SQL), you can do :
SELECT TRY_CONVERT(datetime2, '000100893') AS Result;

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.

converting nvarchar(50) to datetime

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.

Convert a string to a date in Access

I'm migrating data between tables in Access 2003. In the old table, the date was stored as a text field in the format YYYYMMDD.
I want to store the field as a datetime in the new table. I've tried using CDate() in my SQL statement, but it just displays as #Error in the results.
What am I doing wrong?
e.g. cdate(format("20091231", "####/##/##"))
So, in your case it will be
SELECT cdate(format(mystringFieldThatIsInYYYYMMDDFormat, "####/##/##"))
FROM myData