如何在 SQL (postgres) 中解析字符串并创建行

发布于 2024-07-27 16:35:31 字数 701 浏览 2 评论 0原文

我有一个数据库字段,其中包含开始日期、结束日期和表单中的排除项,

available DD/MONTH/YYYY [to DD/MONTH/YYYY]?[, exclude WORD [, WORD]*]?

这意味着它始终以“可用 DD/MONTH/YYYY”开头,可选地具有单个“到 DD/MONTH/YYYY”,并且可选地具有排除子句,它是逗号分隔的字符串列表。 思考 + 、 * 和 ? 的正则表达式含义

我的任务是提取数据,因此我们现在将有一个“开始日期”列、“结束日期”列和一个包含排除项的新表。 它将需要使用从可用性字符串解析的值来填充开始日期和结束日期列。 它还需要在新的排除表中创建多条记录,一条记录对应可用性字符串中“排除”关键字后面的每个逗号分隔值。

这是我只能在 SQL 中进行的迁移吗(postgres 8.4)?

这是针对 postgres 8.4 的。

更新:在同事的帮助下,我们现在有了一个 sql 脚本,它的结果 sql 可以根据排除的解析执行插入语句。 它在 sql 中使用一堆 case 语句和字符串操作来生成结果。 然后,我将输出发送到文件并执行该文件以执行插入。 我对开始日期和结束日期列执行相同的操作。

它不是 100% sql,而是运行第一个 .sql 文件的简单 .bat 或 .sh 文件,然后生成的文件就是让它运行所需的全部。

感谢您的意见。

I have a single database field that contains a start date, end date, and exclusions in the form

available DD/MONTH/YYYY [to DD/MONTH/YYYY]?[, exclude WORD [, WORD]*]?

Meaning it always starts with "available DD/MONTH/YYYY", optionally has a single "to DD/MONTH/YYYY", and optionally has an exclude clause that is a comma separated list of strings. Think regular expression meanings for + , *, and ?

I have been tasked with extracting the data out so we will now have a "startdate" column, "enddate" column, and a new table that will contain the exclusions. It will need to fill the startdate and enddate columns with the values parsed from the availability string. It will also need to create multiple records in the new exclusion table, one for each of the comma separate values after the 'exclude' key word in the availability string.

Is this a migration I can do in SQL only (postgres 8.4)?

This is against postgres 8.4.

Update: With the help of a co-worker we now have a sql script that has as it's results sql to perform the insert statements based on the parsing of the exclusions. It uses a bunch of case statements and string manipulation within the sql to generate the results. I then send the output to a file and execute that file to perform the inserts. I am doing the same for the start and end date columns.

It's not 100% sql, but a simple .bat or .sh file that runs the first .sql file, then the generated one is all that is needed to get it to go.

Thanks for the input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-08-03 16:35:31

您可以结合使用 正则表达式函数 (以及 to_date() 或 to_timestamp() 函数。

但是,在 pl/perl 中的函数中修改文本可能更容易。这将使您能够访问 Perl 中的完整操作函数,同时将工作保留在内部数据库似乎符合您的规范。

You can probably do that with a combination of the regexp functions ( and the to_date() or to_timestamp() functions.

But it may be easier to just mangle the text in a function in say pl/perl. That'll get you access to the full manipulation functions in perl, while keeping the work inside the database as seems to be your specification.

云巢 2024-08-03 16:35:31

为什么是单一SQL?

使用 Ruby/Python/Basic 编写简单的脚本,从源读取数据,解析数据,然后将其放入目标数据库。

或者说有这么大?

why single SQL?

Write simple script in Ruby/Python/Basic to read data from the source, parse it, and put into destination database.

Or it is so big?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文