I've the following problem:
I need to calculate a date using dateadd function.
dateadd(d,delay),CONVERT(DATE,Started,105))
delay is taken from a different table than started, and its in numeric format
the problem is I receive this type of error:
Messaggio 241, livello 16, stato 1, riga 14
Conversion failed when converting date and/or time from character string.
I then tried to do something like:
dateadd(d,CAST(delay AS NUMERIC(8)),CONVERT(DATE,Started,105))
resulting in the same error :(
Anyone can please help me?
The error message is very clear. There are values in the Started column that cannot be converted to date using the style you provided. Such is the path your table designer chose when choosing to store dates as strings. Now you need to either clean up the data (better yet - clean and convert the column to the correct datatype) or you write your queries to avoid the bad rows. So how do you do that?
You could (and should) get into the habit of searching the internet for answers. This question is posted daily somewhere. Assuming you have a current version of sql server, you can use the following to find the "bad" rows.
select * from dbo.mytable as tbl where try_convert(date, tbl.Started, 105) is null
order by tbl.Started;
And you can adapt that logic to avoid bad rows very easily.
Related
I am using MSSQL and I get the aforementioned error message when I try to execute the following query.
Select *
from fa_disp
where dispdt between '2014-10-01' and '2015-09-31';
I know there is a simple answer. When I look at all of the other questions and answers for this error msg they are so complex and involved. I am new to this so when I tried the examples it still didn't work. Help please?
Try using a Convert/Cast in SQL. Something like:
WHERE CONVERT(datetime, dispdt) between '2014-10-01' and '2015-09-31. Those will be the functions I'd start with.
Also, examine the data your comparing in the query. The field dispdt might not contain the data you think it does or be a malformed date.
You should use language-independent format yyyymmdd when you pass datetime literal constants like this:
Select *
from fa_disp
where dispdt between '20141001' and '20150931';
hopefully the title describes what I'm trying to do.
I have a varchar field in a SQL Server 2008 table that contains text dates in the format dd-mm-yyyy (e.g., 31-12-2009). I am trying to use CONVERT to convert it to a DATE field. I was successful in converting a similar varchar field in the same table using the following:
SELECT DISTINCT(CONVERT(DATE, MYDATEFIELD1, 103)) AS [CONV_MYDATEFIELD1] FROM MYTABLE;
But when I apply the same to MYDATEFIELD2, which appears to have the same type of data values as MYDATEFIELD1, it fails with the following error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I've tried sorting and using LIKE to try to find any characters that might prevent the conversion but I haven't been able to pinpoint anything.
Any help will be greatly appreciated. Thanks!
You may have some invalid dates (e.g. 30-02-2009), try to find them splitting the characters and validating the day and the months, assuring that the days correspond to the month and the month is in the range 01 - 12.
If you can't find which value is causing the conversion error then use a cursor to go through all the records individually and use TRY CATCH to find which record(s) cause the conversion error. You could use a PRINT statement in the CATCH block to identify the records that are erroring.
Find your bad dates with the following:
SET DATEFORMAT dmy;
select MYDATEFIELD1, isdate(MYDATEFIELD1)
from MYDATEFIELD1
I figured out the issue that was causing the CONVERT to fail but I'm not sure of the best way to select an answer (veritable stack noob) so, any help on that would be appreciated. Here are the major steps I took to find the issue:
I used MIN and MAX SUBSTRING to identify that the component parts of the
varchar field were correct (i.e., the 1st two digits min=01 max=31,
middle two min=01 max=12)
I used DISTINCT SUBSTRING to identify that all of the date separators were consistent (i.e., all dashes).
I used MAX(LEN) to determine that my varchar "date" field was 12 characters (vs. the 10 characters I was expecting).
I used CONVERT(VARBINARY, MYDATEFIELD2) to determine what was actually stored in the string.
The last step revealed that the field contained line feeds (00A). I opened the source text file in notepad++, clicked View -> Show Symbol -> Show All Characters and I could see the LF at the end of each line.
So now I'm modifying the DTSX package (fixed width text) to include an extra field for the linefeed that I can drop afterwards. Now that I know what the intended format of the date fields is, I'll try to import them as DT_DATE vs DT_STR. I'm not exactly sure how to specify the correct date style 105 at import (thanks #Panagiotis Kanavos) but I'll figure it out.
Whew! What a learning experience! :D
Thanks to everyone who helped - and if you can give advice on the best way to select the best answer it will be greatly appreciated.
I have a staging table loaded with data from a SAS dataset containing 5M records. All the columns are varchar. I am trying to convert a couple of columns to decimal(32,10). But it generates an error. I tried cast, I tried convert and even splitting the data up before and after decimal - same result.
I looked at the IsNumeric flag of the column and there are 0 records <> 1 meaning the data is numeric.
case
when wtd_count = '.' THEN NULL
when wtd_count = '' THEN NULL
else convert(decimal(32, 10), wtd_count)
end
Error:
Msg 8114, Level 16, State 5, Line 99
Error converting data type varchar to numeric.
So I'm wondering what else I can do to convert the data to decimal? Any idea?
Any help will greatly be appreciated.
If you are in SQL Server 2012 and above try to use try_parse or try_convert
ISNUMERIC is not reliable for what you're doing. It will flag values with things like monetary symbols and commas in them as valid.
It seems quite likely that there is some non-numeric data present. TRY_CONVERT or TRY_PARSE are your friend here. As an FYI, SQL Server version 11.0.x is SQL Server 2012, so you should be able to use these.
I also find it hard to believe that converting to numeric works, but not decimal. I can find no information that suggests the actual implementation of these two data types is different, and as such neither should work.
I would do some more in depth analysis of your data to make sure it looks like you're expecting it to.
After read your case statement i suppose that you have coma separated values. I'm pretty sure that you should use: CONVERT(DECIMAL(32,10),REPLACE(wtd_count,',','.'))
I am new to SQL Server so please accept my apologies if my question seems too easy. I tried finding a solution, but so far I couldn't see anything that I could use in my query.
I am trying to find length of the biggest columns in a table and I'm using the following query.
SELECT
SUM(DATALENGTH([INSDetails])) as [INSDetails]
FROM
dbo.Contractors
The table Contractors is slightly over 8GB and has over 30mln rows. INSDetails column is varchar(2048)
The query above works perfectly well for all the other columns of all the other tables in my database, but when I run it on Contractors table it returns an error
Msg 8115, Level 16, State 2, Line 26
Arithmetic overflow error converting expression to data type int.
I understand that this error message appears when you try to convert a value of a certain datatype to another datatype, but that value is too large for the second datatype.
Could you please help me to rewrite the query or suggest alternative approach to get the output?
I read that someone suggested using CAST AS big int to solve this issue, but I'm not sure how it can be incorporated in my query.
Any suggestions will be appreciated. Thank you in advance.
select sum(cast(datalength([INSDetails]) as bigint))
Hi Having a syntax issue - at least I think it is. I want a default date as part of a case statement inside a materialised view (MS SQL 2008 +):
, CASE
WHEN WithFirstDate = 0 THEN CONVERT(DATE,'1900-JAN-1', 101)
WHEN WithFirstDate = 1 THEN
Start1
ELSE --WithFirstDate = 2
Start2
END ValidDate
I'm getting the following error:
view uses an implicit conversion from string to datetime or smalldatetime. Use an explicit CONVERT with a deterministic style value
I'd like to have a solution that works irrespective of localization (i.e US style dates, Japanese style dates and the rest of the world)
Thanks
Instead of:
CONVERT(DATE,'1900-JAN-1', 101)
Just do:
CONVERT(DATE,'1900-01-01')
However the issue may be with the other two columns, Start1 and Start2. I am guessing these are not DATE columns.
The 101 code you are passing the CONVERT function does not match your format. Check the following link to find the correct code:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Ok well this forum has gone downhill IMHO, first my post get endless edited for grammar which doesn't change the meaning, then it gets voted down presumably because it was "to difficult" to answer. https://stackoverflow.com/users/61305/aaron-bertrand was on the right lines. Thanks Aaron. The problem was a computed column in one of the referenced tables was non deterministic. This error only resolved in a flag when the materialising clustered index was being created on the view. I'd post a link to the full answer but not allowed. Shame I cant recover all my old badges and points from a couple of years ago. Full answer here http://tinyurl.com/knor8qk