Home SQL Server: Replace values in field using lookup other table
Reply: 0

SQL Server: Replace values in field using lookup other table

user1283
1#
user1283 Published in July 17, 2018, 7:57 am

Let's say I have below table script

DECLARE @result TABLE
   (
    [ID] Int
   ,[Data] Varchar(500)
   )
DECLARE @codes TABLE
   (
    [ID] Varchar(500)
   ,[FullNames] Varchar(500)
   )
INSERT   INTO @result
         SELECT   1
                 ,'[A]-[B]'
INSERT   INTO @result
         SELECT   2
                 ,'[D]-[A]'
INSERT   INTO @result
         SELECT   3
                 ,'[A]+[C]'

INSERT   INTO @codes
         SELECT   'A'
                 ,'10'
INSERT   INTO @codes
         SELECT   'B'
                 ,'20'
INSERT   INTO @codes
         SELECT   'C'
                 ,'30'
INSERT   INTO @codes
         SELECT   'D'
                 ,'40'

SELECT * FROM @result
SELECT * FROM @codes

Output of those are as below: @result

ID  Data
--  -------
1   [A]-[B]
2   [D]-[A]
3   [A]+[C]

@codes

ID  FullNames
--  -------
A   10
B   20
C   30
D   40

Now I want output as below also:

ID  Data
--  -----
1   10-20
2   40-10
3   10+30

Please help me. Please note: Data columns also contains ([A]-[B]+[D])*[C]

I found similar solution on https://stackoverflow.com/a/26650255/8454103 which is for your reference.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.360918 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO