SQL Server (T-SQL) datetime conversion - sql-server-2008

A silly question maybe but I wanted clarification. I've created a script that has a date parameter like so:
DECLARE #dateparam as datetime
SET #dateparam = '01-01-2013 00:00:00'
This looks like it is working when I test it even if the date string is not in "correct" format yyyy-MM-dd hh:mm:ss. I changed my computer regional settings to English and the script still did what it was supposed to do.
Is this because of SQL Server 2008 R2 that I have in my computer that it knows how to convert the date or can I ran into trouble with using a dateformat like I have used?

Converting 01-01-2013 won't expose issues such as which 01 is the month, and which is the day.
It's not a safe format.
The safe formats (for converting to datetime, rather than to datetime2) are:
YYYYMMDD 20121201
YYYY-MM-DD'T'hh:mm:ss 2012-12-01T10:43:29
YYYY-MM-DD'T'hh:mm:ss.mil 2012-12-01T10:43:29.337
Stick to those and only those. (The examples all represent the 1st December 2012)
Or, better yet, don't treat dates as strings at all, if you can avoid it. If you're, for example, calling SQL Server from .NET code, keep that dates as DateTimes in your code, and let ADO.NET and SQL Server deal with any required translations to make them become datetimes - without translating them to and from strings.

You're making an implicit conversion from something that looks like a date, but inf fact is a string ( '01-01-2013 00:00:00'). Rather than trusting on SQL Server to make the correct guess in what format the string is in, you should make the conversion explicit by specifying the format.
This can be done by using CONVERT (not CAST) and specify a 'style'. The different styles are listed here: http://msdn.microsoft.com/en-us/library/ms187928.aspx.

Related

Eliminate or translate HTML character in SQL import from XML file

I came across an issue when running a procedure that shreds an XML file and imports the data into SQL server.
It has been running successfully for a few months, but today I got an error:
Conversion failed when converting date and/or time from character
string.
This is the line where it fails
SELECT
ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))) AS StartDate
,ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))) AS EndDate
FROM #xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X)
When I looked at the XML file, I noticed that some of the dates were written like this:
01/12/2016&#160
This character &#160 is a Non-breaking space.
I would like to know if there is any way in SQL Server to account for these types of issues? For this specific problem, I can use REPLACE:
SELECT
REPLACE(ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))),'&#160','') AS StartDate
,ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))) AS EndDate
FROM #xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X)
but if other XML/HTML characters come up, is there a way to universally check for/deal with them?
I'd imagine you could create an auxiliary table of strings that you wish to replace/remove and join that to your query. This would be preferred over hard-coding each character, and would allow you to expand on the functionality easily. The caveat is that I'd expect it to slow down you query based on the number of characters you need to replace/remove.
SELECT
REPLACE(ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))),
StringsToRemove.string,'') AS StartDate,
REPLACE(ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))),
StringsToRemove.string, '') AS EndDate
FROM
#xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X),
StringsToRemove

MySQL DATE type value gets displayed with hours/minits/seconds, however its is not stored like that in the database

I have DATE type column in a MySQL database table with dates stored in it. I am using the YYYY-MM-DD format when I upload the dates to the database.
When I check the values they are stored in the correct and expected format (2005-01-02), however when I am attempting to read these values with my C++ program I am receiving something like this: 02/01/2005 00:00:00
I don't really understand the reason, I thought in the DATE type only the YYYYMMDD gets stored without the time. The other weird thing is the reformatting I thought YYYY-MM-DD is an accepted format.
Here is my code:
String^ getDateOfBirth = myReader->GetString("player_date_of_birth");
DateOfBirthLabel->Text = getDateOfBirth;
I am using Visual Studio 2013.
Any help would be appreciated.

Converting a non-standard datetime field in SQL server

I'm using SQL Server 2008 R2. I've been given data that includes numerous varchar fields in the format mm/dd/yyyy hh:mm:ss.mmmmmm AM. Here are some examples:
1/16/2013 10:31:38.000000 AM
11/12/2013 3:42:12.000000 PM
12/6/2013 2:42:46.000000 PM
I'd like to convert into a datetime format so I can work with them, but am having problems... I've found various sites that list the different date time formats (such as this one), but this format isn't listed anywhere.
I've tried casting and converting in various different ways, including:
select CAST (field as datetime) from table;
select CONVERT (datetime, field, 101)
from table;
But I keep getting this error:
"Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string."
I presume because the format my field is in isn't standard so SQL can't recognize it?
Any help would be appreciated. Apologies if this has been covered before - I've spent 4 hours searching this and other sites but can't find the answer...
Shouldn't you be casting to Datetime2 type instead? The Datetime has precision only as high as to milliseconds.

How to make the default date format dd/mm/yyyy in SQL Server 2008?

I have an Excel file that contains a column full of dates in the dd/mm/yyyy format. When I try to import it using openrowset, it said that there was a datatype mismatch. I have a table where the date is defined as type date. Now, I know that the default date format in SQL Server is yyyy-mm-dd. How can I avoid this conflict? Is there a way I can make the default date type be dd/mm/yyyy? I need to do this import operation everyday and it has to be automated and so I cannot afford it to fail in between. I tried using sp_addlanguage to make it British as the default date type is dd/mm/yyyy there, but it didn't work :(. I'm using SQL Server 2008 and Windows 7, if that is of any help. Please help me out! Thanks!
You could CONVERT the incoming data before you insert it. So, in the openrowset statement, where you select the field, you could surround it with a CONVERT statement. Here's an example:
print convert(date,'19/07/2010',103)
This is a UK style date, but if you run it you can see that it's converted it to SQL-friendly format.

SQL Server 2008 VarChar To DateTime

I have a table where unfortunately a number of dates are stored as strings.
I have a number of reports that cast them to datetimes and filter by them. This was working fine until today when all of a sudden i'm getting this error
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
The dates are all stored in the format of "yyyy-mm-dd" and are all valid.
If I run the following SQL statement
SELECT CAST('2010-06-02' AS DateTime)
I would expect to get "2010-06-02" however as of today I'm getting "2010-02-06" something has changed with the way SQL formats dates. I've had a look in regional settings on the server and it all looks to be correct.
What else could be causing this?
Try setting the format explicitly
select convert(datetime, '2010-06-02',101)
An unambiguous way of getting this conversion is to do the following:
SELECT CAST(replace('2010-06-02', '-', '') AS DateTime)
And that will always be interpreted as YYYYMMDD, ignoring the set dateformat ydm declaration or any cultural settings that the database has.
Q1: What else could be causing this?
The local, You probably are under the local101(US) and put data from 103 (British/French)
Like barry sad use convert