I have a field type varchar in sql server. It contains data like "010109" etc.
When I try to convert this to DATETIME it returns a value "9 Jan 2001" when the actual value should be "1 Jan 2009".
Does anybody have a fix for this?
Thanks for your help.
I thought there would be some conversion format so you could put:
select(convert(datetime,'010109',<some magic number>))
and get the result, but I can't seem to find one that gives the right date :(
This works, but is pretty nasty:
declare #dt varchar(6)
select #dt = '010109'
select convert(datetime,RIGHT(#dt,2) + SUBSTRING(#dt,3,2) + LEFT(#dt,2))
Yeesh
When you type date in the format of 'xxxxxx' it seems that SQLServer assumess it is an ISO format yymmdd and as such it is not affected by the SET DATEFORMAT
I was aware of 2 such formats - so called safe formats
ISO: yyyymmdd
ISO8601:yyyy-mm-ddThh:mi:ss.mmm
but it seems that yymmdd is also ISO - check BOL Date and Time Styles - format 12
That would explain why the solution posted by Scorpio did not work
You can use the solution provided by butterchicken with the format specification (12) to be on a safe side:
declare #dt varchar(6)
select #dt = '010109'
select convert(datetime,RIGHT(#dt,2) + SUBSTRING(#dt,3,2) + LEFT(#dt,2),12)
If possible I would be ideal if you could change the column to datetime to avoids similar surprises in the future
SQL Server is expecting the date to be in the format YMD
So if your string was like this '090101' you would get the proper date.
select(convert(datetime,'090101',104))
So you will have to substring out the parts and run the convert.
Related
I have a MySQL-Database with several rows. In that environment, I stored the current time as a Timestamp (int).
Now I need to migrate my data from a MySQL to a T-SQL-Database. I'm running SQL-Server 2008.
I checked several approaches, but couldn't come up with a way which transforms my int into a smalldatetime format.
Is there a build-in function for this? Is this even doable alone in a statement? I really don't want to write a PHP-snippet, which converts the timestamp to the desired format.
Thanks in advance
As per the documentation, smalldatetime uses the following format:
DECLARE #smalldatetime smalldatetime = '1955-12-13 12:43:10';
So, we need to convert the MySQL timestamp into date and format it in the above format to get smalldatetime. This can be done by using FROM_UNIXTIME and DATE_FORMAT functions of MySQL, e.g.:
DATE_FORMAT(FROM_UNIXTIME(timestamp_column), '%e %b %Y') AS 'smalldatetime';
Here's the SQL Fiddle.
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#);
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.
I got in my table two types:
hour_Event in type date
date_Event in type datetime
I would like hour_Event to be formatted to hh:mm
and date_Event to be dd/mm/yyyy
I use PHPMyAdmin and im new with MySQL so I dont know how to change the format and for what.
How do I do it?
**DATE_FORMAT(hour_Event,'%h:%i')** will give you hour_Event in hh:mm
and
**DATE_FORMAT(date_Event,'%d/%m/%Y')** will give you date_Event in dd/mm/yyyy
You could change the format using php before you insert data or after retrieving themfrom your database according to your needs.You could use DateTime / strtotime() & date(). In most cases a single timestamp field in your table is enough for an event.
Have a look here:
Convert one date format into another in PHP
Also you could format your field according to your needs with mysql for example:
DATE_FORMAT(tables.field, '%d-%m-%Y %H:%i:%s') AS 'new_name'
Have a look about it here:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format
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