从字符串(Libreoffice Base)中提取年,月和一天的数量(1至2位)
我正在使用Libreoffice Base(版本7.3)来创建数据库。 如下所示,我需要从构成中国角色的字符串中提取年,每月和一天的数字。我认为一个中国魅力是一个宪章,但我不确定。
我有什么: 1997年8月12日 1971年10月1日 2001年5月26日 2005年12月29日 2010年2月8日 ...
我想实现的目标:
- 我需要提取前4位数字;
- 第一个charactor(年)和第二个charactor(月)之间的数字为1到12(1或2位);
- 第二个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:
- I need to extract the first 4 digits which are the years;
- The numbers between the first charactor (年) and the second charactor(月), which are months from 1 to 12 (1 or 2 digits);
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您没有指定您使用的内置数据库,Firebird或HSQLDB。
您没有提供表名和中文日期字段名称。因此,我将展示如何为火鸟数据库解决此问题,名为Chinatest的表和名为Chinadate的领域。
首先,用仪表板替换前两个字符,然后用空字符串替换最后一个字符(只需删除它)。这将由 repents>()函数>。只需嵌套三个替换()一个在另一个内部:
替换(替换(替换) ',')
现在 cast() 可以轻松将此字符串转换为“真实日期”:
选择cast(< prest; pestry_expression> aS date)“ real dealate'realldate'realldate'从“ chinatest”
当您拥有真实日期时,可以使用 extract()函数 获得任何部分它。
最终解决方案可能看起来像这样:
对于HSQLDB数据库,查询可能会如下:
Unfortunately, you didn't specify which built-in database you are using, Firebird or HSQLDB.
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:
For a HSQLDB database, the query might look like this: