0

I'm trying to convert nvarchar separated with a comma to int, but I have a problem. I tried to use PARSENAME(REPLACE()), and it works fine but takes a long time to execute the code. I have a lot of tables, but here is an example.

I have a column that has name value and nvarchar datatype like (222,333,55,666,777,111). Every number will return color id, and then I use a function that will take converted value into text like(red/blue/green/black/gray). I want to split the value and convert the result into an int.

I have a function called GetTextAllLevel(int value) that takes int id and returns text. For example, it will take the value 222 and return red.

Tabel.GeTextAllLevels(CAST (Replace(Value, ',','.')) AS int) AS Category

I get syntax error at "AS", and I don't know why. I tried the code below that works with only four ids also:

GetCategoryLongName(((PARSENAME(REPLACE((fieldValue), ',', '.'),1)))) AS Category,
5
  • 1
    Never, ever store data as comma separated items. It will only cause you lots of trouble. Commented Feb 27, 2023 at 8:14
  • it is old database yhat I I work with. it is already exist. Commented Feb 27, 2023 at 8:16
  • Then fix the design, @rand Commented Feb 27, 2023 at 8:35
  • What do you mean@Larnu Commented Feb 27, 2023 at 8:41
  • I mean, why not fix the design? Because the database already stores delimited data doesn't mean that you then just ignore that problem; fix it. Normalise the design. Commented Feb 27, 2023 at 8:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.