将整数转换为工作日列表

发布于 2024-07-14 14:45:31 字数 397 浏览 8 评论 0原文

我在数据库中存储了一个整数(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 技术交流群。

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

发布评论

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

评论(4

榆西 2024-07-21 14:45:31

您可以在 T-SQL 中使用按位运算符。 方法如下:

SELECT
  ( CASE WHEN daybits & 1 = 1 THEN 'Sunday ' ELSE '' END ) +
  ( CASE WHEN daybits & 2 = 2 THEN 'Monday ' ELSE '' END ) +
  ( CASE WHEN daybits & 4 = 4 THEN 'Tuesday ' ELSE '' END ) +
  ...
  ( CASE WHEN daybits & 64 = 64 THEN 'Saturday ' ELSE '' END ) +

例如,这将产生“Sunday Saturday”。

You can use bit-wise operators in T-SQL. Here's how:

SELECT
  ( CASE WHEN daybits & 1 = 1 THEN 'Sunday ' ELSE '' END ) +
  ( CASE WHEN daybits & 2 = 2 THEN 'Monday ' ELSE '' END ) +
  ( CASE WHEN daybits & 4 = 4 THEN 'Tuesday ' ELSE '' END ) +
  ...
  ( CASE WHEN daybits & 64 = 64 THEN 'Saturday ' ELSE '' END ) +

That will produce "Sunday Saturday" for example.

可爱咩 2024-07-21 14:45:31

我首先将它放在用户定义的函数上。
另外,您可以使用在位级别操作的 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 :).

踏雪无痕 2024-07-21 14:45:31

编辑:这是执行位运算的 C# 代码。 我在详细阅读问题之前发布了它,但我将把它留在这里作为替代方案。 数据库真的是执行此操作的最佳位置吗?

您可以使用数组:

// input: int value
string[] weekdays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday" };
int flag = 1
List<string> days = new List<string>();
foreach (string day in days) {
   if ((value && flag) != 0) {
      days.Add(day);
   }
   flag <<= 1;
}

结果是一个字符串列表,如果您想合并它们,您可以这样做:

string selected = String.Join(", ", days.ToArray());

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:

// input: int value
string[] weekdays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday" };
int flag = 1
List<string> days = new List<string>();
foreach (string day in days) {
   if ((value && flag) != 0) {
      days.Add(day);
   }
   flag <<= 1;
}

The result is a list of strings, if you want to merge them you can for example do:

string selected = String.Join(", ", days.ToArray());
醉酒的小男人 2024-07-21 14:45:31
DECLARE @in INTEGER;
SET @in = 63;
WITH series(n) AS
    (
    SELECT  0
    UNION ALL
    SELECT  n + 1
    FROM    series
    WHERE   n < 6
    )
SELECT  CASE WHEN ROW_NUMBER() OVER (ORDER BY n) > 1 THEN ', ' ELSE '' END + DATENAME(weekday, DATEADD(day, n, '1980-09-03')) 
FROM    series
WHERE   (POWER(2, n) & @in) > 0
ORDER BY n
FOR XML PATH ('')
DECLARE @in INTEGER;
SET @in = 63;
WITH series(n) AS
    (
    SELECT  0
    UNION ALL
    SELECT  n + 1
    FROM    series
    WHERE   n < 6
    )
SELECT  CASE WHEN ROW_NUMBER() OVER (ORDER BY n) > 1 THEN ', ' ELSE '' END + DATENAME(weekday, DATEADD(day, n, '1980-09-03')) 
FROM    series
WHERE   (POWER(2, n) & @in) > 0
ORDER BY n
FOR XML PATH ('')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文