Converting NVARCHAR(255) to DATE - sql-server

I'm trying to transfer some old SQL Server data using Excel into SQL Server. It seems that Import/Export Data application automatically sets most data columns to NVARCHAR(255). Problem I have, is one of my columns is supposed to be a DATE type, but all data in it looks like this 18.08.2000 14:48:15.
So, when I try to use this query:
SELECT CONVERT(Date, DATE_TIME, 113)
FROM someTable
I get this error:
Msg 9807, Level 16, State 0, Line 1
The input character string does not follow style 113, either change the input character string or use a different style.
None of the [styles] from CAST and CONVERT (Transact-SQL) are working in my case.
Any advise or help is greatly appreciated.
SOLVED:
UPDATE myTable
SET columnName = CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105))
ALTER TABLE myTable
ALTER COLUMN columnName SMALLDATETIME

So, if it will ever become useful to anyone, this is the exact code that changes datatype NVARCHAR to DATETIME:
UPDATE myTable
SET columnName = CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105))
ALTER TABLE myTable
ALTER COLUMN columnName SMALLDATETIME

As far as I can tell - style no. 103 (British/French) should work - no?
DECLARE #input NVARCHAR(255)
SET #input = '18.08.2000 14:48:15'
SELECT CONVERT(DATETIME, #input, 103)
Gives me the output of:
2000-08-18 14:48:15.000
which seems pretty reasonable, no??

If you are using SQL Server 2012, try:
PARSE(Date AS datetime USING 'en-GB')
Or if that doesn't work, try:
PARSE(Date AS datetime USING 'Et-EE')
The Estonian culture specifically uses '.' as a date separator (d.MM.yyyy H:mm:ss) so it should work.
(Both seem to work fine on SQL Fiddle)

Related

Conversion of to_timestamp from Oracle to SQL Server format in Insert scripts [duplicate]

This is not asking how to convert an arbitrary string to datetime in MSSQL such as this question.
I can control the string format but I want to know what the MSSQL syntax is for updating a datetime field using a date string.
UPDATE MyTable SET MyDate = CONVERT(datetime, '2009/07/16 08:28:01', 120)
For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
For instance you can use
update tablename set datetimefield='19980223 14:23:05'
update tablename set datetimefield='02/23/1998 14:23:05'
update tablename set datetimefield='1998-12-23 14:23:05'
update tablename set datetimefield='23 February 1998 14:23:05'
update tablename set datetimefield='1998-02-23T14:23:05'
You need to be careful of day/month order since this will be language dependent when the year is not specified first. If you specify the year first then there is no problem; date order will always be year-month-day.

SQL DateTime Inserted Incorrectly From VB.NET

Have a table I'm logging information from a .NET program into.
The VB.NET app explicity dictates the format of the DATETIME string like below
responsedt = Date.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
I then pass this into an INSERT statement that updates my table, however even though the entire setup of the SQL Server is en-GB (British English) the DateTime has gone in the following format:
2019-09-05 19:09:34.823
This was done yesterday so actually should be
2019-05-09 19:09:34.823
The day and month should be switched around, I have tried performing an update on the table post process to get it to update using the following code
FORMAT (xa.daterequested, 'yyyy-dd-MM HH:MM:ss.fff', 'en-gb')
How while this works in a SELECT statement it doesn't seem to work when I do the UPDATE statement.
It is not ideal to have to update all the records dates after the initial INSERT so a solution to either the .NET side of the issue or the SQL would be appreciated as its pickling my head.
You have 2 options to prevent the error from happening again:
Keep dates as date/time data types instead of converting them to strings.
Use formats that are not language or settings dependent. In SQL Server these would be YYYYMMDD hh:mm:ss.msss OR YYYY-MM-DDThh:mm:ss.mss (notice the T between date and time)
To correct the dates already inserted you could use the format codes in the CONVERT function.
UPDATE t SET
daterequested = STUFF( STUFF( StringDate, 5, 0, SUBSTRING(StringDate,7,2)), 9, 2, '')
FROM YourTable t
CROSS APPLY( SELECT CONVERT( varchar(25), '20190905 19:09:34.823', 121) AS StringDate) AS x;

Error Converting NVARCHAR to DATETIME

I create a temp table
CREATE TABLE #Test(theDate DATETIME)
I use this query to insert data into the temp table
INSERT INTO #Test VALUES(CONVERT(NVARCHAR, '2016-09-30' 21))
It works on one database (SQL Server 2005), but gives error on another (SQL Server 2008). I can't remember the exact error, but it has something to do with 'Error converting NVARCHAR TO DATETIME'
Why is working on one database, but not another? Is there a special property to enforce error on mismatched type? I can't find information anywhere.
There are several problems:
You use CONVERT to convert a string to a string while you want a DATE
You use NVARCHAR without a length (which is 1 by default) Bad habit to kick
There is a comma missing after the date literal
You are using 21 which is the format with a 2-digit-year Details here
Better was this
SELECT CONVERT(DATETIME, '2016-09-30', 121)
But even better was to avoid conversions at all.
In your case use Details here
unseparated: INSERT INTO #Test VALUES('20160930')
ODBC (my favorite): INSERT INTO #Test VALUES({d'2016-09-30'})
UPDATE
I cannot check this, no such versions installed, but I'm quite sure, that different default culture / language and implicit error correction leads to this behaviour...
On my SQL Server 2012 instance all of them work fine. The obvious format error (21 instead of 121) is corrected implicitly. The second obvious error (conversion to a 1-char-string) is corrected as well. Lower versions deal with this differently (probably).
If possible, try these lines on your servers...
SET LANGUAGE US_ENGLISH; --MDY
SELECT CONVERT(NVARCHAR, '2016-09-30', 21)
SET LANGUAGE GERMAN; --DMY
SELECT CONVERT(NVARCHAR, '2016-09-30', 21)
SET LANGUAGE JAPANESE; --YMD
SELECT CONVERT(NVARCHAR, '2016-09-30', 21)
The literal date format YYYY-MM-DD, even as this is short ISO8601, is not sure in all situations. That's why one should use culture independant formats always...
You can try this if it is a problem related to language
declare #language as varchar(100)
SELECT #language= ##LANGUAGE
SET LANGUAGE us_english
--... your code...
SET LANGUAGE #language

SQL Server Converting varchar to datetime

I got a problem in SQL Server with converting a varchar to datetime. I would like to convert/update whole column [datelog] in table:
[dd.mm.yyyy hh:mm:ss]` to `[yyyy-mm-dd hh:mm:ss]
In SQL Server 2012+ you can use PARSE or TRY_PARSE to parse a text value according to a specific culture.
Assuming your text follows the German culture ('de-DE') you can parse it to datetime with :
select PARSE('24.11.2015 13:10:55' as datetime using 'de-DE')
eg:
select PARSE(datelog as datetime using 'de-DE')
The real solution though would be to use the correct field type, ie datetime. It's almost guaranteed that someone, somewhere will either enter text with the wrong format or try to convert the text using the wrong culture.
Date types on the other hand, have no format, they are simply binary values. Using them is faster, safer and easier.
Tricky solution,
DECLARE #inputDate AS VARCHAR(20)='21.11.2015 06:59:00' -- [dd.mm.yyyy hh:mm:ss]
SET #inputDate = REPLACE(#inputDate ,'.' ,'/')
SELECT CONVERT(VARCHAR(24) ,CONVERT(DATETIME ,#inputDate ,103) ,121) OutputDate -- [yyyy-mm-dd hh:mm:ss]
Still you need to change as per your table columns.
temp table with one column of type varchar
create table #temp3 (someDate varchar (30))
insert into #temp3 values ('23.03.1989 15:23:43')
using a combination of concat, substring and right
select concat
(
SUBSTRING(someDate,7,4),'-', SUBSTRING(someDate,4,2),'-',SUBSTRING(someDate,1,2), ' ', right(someDate, 8)
)
from #temp3
gives: 1989-03-23 15:23:43

converting a varchar to datetime in sql server

I have a varchar 20130909132512 and I would like to convert it into just a date 2013-09-09
I keep getting an error when trying
select convert(datetime,'20130909132512',105)
and I am trying to avoid using
select convert(datetime,SUBSTRING('20130909132512',0,8),105)
if possible. Any ideas on how I can do this?
You could transform your varchar to 20130909 13:25:12 using STUFF
Declare #a Varchar(20)
Select #a = '20130909132512'
select convert(datetime,
STUFF(STUFF(STUFF(#a,13,0,':'),11,0,':'),9,0,' ')
,105)
Whether you use bummi's STUFF method or continue using SUBSTRING, you're going to have to pre-format the string no matter what you do.
In this case, SUBSTRING will perform a bit faster
SELECT CONVERT(date, SUBSTRING('20130909132512', 0, 9), 20)
You'll want to use "20" as the conversion style, though, if you want the date in the format 2013-09-09.

Resources