Snippets

Christian Pernillo Python - Replace a varchar field in all tables mssql

Created by Christian Pernillo
import pyodbc

import pandas as pd

import sys

DATABASE_NAME = "DRIVER_FIX_P2P"

# Specifying the ODBC driver, server name, database, etc. directly

cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=34.198.63.58;DATABASE=DRIVER_FIX_P2P;UID=sa;PWD=$omos9ixtig#1')

# Create a cursor from the connection
cursor = cnxn.cursor()

cursor.execute("SELECT table_name, column_name FROM information_schema.columns WHERE data_type='nvarchar'")
rows = cursor.fetchall()

for row in rows:
    cursor.execute("UPDATE " + row[0] + " SET " + row[1] + " = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("+ row[1] +", 'á', 'á'), 'é', 'é'), 'ó', 'ó'), 'ú', 'ú'), 'ñ', 'ñ'), 'Ã', 'í')")
    print("Ejecutando update en la tabla: " + row[0] + " y en el campo " + row[1])
cnxn.commit()

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.