Convert nvarchar to bigint in Sql server 2008 - sql

I want insert all rows of a table into another table, and I also want convert a nvarchar field into bigint, but when I use convert(bigint, col1) SQL Server shows an error:
Error converting data type nvarchar
to bigint
How can I fix this problem?

You could try to use ISNUMERIC to determine those rows that are indeed numeric:
UPDATE dbo.YourTable
SET BigIntColumn = CAST(NVarcharColumn AS BIGINT)
WHERE ISNUMERIC(NVarcharColumn) = 1
That would convert those rows that can be converted - the others need to be dealt with manually.

You should convert bigint to nvarchar not vice versa
cast(Other_Column_name as nvarchar) not cast (Column_Name as bigint)

you can try this:
CAST(CAST(col1 as NUMERIC) as BIGINT)

Here I convert navrchar value of column into bigInt and than perform Addition of thos two columns find blow :
SELECT (CAST(col1 AS BIGINT) + CAST(col2 AS BIGINT)) AS TotalValue from tableName

Related

Conversion failed when converting the nvarchar value to data type int - Error Message

My Query
Select * from MyTable
The table consists 300k rows.
It runs for 200k+ rows and this error pops up.
How to handle this to get the full data?
Does MyTable have any computed columns?
Table consists of a computed column with the name IsExceeds which is given below for your reference.
This is the computed column formula:
(CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))
Field Definitions:
[Pro_PCT] [nvarchar](50) NULL,
[Max_Off] [nvarchar](50) NULL,
[IsExceeds] AS (CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))
Kindly convert in float then convert into int.
declare #n nvarchar(20)
set #n='11.11'
if (isnumeric(#n)=0)
SELECT 0
else
SELECT CAST(CONVERT(float, #n) as int) AS n
Why are you storing amounts as strings? That is the fundamental problem.
So, I would suggest fixing your data. Something like this:
update mytable
set max_off = replace(max_off, '%');
alter table mytable alter pro_pct numeric(10, 4);
alter table mytable alter max_off numeric(10, 4);
(Without sample data or an example of the data I am just guessing on a reasonable type.)
Then, you can define IsExceeds as:
(Pro_PCT - Max_Off)
Voila! No problems.
Based on the formula - either Pro_PCT or Max_Off contains the value 11.11 (well, with an extra % for Max_Off. Perhaps they also contain other values that can't be converted to int.
Here's what you can do to find all the rows that will cause this problem:
Select *
from MyTable
where try_cast(Pro_PCT as int) is null
or try_cast(replace([Max_Off],'%','') as int) is null
After you've found them, you can either fix the values or change the calculation of the computed column to use try_cast or try_convert instead of convert.
Check the bad data with
select * from MyTable where isnumeric( Max_Off ) = 0

mssql - copy different datatypes datas from one column to another column in same table

I Tried to copy data's from one column to another column in same table by using following code.
UPDATE [dbo].[name] SET [name_HId] = [name_Id]
while running this query its showing an error which is
Operand type clash: int is incompatible with hierarchyid
Here the name_HId is in hierarchyid datatype and the name_Id is in int.
how can solve this problem?
just cast the int value to varchar:
UPDATE [dbo].[name]
SET [name_HId] = Cast([name_Id] as varchar(max))
add the slashes and then cast to hierarchyid
select cast('/' +cast(5 as varchar)+ '/' as hierarchyid)
When you cast a hierarchyid to string, it will look something like '/5/' or '/5/8/'

Why does MS SQL CAST as bigint result in int overflow?

Why does the following T-SQL statement:
select CAST(COALESCE('3537601039',0) as bigint)
result in the following error when run against MS SQL Server 10?
The conversion of the varchar value '3537601039' overflowed an int
column.
Is there a "more correct" way to do this conversion of a varchar to a bigint that would avoid this problem when the number is within range of a bigint?
The first thing that is happening is that your string is trying to convert to a regular int to match the 0. Try this:
select CAST(COALESCE('3537601039','0') as bigint)
It appears that putting the unqualified zero in the coalesce is implying a conversion to a smaller int before it is explicitly recast to a bigint.
The following simple change to make it a coalesce of string values solves the problem:
select CAST(COALESCE('3537601039','0') as bigint)
Cast first, then do ISNULL
select ISNULL(CAST('3537601039' as bigint), 0)
select ISNULL(NULLIF(ISNULL(CAST('3537601039' as bigint), 0), 0), 1) + 3537601039
sql server firstly discovers isnull(string, numeric) and that numeric zero is by default int. So it tries to convert that string containing bigint to int. You were trying to cast to bigint too late.
another try:
select ISNULL('3537601039', 100000000000)

Data type varchar to numeric

I have a table with Varchar values that are numbers, and I want them to be decimal values. I'm getting an error saying Error converting data type varchar to numeric. I've tried:
SELECT ROUND(CAST(MYCOLUMN AS decimal(10, 2)), 2)
FROM TABLE
and
ALTER TABLE MYTABLE
ALTER COLUMN MYCOLUMN DECIMAL(10,2)
and
ALTER TABLE MYTABLE
MODIFY COLUMN ACQ_FIELD_8 DECIMAL(10,2)
and
SELECT ISNULL(CAST(NULLIF(MYCOLUMN, 'NULL') AS NUMERIC(10,2)), 0)
FROM MYTABLE
and I keep getting the same error. I looked the through the data to look for any special characters or letters by using:
SELECT MYCOLUMN FROM MYTABLE
WHERE MYCOLUMN LIKE '%[a-zA-Z]%'
or MYCOLUMN LIKE '%[(]%' --etc for each character.
The only thing that I have found is that every value for some reason has a '^' in it, but when I replace the '6' with nothing, the value still can't be converted to a decimal or numeric. If I try ordering the column by number (< 0.00), I get Arithmetic overflow error converting varchar to data type numeric.
Anyone know what to do?
Try this :
select round(try_convert(decimal(10,2), Mycolumn),2)
looks like you have some values which can not be converted to decimal. try_convert will convert them as NULL
You can try isnumeric:
SELECT ROUND(CAST(MYCOLUMN AS decimal(10, 2)), 2)
FROM TABLE
WHERE isnumeric(MYCOLUMN) = 1

Using SQL 2005 trying to cast 16 digit Varchar as Bigint error converting

First, thanks for all your help! You really make a difference, and I GREATLY appreciate it.
So I have a Varchar column and it holds a 16 digit number, example: 1000550152872026
select *
FROM Orders
where isnumeric([ord_no]) = 0
returns: 0 rows
select cast([ord_no] as bigint)
FROM Progression_PreCall_Orders o
order by [ord_no]
returns: Error converting data type varchar to bigint.
How do I get this 16 digit number into a math datatype so I can add and subtract another column from it?
UPDATE: Found scientific notation stored as varchar ex: 1.00054E+15
How do I convert that back into a number then?
DECIMAL datatype seems to work fine:
DECLARE #myVarchar AS VARCHAR(32)
SET #myVarchar = '1000550152872026'
DECLARE #myDecimal AS DECIMAL(38,0)
SET #myDecimal = CAST(#myVarchar AS DECIMAL(38,0))
SELECT #myDecimal + 1
Also, here's a quick example where IsNumeric returns 1 but converting to DECIMAL fails:
DECLARE #myVarchar AS VARCHAR(32)
SET #myVarchar = '1000550152872026E10'
SELECT ISNUMERIC(#myVarchar)
DECLARE #myDecimal AS DECIMAL(38,0)
SET #myDecimal = CAST(#myVarchar AS DECIMAL(38,0)) --This statement will fail
EDIT
You could try to CONVERT to float if you're dealing with values written in scientific notation:
DECLARE #Orders AS TABLE(OrderNum NVARCHAR(64), [Date] DATETIME)
INSERT INTO #Orders VALUES('100055015287202', GETDATE())
INSERT INTO #Orders VALUES('100055015287203', GETDATE())
INSERT INTO #Orders VALUES('1.00055015287E+15', GETDATE()) --sci notation
SELECT
CONVERT(FLOAT, OrderNum, 2) +
CAST(REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120), '-', '') AS FLOAT)
FROM #Orders
WITH validOrds AS
(
SELECT ord_no
FROM Orders
WHERE ord_no NOT LIKE '%[^0-9]%'
)
SELECT cast(validOrds.ord_no as bigint) as ord_no
FROM validOrds
LEFT JOIN Orders ords
ON ords.ord_no = validOrds.ord_no
WHERE ords.ord_no is null
Take a look at this link for an explanation of why isnumeric isn't functioning the way you are assuming it would: http://www.sqlservercentral.com/articles/IsNumeric/71512/
Take a look at this link for an SO post where a user has a similar problem as you:
Error converting data type varchar
hence, you should always use the correct datatype for each column unless you have a very specific reason to do so otherwise... Even then, you'll need to be extra careful when saving values to the column to ensure that they are indeed valid values