Casting varchar to bigint in SQL Server 2016 - sql-server

I am trying to concat two integers together and then cast it as bigint, but SQL Server 2016 is throwing an error:
Error converting data type varchar to bigint
My table has two columns idnum int not null and innumdb not null.
In my select query, I am doing the following:
SELECT CAST(CONCAT(idnum, idnumdb) AS BIGINT)
FROM TABLE
When I execute the query, some data will be display but then while the query is displaying data, I get the error mentioned above.
I don't understand why since both idnum and idnumdb are int datatype in the table. I am assuming the concat function is messing up the data. Any ideas what's going on here? Thanks in advance

thanks everyone for helping out. i tried Udai solution of executing
SELECT CONCAT(idnum, idnumdb) FROM TABLE WHERE ISNUMERIC(CONCAT(idnum, idnumdb) ) <> 1
it turns out that idnumdb has a - sign for a few rows. i test on idnum only and that was fine. idnumdb was the problem. thanks all

Related

SQL CAST with ISNULL results in conversionfailure

I've got a nullable column of numbers, which is unfortunately declared as a varchar in the database, and so I want to convert the NULL to 0. Doing this accomplishes that.
select ISNULL(col, '0') from table;
So now there are no null possibilities, and then I want to convert the column to an actual int value, so I tried to wrap that.
select CAST(ISNULL(col, '0') AS INT) from table
When that runs I get a conversion error, as shown below. I don't understand why, or how to get around this:
Conversion failed when converting the varchar value 'NULL' to data type int.
Try like this: Take your ISNULL statement outside
SELECT ISNULL(TRY_CAST(col AS INT),0) AS [col]
FROM SAMPLE_TAB;
If this does not help, then please comment your SQL version.

MSSQL + Laravel: "Error by converting varchar to bigint"

Actually, I can't insert data into a table with big number.
The primary key has type of BIGINT, when I try to insert a row I'm getting this error.
Same thing happens when I try it manually with HeidiSQL.
It seems you are going to insert string/varchar/nvarchar data to your BIGINT type column. please check your data.
The problem is solved. It's been caused by a trigger in the database, which tried to put a string with "8001049473-A1-R" into a bigint field. After deleting that row containing the "8001049473-A1-R", everything works.

Error in stored procedure but no error when running it manually

I have a stored procedure that creates a xml file. When I run it, I get an error
Conversion failed when converting the varchar value 'some text here' to int
If I put the same code in the query window in SSMS and execute it, it works fine - no error.
Can someone tell me why could that happen? The procedure is long enough to post it here, it's mainly a huge select with lots of unions and joins.
Thank you!
Somewhere in one of the columns in your join is comparing a varchar to an integer, and at least one of the varchar values is not an integer.
So try the following test:
select *
from table
where myCol NOT LIKE '%[^0-9]%'
and perhaps
select *
from table
where CONVERT (bigint, myCol) NOT BETWEEN -2147483648 AND 2147483647
Shake the tree and see what falls off.

Using Sum(Cast(Replace syntax

I am trying to write a simple query in order to change some of our stage data. I have a varchar $ column (unfortunately) that needs to be summed. My issue is that because of commas, I cannot change the datatype.
So, I can use REPLACE(AMT,',','') to remove the commas but It still wont let me cast it as a decimal and I get
Error converting data type varchar to numeric.
I am trying the following below with no luck. Any ideas? Can this be done or am I using the wrong syntax here?
Select SUM(Cast(REPLACE(Amt,',','') as Decimal (18,2)) )
I was able to resolve this with #HABO suggestion. I used Cast(Ltrim(rtrim(table.AMT)) as Money) for all instances of the varchar amount. This removed white space and removed the commas from the numbers.
This should work... including an example.
Edit: if you are on SQL Server 2012+, you may be able to shorten your task by using Try_Convert
DECLARE #SomeTable AS TABLE (Amt Varchar(100));
INSERT INTO #Sometable (Amt) VALUES ('abc123,456.01'),(' 123,456.78 '),(Null),('asdad'),('');
With NumericsOnly AS
(
SELECT
REPLACE(Left(SubString(Amt, PatIndex('%[0-9.-,]%', Amt), 8000), PatIndex('%[^0-9.,-]%', SubString(Amt, PatIndex('%[0-9.,-]%', Amt), 8000) + 'X')-1),',','') AS CleanAmt
FROM
#SomeTable
)
SELECT
SUM(CONVERT(DECIMAL(18,2), CleanAmt)) AS TotalAmt
FROM
NumericsOnly
WHERE
IsNumeric(CleanAmt)=1
General methodology is taken from here
I wouldn't use money as a data type as it is notorious for rounding error.
The error is due to SQL order of operations within your SUM(CAST(REPLACE... operation. This issue can be resolved by summing the column AFTER it's been staged to be summed via a subquery:
SELECT SUM(Field),...
FROM ( SELECT
Cast(REPLACE(Amt,',','') as NUMERIC) as 'Field'
,...
) [Q]
If the table you're summing is administered by a BI Team, get them to stage the data there. Happy Data Happy life.

Converting 78.3 F (nvarchar) to 78.3 (numeric)

Good afternoon, all!
Trying to get temperature off a database on my SQL server stored as "78.3 F" and make it only the number "78.3". The database has this field as nvarchar. I tried:
select
Cast(Left(Paint_Environ.Booth1_Temp, Len(Paint_Environ.Booth1_Temp) - 2) As NUMERIC(4,1)) As Temp
from Paint_Environ
but get an errors saying "Error converting data type nvarchar to numeric" Is there a better way to convert nvarchar to something numeric???
Thanks in advance!
Matt
As I mentioned in the comments, your code should work with data '78.3 F'
You may try this
SELECT
CAST(REPLACE(Booth1_Temp,'F','') AS NUMERIC(4,1)) As Temp
FROM Paint_Environ
UPDATE:
You may start debug with this to see which data format give you error
SELECT Booth1_Temp
FROM Paint_Environ
WHERE ISNUMERIC(REPLACE(Booth1_Temp,'F','')) <> 1

Resources