将整数转换为工作日列表
我在数据库中存储了一个整数(SQLAgent 频率间隔) 该整数实际上是计划运行的一周中选定天数的总和 可能的值是这些值的任意组合
- Sunday =1
- Monday = 2
- Tuesday =4
- Wednesday= 8
- Tuesday = 16
- Friday = 32
- Saturday =64
ex 65 表示计划应在周六和周日运行
我的问题是我需要表示当给定 65 时,这些值作为文本“星期六”和“星期日”,我试图在 SQL 中执行此操作,
除了包含所有可能组合的巨大 CASE 语句之外,任何人都可以想到一种方法来执行此操作吗?
谢谢
I have an integer stored in a database (SQLAgent Frequency Interval) This integer is actually the sum of the selected days of the week that the schedule is to run
The possible values are any combination of these values
- Sunday =1
- Monday = 2
- Tuesday =4
- Wednesday= 8
- Thursday = 16
- Friday = 32
- Saturday =64
ex 65 means that the schedule should run on Saturday and Sunday
My problem is that I need to represent these values as the text "Saturday" and "Sunday" when given 65 and I am trying to do this in SQL
Other than a huge CASE statement with all of the possible combinations can anyone think of a way to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 T-SQL 中使用按位运算符。 方法如下:
例如,这将产生“Sunday Saturday”。
You can use bit-wise operators in T-SQL. Here's how:
That will produce "Sunday Saturday" for example.
我首先将它放在用户定义的函数上。
另外,您可以使用在位级别操作的 and 来检查它 - 我认为它是 &,将会更新。
更新1:是&,杰森已经举了一个例子。 我仍然建议使用用户定义的函数:)。
I would start by putting it on an user defined function.
Also, you can use an and that operates at the bit level to check for it - I think it is &, will update.
Update 1: It was &, Jason already put up an example. I still recommend to use an user defined function for it :).
编辑:这是执行位运算的 C# 代码。 我在详细阅读问题之前发布了它,但我将把它留在这里作为替代方案。 数据库真的是执行此操作的最佳位置吗?
您可以使用数组:
结果是一个字符串列表,如果您想合并它们,您可以这样做:
Edit: This is C# code to do the bit operations. I posted it before reading the question in detail, but I will leave it here as an alternative. Is the database really the best place to do this...?
You can use an array:
The result is a list of strings, if you want to merge them you can for example do: