0

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...

enter image description here

To this... enter image description here

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.