0

I have a Postgres DB with some very bad schema design, I'm trying to join two tables with and ID and a field which has the ID but with a couple of characters at the beginning, how would I join these?

eg.

TABLE1
------
Field name: ID
Value: 1000

TABLE2
------
Field name: NUMBER
Value: WO-1000

so basically I need it to say something like:

JOIN TABLE2 ON (TABLE1.ID = 'WO-' + TABLE2.NUMBER)

Thanks

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Mankind1023
  • 6,169
  • 13
  • 53
  • 73

1 Answers1

1

Use || operator to concatenate two values in Postgres. Try this.

JOIN TABLE2 ON (TABLE1.ID = 'WO-' ||  TABLE2.NUMBER)

If your Number column is of Integer type then cast it to text.

JOIN TABLE2 ON (TABLE1.ID = 'WO-' ||  cast(TABLE2.NUMBER as text)) --or TABLE2.NUMBER::text

Check here for more info

Community
  • 1
  • 1
Pரதீப்
  • 88,697
  • 17
  • 124
  • 160