如何从 sql 中的列中删除年份?
how to remove year from column in sql?
我在 python 中有这个解决方案,我正试图在 SQL 服务器中复制它。
movies_df['title']
title
Toy Story (1995)
The 2000 Year Old Man (1975)
The With (2) Sant (1999)
我运行这段代码去掉了年份
movies_df['title'] = movies_df.title.str.replace('((\d\d\d\d))', '')
输出:
title
Toy Story
The 2000 Year Old Man
The With (2) Sant
我在 SQL 中有一个名为 table 的电影。我想在 SQL
中获得相同的输出
select movie_title 来自电影
我该如何解决这个问题?
您可能已经发现,SQL 服务器没有模式替换功能。您需要使用模式匹配,然后删除那么多字符。我假设值总是在最后,并且总是以 space:
开头
SELECT V.Title,
STUFF(V.title,PATINDEX('% ([0-9][0-9][0-9][0-9])',V.Title),7,'')
FROM (VALUES('Toy Story (1995)'),
('The 2000 Year Old Man (1975)'),
('The With (2) Sant (1999)'))V(title)
我在 python 中有这个解决方案,我正试图在 SQL 服务器中复制它。
movies_df['title']
title |
---|
Toy Story (1995) |
The 2000 Year Old Man (1975) |
The With (2) Sant (1999) |
我运行这段代码去掉了年份
movies_df['title'] = movies_df.title.str.replace('((\d\d\d\d))', '')
输出:
title |
---|
Toy Story |
The 2000 Year Old Man |
The With (2) Sant |
我在 SQL 中有一个名为 table 的电影。我想在 SQL
中获得相同的输出select movie_title 来自电影
我该如何解决这个问题?
您可能已经发现,SQL 服务器没有模式替换功能。您需要使用模式匹配,然后删除那么多字符。我假设值总是在最后,并且总是以 space:
开头SELECT V.Title,
STUFF(V.title,PATINDEX('% ([0-9][0-9][0-9][0-9])',V.Title),7,'')
FROM (VALUES('Toy Story (1995)'),
('The 2000 Year Old Man (1975)'),
('The With (2) Sant (1999)'))V(title)