一周中的几天存储在一个字段中

编程入门 行业动态 更新时间:2024-10-24 06:24:17
本文介绍了一周中的几天存储在一个字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

关于特定整数字段存储数据的方式,我遇到了一些障碍。

I've encountered a bit of a mental roadblock regarding the way a specific integer field is storing data.

具体来说,有一列包含整数的范围是1-127;每个整数代表一周中不同天的组合。例如:星期一= 2 ^ 0或1,星期二= 2 ^ 2或2,星期三= 2 ^ 3或8;可以选择加法,星期一+星期二= 3。

Specifically, there is a column with integers that range from 1 - 127; each integer represents a combination of different days of the week. For example: Monday = 2^0 or 1, Tuesday = 2^2 or 2, Wednesday = 2^3 or 8; with the option of addition, Monday + Tuesday = 3.

我已经可以使用此处。但是,当两天加在一起时(例如,星期一+星期二= 3),该特定示例将不起作用。谁能指出我正确的方向?

I've been able to extract the date values partially using the example found here. However, that particular example does not work when two days get added together (eg. Monday + Tuesday = 3). Can anyone point me in the right direction?

仅供参考,我正在使用SQL Server 2008 R2。我很抱歉,如果以前已经发布过,我看了一下,但是找不到其他发布。

FYI, I am using SQL Server 2008 R2. My apologies if this has been posted before, I took a look but was unable to find any other postings.

推荐答案

这里是一个很好地阅读,上面有简单的例子。

Here's a good read on it with clear simple examples.

为了完整起见,这是您在一周中的每一天查看的内容。

For the sake of completeness, here is what you're looking at broken down into columns for each day of the week.

DECLARE @bitwise TABLE (someValue TINYINT) INSERT INTO @bitwise (someValue) SELECT 1 UNION SELECT 5 UNION SELECT 127 SELECT someValue, CASE WHEN (1&someValue)=1 THEN 'SUNDAY' END , CASE WHEN (2&someValue)=2 THEN 'MONDAY' END , CASE WHEN (4&someValue)=4 THEN 'TUESDAY' END , CASE WHEN (8&someValue)=8 THEN 'WEDNESDAY' END , CASE WHEN (16&someValue)=16 THEN 'THURSDAY' END , CASE WHEN (32&someValue)=32 THEN 'FRIDAY' END , CASE WHEN (64&someValue)=64 THEN 'SATURDAY' END FROM @bitwise

更多推荐

一周中的几天存储在一个字段中

本文发布于:2023-11-27 05:13:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几天   字段

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!