SELECT phone_number FROM customers;
UPDATE customers
SET phone_number =
REGEXP_REPLACE(phone_number, '[^0-9]', '', 'g')
WHERE phone_number ~ '[^0-9]';
SELECT phone_number FROM customers;
UPDATE customers
SET phone_number =
REGEXP_REPLACE(phone_number, '([0-9]{3})([0-9]{3})([0-9]{4})',
'\1-\2-\3')
WHERE phone_number ~'^[0-9]{10}$';
SELECT
CONCAT(firstname, ' ', UPPER(SUBSTRING(lastname, 1, 1)), '.')
AS fullname,
email,
(SELECT CONCAT('***-***-',
RIGHT(phone_number, 4)) AS masked_phone_number)
FROM customers;
SELECT order_id, street, city, state, zip_code FROM orders;
UPDATE orders
SET
street = INITCAP(TRIM(street)),
city = INITCAP(TRIM(city)),
state = UPPER(TRIM(state)),
zip_code = SUBSTRING(REGEXP_REPLACE(TRIM(zip_code), '[^0-9]', '', 'g'), 1, 5)
WHERE (
street != INITCAP(TRIM(street)) OR
city != INITCAP(TRIM(state)) OR
state != UPPER(TRIM(state)) OR
SUBSTRING(REGEXP_REPLACE(TRIM(zip_code), '[^0-9]', '', 'g'), 1, 5) != zip_code OR
LENGTH(zip_code) != 5);
SELECT * FROM bookmarks;
INSERT INTO bookmarks (url, name, description)
SELECT 'http://example.com/' || generate_series AS url,
'Bookmark ' || generate_series AS name,
'Description for Bookmark ' || generate_series AS description FROM generate_series(1,50) AS generate_series
RETURNING *;
通过从1到50生成一系列数字,将生成的数字与预定义的字符串连接起来,并将结果分别插入“url”、“name” 和 “description” 字段。
SELECT * FROM users;
ALTER TABLE users
ADD COLUMN password_hash VARCHAR(255),
ADD COLUMN password_salt VARCHAR(255);
UPDATE users
SET password_salt = substr(md5(random()::text), 1, 16);
UPDATE users
SET password_hash = md5(concat(password_salt, password))
WHERE password_hash IS NULL;
SELECT pid, query, xact_start, wait_event, wait_event_type
FROM pg_stat_activity
WHERE backend_type = 'client backend'
AND wait_event IS NOT NULL;
SELECT pg_cancel_backend(3236);
SELECT pid, query, xact_start, wait_event, wait_event_type
FROM pg_stat_activity
WHERE backend_type = 'client backend'
AND wait_event IS NOT NULL;