我需要弄清楚如何将我的“名称”字段显示为我的查询中单独的“名字”和“姓氏”字段。这是我的数据:
set linesize 250
drop table homework1data;
CREATE TABLE homework1data (
name VARCHAR2(30),
address VARCHAR2(30),
location VARCHAR2(30),
zip VARCHAR2(10)
);
INSERT INTO Homework1Data(Name, Address, Location, Zip)
VALUES('Ferguson, Shawn M.', '1940 Fountainview Court', 'Reynoldsburg, Ohio', '43068');
INSERT INTO Homework1Data(Name, Address, Location, Zip)
VALUES('Phillips, George', '19 Pleasant St.', 'Columbus, OH', '43231');
INSERT INTO Homework1Data(Name, Address, Location, Zip)
VALUES('Thompson, Mary', '200 E. Main St.', 'Columbus, Oh', '43215');
INSERT INTO Homework1Data(Name, Address, Location, Zip)
VALUES('Swatson, Robert', '584 Yellowstone Dr.', 'Westerville, OH', '43081');
INSERT INTO Homework1Data(Name, Address, Location, Zip)
VALUES('Banks, Heather T.', '19 Pleasant St.', 'Columbus, Ohio', '43231');
我想我需要使用带有LTRIM和RTRIM的select语句,但不确定如何这样做
更新
我这样做是为了打破我的领域:
SELECT substr(name, 1, instr(name, ',')) "Last Name",substr(name, instr(name, ' ', 1)) "First Name", address,substr(location, 1, instr(location, ',')) "City",substr(location, instr(location, ' ', 1)) "State", zip
FROM homework1data
ORDER BY zip, "Last Name" , "First Name";
但是现在我的查询报告在每列之间有太多空间,因此您必须使用滚动条来查看最后一个字段。这可调吗?尝试更改行和页面大小但未成功。
另外,刚才意识到由于某些原因我的所有行都有重复(可以在截图中看到),给我双倍的一切。不确定这是怎么发生的?
答案 0 :(得分:0)
试试这个:
SELECT
SUBSTRING_INDEX(name, ',', 1) AS FirstName,
SUBSTRING_INDEX(name, ',', -1) AS LastName,
address,
SUBSTRING_INDEX(location, ',', 1) AS State,
SUBSTRING_INDEX(location, ',', -1) AS City,
zip
FROM homework1data
答案 1 :(得分:0)
在Oracle SQL中,您可以使用如此强大的工具作为正则表达式:
SELECT
REGEXP_SUBSTR (name, '^\w+') as first_name,
REGEXP_SUBSTR (name, '\w+$') as last_name
FROM
homework1data