Reputation: 1257
I have a table where I'm trying to create the value of the attribute from one column based off the attribute of the value of another column...
Essentially, one column is a long series of numbers... such as 540379724021081 and I am trying to for that tuple, make another attribute value of the first 4 characters so...5403
So in the end my table would go from...
I started doing this in Python with Psycopg2...but don't think it's the right way with a quick script I made
import psycopg2
conn = psycopg2.connect("dbname='gis3capstone' user='postgres' password='password' host='localhost' port='5433'")
cur = conn.cursor()
cur.execute('SELECT * FROM fcc_form_477')
row = cur.fetchone()
while row:
val2 = str(row[0])[0:4]
# Set row[1] = val2 ??
row = cur.fetchone()
Any help to go about this?
EDIT: or SQL...if I can do it that way
Upvotes: 0
Views: 52
Reputation: 1269973
You can use substr()
:
select substr(val1, 1, 4) as val2
If the value is a number, then convert it to a string:
select substr(val1::text, 1, 4) as val2
Upvotes: 1