๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ cast-convert-nvarchar-into-int
cast / convert NVARCHAR into INT โ€“ SQLServerCentral Forums
September 22, 2007 - Need to Insert an NVARCHAR(2) column into an INT column. INSERT INTO tbl_a (col_A) VALUES SELECT col_B FROM tab_b ... Also, is there a simple way to account for non-numeric col_B source values in this INSERT statement? ... Melissa L. Adams ... I am having a similar problem. The case and convert ...
Discussions

How do I convert int to nvarchar and vice versa - SQL Server Forums
Microsoft SQL Server articles, forums and blogs for database administrators (DBA) and developers. More on sqlteam.com
๐ŸŒ sqlteam.com
July 4, 2007
ms access - convert nvarchar to int - Database Administrators Stack Exchange
I have a table in access linked to SQL.I am doing a query where I am using a field named comment from this table. The field is designed in SQL as nvarchar(255) . I am writing integers numbers in this field. What I want to do in my query is to convert this field from nvarchar to int so I can ... More on dba.stackexchange.com
๐ŸŒ dba.stackexchange.com
September 24, 2014
Not able to cast nvchar as number
I have checked the column and all it has in it is nulls or integers. But the error messages says, the column is of type nvarchar. I guess, there is an "invisible" character in the data, like control chars (tab, CR, LF), which makes the implicit (!) conversion failing. You can try using the ISNUMERIC (Transact-SQL... More on learn.microsoft.com
๐ŸŒ learn.microsoft.com
4
0
Possible cast issue with Int and nvarchar data
You are not using fieldtext as a number, so change your case/when conditions to string comparison to avoid implicit conversions: WHEN LIPR.FieldText = '263' THEN More on reddit.com
๐ŸŒ r/SQL
5
6
August 12, 2022
๐ŸŒ
SQL Team
sqlteam.com โ€บ forums โ€บ topic.asp
How do I convert int to nvarchar and vice versa - SQL Server Forums
July 4, 2007 - Microsoft SQL Server articles, forums and blogs for database administrators (DBA) and developers.
Top answer
1 of 2
2

Here are some options.

  • CBoolโ€”Converts a value to a Boolean data type.
  • CDateโ€”Converts a value to a Date data type.
  • CIntโ€”Converts a value to an Integer data type.
  • CStrโ€”Converts a value to a String data type.
  • CVarโ€”Converts a value to a Variant data type.

Syntax

  • CBool(expression)
  • CByte(expression)
  • CCur(expression)
  • CDate(expression)
  • CDbl(expression)
  • CDec(expression)
  • CInt(expression)
  • CLng(expression)
  • CSng(expression)
  • CStr(expression)
  • CVar(expression)

Converting to an Integer Data Type

The CInt function takes a numeric or string value and converts it to an Integer data type. The argument is required and needs to represent a value within the range of -32,678 to 32,767. If the argument contains a decimal, Access rounds to the next whole number. A value of .5 or higher is rounded up; anything lower is rounded down. Some examples of CInt functions follow:

Dim MyDouble, MyInt
MyDouble = 2345.5678    ' MyDouble is a Double.
MyInt = CInt(MyDouble)    ' MyInt contains 2346.

IN SQL, you could use CAST.

SELECT
   CAST([Column] AS INT) AS [Column Name]
FROM [Table]
WHERE ...

Or, if you wanted to SUM off the SQL query.

SUM(CAST([Column] AS INT)) AS [Column Name]
2 of 2
1

Perhaps you should store the data in SQL Server as an INT field, instead of a VARCHAR(255) field?

If you did that, you wouldn't need to make any conversions in Access.

Having said that, if you don't want to make changes to the SQL Server database, you can use VB's CInt(field) in Access queries to convert numbers stored as a string into integers. Be aware this approach will fail if there are any non-numeric values stored in the varchar field.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ sql-query-to-convert-numeric-to-nvarchar
SQL Query to convert NUMERIC to NVARCHAR - GeeksforGeeks
November 17, 2022 - Here we will see, how to convert NUMERIC data to NVARCHAR data in a MS SQL Server's database table using the CAST(), CONVERT() and FORMAT() functions. We will be creating a person table in a database called "geeks".
Top answer
1 of 4
1

"I have checked the column and all it has in it is nulls or integers."

You might have checked the data visually, in that case you have garbage in the data which doesn't show up visually.

The trick is to find those rows, and those rows only. Below show you how you can do that. In your WHERE clause, use IS NOT NULL so you don't return the rows which are NULL and also use TRY_CAST to find those that aren't convertible to int:

DROP TABLE IF EXISTS #t

CREATE TABLE #t(c1 int identity, c2 nvarchar(10))

INSERT INTO #t(c2) VALUES ('1')
INSERT INTO #t(c2) VALUES ('123')
INSERT INTO #t(c2) VALUES (NULL)
INSERT INTO #t(c2) VALUES ('12' + CHAR(9)) --Note that CHAR(9) is TAB

--It looks like data is fine!
SELECT * FROM #t

--But conversion fails because of TAB
SELECT c1, CAST(c2 AS int)
FROM #t

--How to find the failed rows, and those only?
SELECT c1, c2
FROM #t
WHERE c2 IS NOT NULL
AND TRY_CAST(c2 AS int) IS NULL
2 of 4
1

I would suggest the following approach.
Change CAST(...) to TRY_CAST(...)

It will allow you to filter just questionable rows for analysis. After that you will be able to adjust the conversion/casting statement.

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, DEFINED_KEY1 NVARCHAR(100) NULL);
INSERT INTO @tbl (DEFINED_KEY1) VALUES
(N''),
(NULL),
(N'1100000'),
(N'0');
-- DDL and sample data population, end
-- all rows
SELECT * 
 , TRY_CAST(ISNULL(M.DEFINED_KEY1, 0) AS NUMERIC(9, 0)) AS [No_of_Cats]
FROM @tbl AS m;
-- failing to convert
;WITH rs AS
(
 SELECT * 
 , TRY_CAST(ISNULL(M.DEFINED_KEY1, 0) AS NUMERIC(9, 0)) AS [No_of_Cats]
 FROM @tbl AS m
)
SELECT * FROM rs
WHERE [No_of_Cats] IS NULL;
๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ possible cast issue with int and nvarchar data
r/SQL on Reddit: Possible cast issue with Int and nvarchar data
August 12, 2022 -

I have a column that contains both numbers and text. One I run each these selects separately I have no problems. However, when I try and include both column in one query I'm getting conversion failed when converting nvarchar value "Name" to int. I tried adding CAST but it didn't do the trick.

SELECT DISTINCT 


CASE 
        WHEN LIPR.IpMapFieldId = 137 AND CAST(FieldText AS nvarchar(MAX)) IS NOT     
    NULL THEN CAST(FieldText AS nvarchar(max)) ELSE M.MarkName END AS 
    'CHAIN__C',

CASE
	WHEN LIPR.FieldText  = 262 THEN 'Brand'
	WHEN LIPR.FieldText  = 263 THEN 'Chain'
	WHEN LIPR.FieldText  = 264 THEN 'Chain'
	WHEN LIPR.FieldText  = 265 THEN 'Chain'
	ELSE ' ' END

FROM MARK M JOIN LnkIpRecordFields LIPR ON LIPR.RecordId = M.MarkId JOIN RefIpFields RIF ON RIF.IpMapFieldId = LIPR.IpMapFieldId 
WHERE RecordId = '81039538' 
AND CAST(LIPR.IpMapFieldId AS nvarchar(MAX)) IN (136,137)

Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ func_sqlserver_cast.asp
SQL Server CAST() Function
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... The CAST() function converts a value (of any type) into a specified datatype.
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 27773882 โ€บ Converting-nvarchar-to-int.html
Solved: Converting nvarchar to int | Experts Exchange
June 28, 2012 - Do they have decimal points? ... >converts nvarchar values (@pricemin,@pricemax) to int. Use the CAST function. Declare @str nvarchar(10) = '1234567890' SELECT CAST(@str as int) ...
๐ŸŒ
Databasefaqs
databasefaqs.com โ€บ home โ€บ how to convert int to varchar in sql server
How to Convert Int to Varchar in SQL Server - DatabaseFAQs.com
May 22, 2024 - Although it can also be used for basic type conversions, the SQL Server-specific CONVERT function provides more flexibility with style parameters for date and time conversions. Below is the syntax ยท SELECT CONVERT(VARCHAR(length), your_int_column) AS your_varchar_column FROM your_table;
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-Varchar-to-Int-in-SQL-server
How to convert Varchar to Int in SQL server - Quora
Programming since 1970, now in T-SQL and vb.net with WPF ยท Author has 326 answers and 304.4K answer views ยท 3y ยท Data type conversion from Int to varchar (or nvarchar) is implicit.
Top answer
1 of 3
5

You have a few problems here:

The @@IDENTITY is a system function contains the last identity value that is generated when an INSERT, SELECT INTO, or BULK COPY statement is completed. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. If multiple rows are inserted, generating multiple identity values, @@IDENTITY returns the last identity value generated.

In your case, you have an INSTEAD OF INSERT trigger, so there is no INSERT.

This below query is completely wrong and will gives wrong results, it works as expected only if one row inserted, if there is more than 1 row, then those variables will hold just the values of one row, and you will lose the other values of the other rows, cause the pseudo INSERTED may contains 1 or more rows

select @AgentCode=AgentCode,
       @NationalCode=NationalCode,
       @FirstName=FirstName,
       @LastName=LastName,
       @IsActive=IsActive 
from inserted

Now, looking to your table, you already have an IDENTITY column, so you don't need to a TRIGGER at all, you can just make a computed column as

CREATE TABLE [dbo].tblAIAgent),
    [NationalCode] [bigint] NOT NULL 
    CONSTRAINT [DF_tblAIAgent_NationalCode]  DEFAULT ((0)),
    [FirstName] nvarchar NOT NULL 
    CONSTRAINT [DF_tblAIAgent_Name]  DEFAULT (''),
    [LastName] nvarchar NOT NULL 
    CONSTRAINT [DF_tblAIAgent_Family]  DEFAULT (''),
    [IsActive] [bit] NOT NULL 
    CONSTRAINT [DF_tblAIAgent_Active]  DEFAULT ((1)),
    [Counter] [int] IDENTITY(1,1) NOT NULL,
    CONSTRAINT [PK_tblAIAgent] PRIMARY KEY ([Counter])
    );

UPDATE:

According to your comment "a computed column can no longer be selected as the PK. I want this column to be placed in other relevant tables as a FK.I wrote the trigger to get the column value instead of the computed column so that I can select the column as the primary key". You are trying to make it a PRIMARY KEY so you can do as

CREATE TABLE T(
  Counter INT IDENTITY(1,1) NOT NULL,
  OtherCol INT,
  Computed AS CONCAT('Agent_', CAST(Counter AS VARCHAR(10))) PERSISTED,
  CONSTRAINT PKT PRIMARY KEY(Computed)
);

CREATE TABLE TT(
  ReferenceComputedColumn VARCHAR(16) NOT NULL,
  OtherColumn INT,
  CONSTRAINT FK_ReferencedComputedColumn 
      FOREIGN KEY(ReferenceComputedColumn) 
      REFERENCES T(Computed)
)

INSERT INTO T(OtherCol) VALUES
(1), (2), (3);

INSERT INTO TT(ReferenceComputedColumn, OtherColumn) VALUES
('Agent_1', 10),
('Agent_3', 20);

SELECT *
FROM T LEFT JOIN TT 
ON T.Computed = TT.ReferenceComputedColumn;

See how it's working.

See also this article Properly Persisted Computed Columns by Paul White.

2 of 3
1

Try this

SELECT CONVERT(NVARCHAR(255), @AgentCode)
๐ŸŒ
MSSQLTips
mssqltips.com โ€บ home โ€บ sql convert int to string
SQL Convert INT to String
May 28, 2025 - To do that, we will try different methods. The following example shows how to use the SQL CAST function for an int to string conversion. In this example, we are converting the OrderQty which is an integer into varchar with SELECT CAST syntax.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ convert-int-to-varchar-sql
Convert INT to VARCHAR SQL - GeeksforGeeks
July 23, 2025 - One of the basic SQL operations used to convert numerical data into string format is the conversion of INT to VARCHAR. we have come across a number of way, including CAST, CONVERT, CONCAT, and FORMAT, all of which provide different ways to accomplish the conversion.
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ how to convert nvarchar to int in sql
How to convert nvarchar to int in SQL - Intellipaat
February 7, 2025 - You can convert nvarchar(string/text) into an integer using SQL commands like CAST, or CONVERT. SQL, Structured Query Language, is a tool to manage and interact with databases. SQL allows us to convert datatypes using CAST, CONVERT, TRY_CAST, ...