PostgreSQL 8.1 CASE 转字符串
我有一个数据库,它使用一串 0 和 1 来表示一个人的工作时间表中的日期。例如,0111110 代表“否”星期日,“是”星期一至星期五,“否”星期六。我想使用 SQL 将其从数据库中取出,最后得到一个看起来像“星期一、星期二、星期三、星期四、星期五”的字符串。 这就是我所得到的……
CASE
WHEN SUBSTR(regular_work_days, 1,1)='1' THEN 'Sunday'
WHEN SUBSTR(regular_work_days, 2,1)='1' THEN 'Monday'
WHEN SUBSTR(regular_work_days, 3,1)='1' THEN 'Tuesday'
WHEN SUBSTR(regular_work_days, 4,1)='1' THEN 'Wednesday'
WHEN SUBSTR(regular_work_days, 5,1)='1' THEN 'Thursday'
WHEN SUBSTR(regular_work_days, 6,1)='1' THEN 'Friday'
WHEN SUBSTR(regular_work_days, 7,1)='1' THEN 'Saturday'
ELSE ' '
END AS "Regular Work Days",
可以预见的是,我最终得到了第一个“1”,而没有其他日子。我尝试添加||但意识到我不知道如何让它发挥作用。如果逗号可以简化事情,那么就没有必要。 请帮忙?
I have a database that uses a string of 0's and 1's to represent the days in a persons work schedule. For example 0111110 would represent NO sunday, YES monday - friday, NO saturday. I would like to pull that out of the database using SQL and end up with a string that looks like 'Monday, Tuesday, Wednesday, Thursday, Friday'.
This is a far as I've gotten....
CASE
WHEN SUBSTR(regular_work_days, 1,1)='1' THEN 'Sunday'
WHEN SUBSTR(regular_work_days, 2,1)='1' THEN 'Monday'
WHEN SUBSTR(regular_work_days, 3,1)='1' THEN 'Tuesday'
WHEN SUBSTR(regular_work_days, 4,1)='1' THEN 'Wednesday'
WHEN SUBSTR(regular_work_days, 5,1)='1' THEN 'Thursday'
WHEN SUBSTR(regular_work_days, 6,1)='1' THEN 'Friday'
WHEN SUBSTR(regular_work_days, 7,1)='1' THEN 'Saturday'
ELSE ' '
END AS "Regular Work Days",
Predictably I end up with the first '1', and no other days. I've tried adding || but realized I have no idea how to get this to work. The comma isn't necessary, if that simplifies things.
Help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打包的日期字符串只有 128 个可能的值(七个槽位,每个槽位有两个可能的值意味着 128 种可能性),因此只需生成一个涵盖所有选项的表并连接到该表即可获取未打包的字符串。您应该能够非常轻松地生成这样的表格:
然后您可以将打包日列加入到
packed
上的此表格中,并选择unpacked
来获取人类友好的字符串。请记住,有限域上的函数是关联表,如果域很小,那么您可以很容易地将函数实现为表。如果你必须以困难的方式做到这一点,那么这将在 8.1 中工作,但它相当可怕,并且应该让你相信(1)你不应该像那样存储你的日子,(b)你应该处理这种格式数据库之外。在现实生活中我不会做这样的事情,我只是将其包括在内,因为我想看看是否可以想出一些可以在受限的 8.1 环境中工作的东西。您可能也想尽快升级,8.1 已经过时很久了,不再受支持。
首先,您需要一个要连接的日期名称表:
然后是一个自定义聚合,用于将用逗号分隔的字符串连接在一起:
最后,一个用于隐藏所有丑陋之处的解包函数:
现在您可以这样说:
You only have 128 possible values for your packed day strings (seven slots and two possible values for each slot means 128 possibilities) so just generate a table that covers all the options and join to that table to get your unpacked string. You should be able to generate a table like this pretty easily:
Then you can join your packed day column to this table on
packed
and selectunpacked
to get the human friendly string. Keep in mind that a function over a finite domain is an association table and if the domain is small then you can quite easily implement the function as a table.If you must do it the hard way, then this will work in 8.1 but it is rather horrendous and should convince you that (1) you shouldn't be storing your days like that and (b) you should be handling this sort of formatting outside the database. I wouldn't do anything like this in real life, I'm only including it because I wanted to see if I could come up with something that would work in the constrained 8.1 environment. You might want to upgrade ASAP too, 8.1 is pretty long in the tooth and no longer supported.
First you want a table of day names to join against:
Then a custom aggregate for joining strings together separated by commas:
And finally, an unpacking function to hide the all the ugliness:
Now you can say this:
这应该有效。也许有一种更简单的方法但是......
这给出了:
This should work. Perhaps there is an easier way but...
This gives: