将整数转换为工作日列表 [英] Convert integer to a list of week days

查看:38
本文介绍了将整数转换为工作日列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中存储了一个整数(SQLAgent 频率间隔)这个整数实际上是计划要运行的一周中选定天数的总和可能的值是这些值的任意组合

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

  • 星期日 =1
  • 星期一 = 2
  • 星期二 =4
  • 星期三= 8
  • 星期四 = 16
  • 星期五 = 32
  • 星期六 =64

ex 65 表示计划应该在周六和周日运行

ex 65 means that the schedule should run on Saturday and Sunday

我的问题是,当给定 65 时,我需要将这些值表示为文本星期六"和星期日",并且我正在尝试在 SQL 中执行此操作

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

除了包含所有可能组合的巨大 CASE 语句之外,还有人能想到一种方法来做到这一点吗?

Other than a huge CASE statement with all of the possible combinations can anyone think of a way to do this?

谢谢

推荐答案

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

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.

这篇关于将整数转换为工作日列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆