select convert(nvarchar(255), 4343)
Should do the trick.
Answer from Olivier S on Stack OverflowHow do I convert int to nvarchar and vice versa - SQL Server Forums
ms access - convert nvarchar to int - Database Administrators Stack Exchange
Not able to cast nvchar as number
Possible cast issue with Int and nvarchar data
CONVERT takes the column name, not a string containing the column name; your current expression tries to convert the string A.my_NvarcharColumn to an integer instead of the column content.
SELECT convert (int, N'A.my_NvarcharColumn') FROM A;
should instead be
SELECT convert (int, A.my_NvarcharColumn) FROM A;
Simple SQLfiddle here.
You can always use the ISNUMERIC helper function to convert only what's really numeric:
SELECT
CAST(A.my_NvarcharColumn AS BIGINT)
FROM
A
WHERE
ISNUMERIC(A.my_NvarcharColumn) = 1
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]
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.
"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
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;
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)
Assuming your first table is named A with a column named MyIntColumn of type int and your second table is named B with a column named MyNVarcharColumn of type nvarchar, you could do
From A
Inner Join B On A,MyIntColumn = Try_Cast(B.MyNVarcharColumn As int)
Tom
Hi All,
I faced the same issue whenever a maintenance job was done by Azure Managed Service. The strange thing is that I got this exception from my services, and it was only resolved after a restart.
What happened in between?
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
Use the STR function:
SELECT STR(field_name) FROM table_name
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
source: https://msdn.microsoft.com/en-us/library/ms189527.aspx
are you sure the error is related to the casts and not to the where conditions? check that the column types are matching
i think this'll do :
SELECT (CONVERT(NVARCHAR, GotoviProizvodi.ID) + ' - ' + GotoviProizvodi.Opis) AS Opis
FROM GotoviProizvodi,Recept
WHERE Recept.ID=@ID
AND Recept.Proizvod=GotoviProizvodi.Opis
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.
Try this
SELECT CONVERT(NVARCHAR(255), @AgentCode)