Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Computed Column error in View (Convertion of string to int)

(0) ShareShare
ReportReport
Posted on by 123
Hello,this is a method I use for creating a computed field in View and I need to filter for priceDiscTable.AccountRelation = "01". AccountRelation is String And I have a problem with the value I want to filter "01", because in SQL it is always converted to Int and I get this error: 'Conversion failed when converting the nvarchar value to data type int'.
public static server str getprice() 
{ 
str accRelation = "01";
 str price; 
ItemID itemId; 
itemId = SysComputedColumn::returnField(tableStr(PriceDisc_SAM),tableStr(InventTable),fieldStr(InventTable, itemId)); 

price = 'select top 1 priceUnit from PriceDiscTable where PriceDiscTable.ItemCode = 0 and PriceDiscTable.ItemRelation =' + itemId + ' and PriceDiscTable.AccountRelation = 'accRelation'+ 
and PriceDiscTable.Relation = 4 order by PriceDiscTable.FromDate Desc'; 
return price; }
The T-SQL definition I have is:
SELECT ITEMID, DATAAREAID, PARTITION, RECID, CAST
((SELECT TOP (1) PRICEUNIT
FROM dbo.PRICEDISCTABLE
WHERE (ITEMCODE = 0) AND (ITEMRELATION = T1.ITEMID) AND (ACCOUNTRELATION = 01) AND (RELATION = 4)
ORDER BY FROMDATE DESC) AS NUMERIC(32, 16)) AS PRICEUNIT
FROM dbo.INVENTTABLE AS T1
and I want it to be like :
SELECT ITEMID, DATAAREAID, PARTITION, RECID, CAST
((SELECT TOP (1) PRICEUNIT
FROM dbo.PRICEDISCTABLE
WHERE (ITEMCODE = 0) AND (ITEMRELATION = T1.ITEMID) AND (ACCOUNTRELATION = '01') AND (RELATION = 4)
ORDER BY FROMDATE DESC) AS NUMERIC(32, 16)) AS PRICEUNIT
FROM dbo.INVENTTABLE AS T1.
How should I write my method so 01 is taken as a string?

*This post is locked for comments

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Computed Column error in View (Convertion of string to int)

    Was your issue resolved? If yes, please mark the helpful answer(s) as verified. Thanks!

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 231,307 Most Valuable Professional on at
    RE: Computed Column error in View (Convertion of string to int)

    Note that SysComputedColumn class has helper methods for thiee purposes. In this case, you can use comparisonLiteral().

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Computed Column error in View (Convertion of string to int)

    In your statement you simply input 01 without quotation marks so that's exactly what you get.

    Maybe you could try this:

    price = "select top 1 priceUnit from PriceDiscTable where PriceDiscTable.ItemCode = 0 
    and PriceDiscTable.ItemRelation = '" + itemId + "' and PriceDiscTable.AccountRelation = '" +accRelation 
    + "' and PriceDiscTable.Relation = 4 order by PriceDiscTable.FromDate Desc"; 


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans