SQL REPLACE giving error: 'String or binary data would be truncated.' - sql-server

I'm trying to replace just part of a string in our company database. The column in which I'm trying to update is MERGECODES (varchar(20),null). A typical value of this column would be something like 'M, GPE, T'.
I would like to replace every instance of T with KD but I'm getting the error below. It will allow me to change anything with the same number of characters or less, for example, it will allow me to replace T with K but not with KD. Any help would be greatly appreciated. Thanks guys!
Code:
UPDATE GoldMine.dbo.CONTACT1
SET MERGECODES = REPLACE(MERGECODES, 'T', 'KD')
ERROR:
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.

You need to increase your data type size.
Currently you have varchar(20).
If the data is 20 characters long, and you replace 1 character for 2, then that will be 21 characters long, which will cause truncation.
Try increasing your data type to varchar(50) for example, and this should resolve your problem.

Related

How to fix "conversion failed when converting the varchar value 'is turned' to data type int"

I did some changes in my IMOS database and got it working but when my customer tried the same they ended up with an error. When searching from google I think the line needs some CONVERT(...) included but I don't know how to do this. Can somebody help me? The code and error is below.
update CAMDLATTROUTPUT
set OUTPUTFLAG = 1
where ATTRTYPE = '10400'
and ATTRTYPE = 'is turned'
Error:
Conversion failed when converting the varchar value 'is turned' to data type int.
It seems the ATTRTYPE column is defined as int, therefore the first part of your statement should be
UPDATE CAMDLATTROUTPUT
SET OUTPUTFLAG = 1
WHERE ATTRTYPE = 10400
(without the single quotes).
What leaves me confused is the line
AND ATTRTYPE = 'is turned'
which can't be an integer of any kind. Therefore I suspect that this could be the foreign key to another table that contains a record with an ID and the text "is turned". In this case you would need a join.
On the other hand: A single column of a record can never ever be as well 10400 AND 'is turned' at the same time. Therefore, even if ATTRTYPE would be a string and you would not get a syntax error, the number of rows affected by that query will always be 0.
Got it working, changed line -
update CAMDLATTROUTPUT set OUTPUTFLAG=1 where ATTRTYPE='10400' and ATTRIBUTE = 'is turned'
into
update [imos].[dbo].[CAMDLATTROUTPUT] set OUTPUTFLAG=1 where ATTRTYPE='10400' and ATTRIBUTE = 'is turned'
Don't know why first one is not working for everyone but I hope that atleast someone will find a solution for his problem from here :)

Msg 8152, Level 16, State 10 on Like where clause

Might get some negative points here but feel the need to post an problem (And self-defined solution) that I could not match to anything on the interweb...
I have a very simple bit of code that returns the queries that my company's SSRS RDL datasets uses to obtain data.
It works nicely when returning all data, but the moment I add a where clause to only return records with specific text within the data set like the parameter I get an error:
Msg 8152, Level 16, State 10 ...
String or binary data would be truncated
The parameter is defined exactly the same as the column I am searching in is (NVARCHAR(MAX)).
I know what that error means ("The error means that you're trying to put a value into a table that is larger than what the column can hold. I.e. you're to insert a varchar(100) into a column that's been defined as varchar(80).") but what stumped me was that the flippen thing only errors out when run the query with the where clause.
I managed to narrow the culprit record down to one having 12351 characters (Using LEN).
The solution that I found was to change the LIKE to an =. Not 100% ideal because I would like the flexibility to search for any text within the column, but I can live with it.
If you are using LIKE operator in following manner:
WHERE myColumn LIKE '%some text%`
which I suspect you do from your description:
to search for any text within the column
Then, you can replace it with WHERE CHARINDEX('some text', myColumn) > 0 and see if it will work.

Convert varchar dd-mm-yyyy to yyyy-mm-dd date field in SQL Server 2008

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.

Error converting Varchar to Decimal SQL Server

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,',','.'))

Loading numeric values incorrectly stored as text in to SQL

I have a .csv file that I have received from a third party - so am unable to sort the issue out at source. The .csv file is separated as follows:
"1","client_name","0","0"
"2","client_name","0","0"
I have successfully loaded the file into a staging table with all columns stored as varchar(255). I am unable to convert column 4 to a decimal(10,0). Which is the datatype of the live destination table.
Strangely enough column 3 and 4 contain only zeros, column 3 can be converted to decimal(10,0) but column 4 cannot.
ISNUMERIC(REPLACE(credit_terms, '"', ''))
Returns 0 for all rows in column 4, though 1 for all rows in column 3.
CAST(REPLACE(credit_terms, '"', '') AS DECIMAL(10,0))
Returns the error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
It is strange that what looks on the surface as an identical column (3) will covert fine, but 4 will not.
I fully understand that storing numbers as text is poor practice, but am unable to resolve this as the data originates from elsewhere.
Following suggestions I have now opened the file in a hex editor. At the end of each line is a line break that looks like this: .. (Od & Oa).
In the hex editor each row looks like this:
"1","client_name","0","0",..
"2","client_name","0","0",..
Ensure that there isn't a hidden space " " character after the last 0; this can happen a lot in excel/csv files. That would cause it to fail the conversion to numeric.
Also, make sure your index is correct. Many things start at a 0 index so are you possibly trying to convert column[4], which relates to the 5th logical column?
I HIGHLY recommend downloading a free trial of UltraEdit, then opening it, open one of the above files, hit Ctrl-H to go into Hex mode which allows you to view the Hex / ASCII code of every character in a file (even the not visible ones), and eyeball whethere there is any characters that can't be seen that would cause the above conversion error.
I've seen this all the time with files generated by mainframes that add characters that hose up a subsequent import.
Also, since you have loaded it in SQL, do a quick SELECT LEN(Column4) to see if it returns anything other than 1.
Even better, do a SELECT Column4 FROM YourTable WHERE ISNUMERIC(Column4) = 0 to return a set with the offending values.
Try using TRY_CAST to find values that won't convert;
select * from MyTable
where TRY_CAST(REPLACE(credit_terms, '"', '') AS DECIMAL(10,0)) is null
After advice from this question I found that the line break for the data was OA and OD.
0A is a line feed
0D is a carriage return
I was then able to replace char(10) - line feed and char(13) - carriage return as follows:
CAST(REPLACE(REPLACE(REPLACE(credit_terms,char(10),''),CHAR(13),''),'"','') AS DECIMAL(10,0))
The column will now convert to a decimal as required. Thank you for all your pointers.

Resources