从字符串(Libreoffice Base)中提取年,月和一天的数量(1至2位)

发布于 2025-01-19 15:24:48 字数 446 浏览 3 评论 0原文

我正在使用Libreoffice Base(版本7.3)来创建数据库。 如下所示,我需要从构成中国角色的字符串中提取年,每月和一天的数字。我认为一个中国魅力是一个宪章,但我不确定。

我有什么: 1997年8月12日 1971年10月1日 2001年5月26日 2005年12月29日 2010年2月8日 ...

我想实现的目标:

  1. 我需要提取前4位数字;
  2. 第一个charactor(年)和第二个charactor(月)之间的数字为1到12(1或2位);
  3. 第二个charactor(月)和第三个charactor(日)之间的数字是一个月从1到31(1或2位数字)的天数;

我是新手,我尝试使用右()和left()函数或substring()方法的组合尝试,但是我失败了,因为几个月和天的数量有时为1位数字有时是1位数字。如果有人能提供帮助,我将非常感谢。太感谢了。

I am using LibreOffice Base (version 7.3) to create a database.
I need to extract year, month and day numbers from such strings that consist Chinese charactors as shown following. I think one Chinese charactor is one charactor but I am not sure.

What I have:
1997年8月12日
1971年10月1日
2001年5月26日
2005年12月29日
2010年2月8日
...

What I want to achieve:

  1. I need to extract the first 4 digits which are the years;
  2. The numbers between the first charactor (年) and the second charactor(月), which are months from 1 to 12 (1 or 2 digits);
  3. The numbers between the second charactor (月) and the third charactor (日),which are days in a month from 1 to 31 (1 or 2 digits);

I am a newbie, I tried using combination RIGHT() and LEFT() function or substring() method but I failed because of the fact the numbers of months and days are sometimes 1 digit sometimes 2 digits. I will be very grateful if someone can help. Thank you so much.

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

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

发布评论

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

评论(1

从﹋此江山别 2025-01-26 15:24:48

不幸的是,您没有指定您使用的内置数据库,Firebird或HSQLDB。

“数据库类型”

您没有提供表名和中文日期字段名称。因此,我将展示如何为火鸟数据库解决此问题,名为Chinatest的表和名为Chinadate的领域。

首先,用仪表板替换前两个字符,然后用空字符串替换最后一个字符(只需删除它)。这将由 repents>()函数>。只需嵌套三个替换()一个在另一个内部:

替换(替换(替换) ',')

现在 cast() 可以轻松将此字符串转换为“真实日期”:

选择cast(< prest; pestry_expression> aS date)“ real dealate'realldate'realldate'从“ chinatest”

当您拥有真实日期时,可以使用 extract()函数 获得任何部分它。

最终解决方案可能看起来像这样:

SELECT "RealDate", EXTRACT( YEAR FROM "RealDate" ) "iYear", 
                   EXTRACT( MONTH FROM "RealDate" ) "iMonth", 
                   EXTRACT( DAY FROM "RealDate" ) "iDay" 
    FROM (SELECT CAST( 
                   REPLACE( 
                      REPLACE( 
                         REPLACE( "ChinaTest"."ChinaDate", '年', '-' ), 
                      '月', '-' ), 
                   '日', '' ) 
                  AS DATE ) "RealDate" FROM "ChinaTest")

“

对于HSQLDB数据库,查询可能会如下:

SELECT
 CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) AS "BDate",
 YEAR( CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) ) AS "BDYear",
 MONTH( CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) ) AS "BDMonth",
 DAY( CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) ) AS "BDDay" 
FROM "Employess"

”

Unfortunately, you didn't specify which built-in database you are using, Firebird or HSQLDB.

Database type

You didn't provide your table name and Chinese date field name. Therefore, I will show how this problem is solved for the Firebird database, a table named ChinaTest and a field named ChinaDate.

First, replace the first two characters with a dash, and the last character with an empty string (just delete it). This will be done by the REPLACE() function. Just nest three REPLACE()'s one inside the other:

REPLACE( REPLACE( REPLACE( "ChinaTest"."ChinaDate", '年', '-' ), '月', '-' ), '日', '' )

Now the CAST() function easily converts this string into a "real date":

SELECT CAST( <previous_expression> AS DATE ) "RealDate" FROM "ChinaTest"

When you have a real date, you can use the EXTRACT() function to get any part of it.

The final solution might look like this:

SELECT "RealDate", EXTRACT( YEAR FROM "RealDate" ) "iYear", 
                   EXTRACT( MONTH FROM "RealDate" ) "iMonth", 
                   EXTRACT( DAY FROM "RealDate" ) "iDay" 
    FROM (SELECT CAST( 
                   REPLACE( 
                      REPLACE( 
                         REPLACE( "ChinaTest"."ChinaDate", '年', '-' ), 
                      '月', '-' ), 
                   '日', '' ) 
                  AS DATE ) "RealDate" FROM "ChinaTest")

Result

For a HSQLDB database, the query might look like this:

SELECT
 CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) AS "BDate",
 YEAR( CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) ) AS "BDYear",
 MONTH( CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) ) AS "BDMonth",
 DAY( CAST( REPLACE( REPLACE( REPLACE( "Employess"."CNBirthDate", '年', '-' ), '月', '-' ), '日', '' ) AS DATE ) ) AS "BDDay" 
FROM "Employess"

HSQLDB

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