SQL Server 2008 VarChar To DateTime - sql-server-2008

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

Related

How can I convert a varchar into a date in MySQL Workbench?

Probably it's super simple but i've been stuck some hours on this.
I have a column called "Publish_Date" which is a varchar, but my date shows like this: 17.01.11 (year.day.month) and I want to convert it to a date (at this point, any date format it's ok).
Every time i tried to use "convert" or "cast" it gives me a syntax error or the data doesn't change or all the data in the column changes to "null" values.
I'd appreciate if you can help me.
Assuming your data is all greater than 2000 then you can add missing part of YEAR then cast it.
SELECT CAST(CONCAT('20', Publish_Date) AS DATETIME);
You can use STR_TO_DATE with the format %y.%m.%d since this is how your date value is stored
select
str_to_date(birth_date, '%y.%m.%d')
from
mytable
Here is an SQL Fiddle I created for this case

Update-Set for a Date type not processing query part correctly in MS Access 2007

I'm trying to extract a text date into a date type field and change the format from yyyymmdd to ddmmyyyy in the process. I have set up a simple select statement checking that the dates are valid and if not setting a default date and this worked fine, no bad dates.
SELECT
IIf(isdate(Format(Left([EffectiveDate],10),"dd/mm/yyyy")),Format(Left([EffectiveDate],10),"dd/mm/yyyy"),#01/01/1900#) AS Expr1
FROM Relationships;
But when I embed this exact same select statement in an Update Set Query:
UPDATE Relationships
SET MSDate = IIf(isdate(Format(Left([EffectiveDate],10),"dd/mm/yyyy")),Format(Left([EffectiveDate],10),"dd/mm/yyyy"),#01/01/1900#);
the dates are formed as mmddyyyy and not ddmmyyyy as the select query does.
Interestingly, when I tried to change the format type to "long date"
UPDATE Relationships
SET MSDate = IIf(isdate(Format(Left([EffectiveDate],10),"long date")),Format(Left([EffectiveDate],10),"dd/mm/yyyy"),#01/01/1900#);
I got the default 01/01/1900 result suggesting what was extracted was not a valid date. By the way, just using the query in a Select statement worked just fine.
I can't help thinking that something is happening in the conversion to date type. I even tried to do DateValue on the query but still no joy.
Since you're using Left([EffectiveDate],10), I assume that the text field actually contains yyyy-mm-dd (the ISO format).
You should leave the string in this format (Access understands ISO and US format mm/dd/yyyy best), and convert it with the CDate() function.
UPDATE Relationships
SET MSDate = IIf(IsDate(Left([EffectiveDate],10)),
CDate(Left([EffectiveDate],10)),
#1900-01-01#);

Error when using DateAdd - TSQL

I have an app that uses a table that has a varchar column [BadColumn] which is populated by a date in the format MM/DD/YYYY or at least that's what most of the rows contain.
I have no control on modifying this table and changing the data type.
I need to report on this data to show rows that are within a week.
Here's my problem:
every time I use something to compare/filter the date I get an error
Conversion failed when converting date and/or time from character string
So here's what I have tried so far :
Tried to create a view with the [BadColumn] being converted to a Date type column [NewBadColumn] and convert the value from the original table to a date type using Convert(date, BadColumn). The view gets populated, but now when I still try to query using DateADD or do any date comparisons I get the same error.
I have also tried to use Convert(Date, BadColumn, 105), but same problem.
I have tried other formats, but still the same issue cannot do a date comparison on that column.
I am not sure which row is throwing the error, how do I find it and fix this issue.
Thank you for the responses. I have tried using ISDate function to find the bad records.
But I am still getting the same error "Conversion failed when converting date and/or time from character string." when I try to use DateAdd.
Here's the code I am using :
Select t.*
from
(Select * from dbo.BadTableName q with (nolock)
where ISDate(BadColumn)=1 and ISDATE(BadColumn) Is Not Null
) t
where t.BadColumn > DATEADD(dd,-2, GetDATE())
Any help is appreciated.
Try checking the column using the ISDATE() function. You can do this without creating a view first.
IF ISDATE(YourColumn) = 1
DATEADD (datepart , number , YourColumn )
ELSE 'No valid date provided'
Look at this other stack over flow question and answer
Find invalid dates in SQL Server 2008
You can use the ISDATE function to test the individual rows.

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date

SQL Server (T-SQL) datetime conversion

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.