SQL Server Truncation Error when column length should be sufficient - sql

I have a table in SQL Server 2008
Table Name : tbl_device
Table Structure:
Column | Type
col1 | nvarchar(200)
Now when i try to insert data into this (it works for shorter cases but) and the string data is long i.e. with
LEN function it is 162
Still the server gives error :
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
what should be the reason for it ??

There are trailing blanks in the string that generates the error message but they are not counted using len() function.

Related

Format Float to N0 and convert to String

I have 3 columns, two are bit values, and one is a float. I want to combine these values into one column using the following case statement, I need the final column to be a string, so I can have text values representing the Bit values. This works fine, however, I also want to format the float as 'N0' before converting to a string. So a large number is shown as 1,234,567,890.
CASE
WHEN nd = 1 THEN 'ND'
WHEN nil = 1 THEN 'Nil'
ELSE LTRIM(STR(FORMAT(position, 'N0'))
END AS position
Msg 8114, Level 16, State 5, Procedure ChkSummary, Line 46 [Batch Start Line 152]
Error converting data type nvarchar to float.
EDIT:
DBMS SQL Server
As comments suggest ELSE FORMAT(position, 'N0') works.

SQL conversion failed when converting the varchar

OK just joined stackoverflow today and I am very new to scripting, specially SQL
I am trying to run below to get a invoice num
SELECT
*
FROM
[Database].[dbo].[Table]
WHERE
[EPSINO] = 915561
AND [EPYEA4] = 2017
AND [EPSPYN] = EPSPYN
AND [EPCONO] = 100
AND [EPDIVI] = 400
GO
But I'm getting an error
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'R8463 ' to data type int.
Understand I need a convert line in there somewhere but not sure where
PLEASE HELP
Thanks
Look at the datatypes defined on your columns. nvarchar columns need to be wrapped in single quotes. For example, EPSPYN is most likely an nvarchar column. I can't tell if the remaining columns are int or nvarchar. You need to look at your table definition and wrap in quotes appropriately.
select *
from[Database].[dbo].[Table]
where [EPSINO] = 915561
and [EPYEA4] = 2017
and [EPSPYN] = 'EPSPYN'
and [EPCONO] = 100
and [EPDIVI] = 400
GO
When you have a column of type char, you have to compare its value within single quotes. So if column EPSPYN was of type char, ensure that the value against it is written within single quote.
Select * From[Database].[dbo].[Table]
WHERE
[EPSINO] = 915561
and [EPYEA4] = 2017
and [EPSPYN] = 'EPSPYN' -- See this.
and [EPCONO] = 100
and [EPDIVI] = 400
GO
First find out the datatypes of that table, then build the WHERE clause. like VARCHAR, NVARCHAR and DATE Type enclose the Search text with Single Quotes.
Use Below command for the list of columns and its datatypes and build your Query.
SP_HELP [Database].[dbo].[Table]

Conversion failed when converting from a character string to uniqueidentifier in TSQL

I am having an issue with my T-SQL query here:
UPDATE a
SET a.application =
SUBSTRING(b.postdata,
NULLIF(
PATINDEX('%[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-%',b.postdata),0),36)
FROM [Lionel1].[dbo].[CSL_Logging_STAGE] a
INNER JOIN [Lionel1].[dbo].[CSL_Logging_STAGE] b ON b.Id = a.Id
WHERE
a.application LIKE '%000%'
AND a.postdata != ''
AND a.postdata IS NOT NULL
AND a.http_method = 'post';
I have tried a handful of different castings for the GUID that is stored in the application column and I have also restructured the query to have a subquery but that makes no difference.
The error is
Msg 8169, Level 16, State 2, Line 3
Conversion failed when converting from a character string to uniqueidentifier.
Anyone point me in the right direction?
Some of the VALUES I am trying to set to application are
NULL
0dab3646-a4ed-4b63-9441-11e27fa36df8
f6a5c360-635a-48e0-bf71-76ef0b865441
It looks like I needed to do a better job matching the appkey. It was grabbing extra data on a few of the compares adding quotes and extra characters which are not valid in a GUID.

full-text search in Microsoft SQL Server

I have a table (name: errors) with lots of rows. some of rows of this table shows in following:
202 16 Invalid type '%s' for WAITFOR. Supported data types are CHAR/VARCHAR, NCHAR/NVARCHAR, and DATETIME. WAITFOR DELAY supports the INT and SMALLINT data types.
203 16 The name '%.*ls' is not a valid identifier.
204 20 Normalization error in node %ls.
205 16 All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
206 16 Operand type clash: %ls is incompatible with %ls
207 16 Invalid column name '%.*ls'.
instead of strings like '%.*ls', different words can be used. for example i have a string like this:
Invalid column name 'test'.
I want to use full-text search in Microsoft SQL Server 2012 to find most similar string in errors table with (Invalid column name 'test'.) i used below query but this query does not return any output
SELECT *
FROM errors AS A
INNER JOIN
CONTAINSTABLE(errors , text, 'ISABOUT ("Invalid column name ''text''")',
LANGUAGE N'English',10) AS K
ON A.text = K.[KEY]
any idea?

Bulk Insert Multiple Pipe Data in Table

I wrote a query to insert data from .lst file into table name AxisATM But when i try to insert it gives me error that
WHEN I REMOVES ALL ROWS FROM LST file except First ..It gives me Success msg saying 1 rows changed..
Msg 4866, Level 16, State 1, Line 1
The bulk load failed. The column is too long in the data file for row 1, column 36. Verify that the field terminator and row terminator are specified correctly.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Query :-
BULK
INSERT dbo.ATMAxis
FROM 'c:\AGS_WINCORE_120901.lst'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = 'CHAR(13)'
)
GO
AGS_WINCORE_120901.lst :-
PRO1|......|00000000000|0| {Like it has 36 '|' separators}
Using Notepad++ i know Line End Characters are CR LF
As expected the problem lies in
ROWTERMINATOR = 'CHAR(13)'
I have Changed it to
ROWTERMINATOR = '0x0A'
As the error says column 36 has more chanracters than that of column 36 of table's column. You need to increase the size of that column
It seems the row terminator creates issue. If that's the case, try replacing /r/n with Char(10) it will solve the problem. Also, check for char(13).
This MSDN thread has more information.