SQL 查询帮助:以非平凡的方式转换日期
我有一个带有“日期”列的表,我想做一个执行以下操作的查询:
如果日期是星期一、星期二、星期三或星期四,显示的日期应向上移动 1 天,如
DATEADD(day, 1, [Date])On the other hand, if it is a Friday, the displayed date should be incremented by 3 days (i.e. so it becomes the following Monday).
如何在 SELECT 语句中执行此操作? 如
SELECT somewayofdoingthis([Date]) FROM myTable
(这是 SQL Server 2000。)
I have a table with a "Date" column, and I would like to do a query that does the following:
If the date is a Monday, Tuesday, Wednesday, or Thursday, the displayed date should be shifted up by 1 day, as in
DATEADD(day, 1, [Date])
On the other hand, if it is a Friday, the displayed date should be incremented by 3 days (i.e. so it becomes the following Monday).
How do I do this in my SELECT statement? As in,
SELECT somewayofdoingthis([Date]) FROM myTable
(This is SQL Server 2000.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
从数据仓库人员那里获取页面并制作一个表格怎么样? 在 DW 术语中,这将是一个日期维度。 标准日期维度 会有各种日期名称(“MON”、“Monday”) 、“1998 年 8 月 22 日”),或月末和月初等指标。 但是,您也可以拥有仅在您的环境中有意义的列。
例如,根据问题,您的下一个工作日列可能会指向相关日期的关键。 这样您就可以进一步自定义它以考虑假期或其他非工作日。
DW 人员坚持使用无意义的密钥(即,不要只使用截断的日期作为密钥,而是使用生成的密钥),但您可以自己决定。
日期维度工具包具有在各种 DBMS 中生成您自己的表的代码,并且它具有多个 CSV 数据多年的约会。
How about taking a page from the Data Warehouse guys and make a table. In DW terms, this would be a date dimension. A standard date dimension would have things like various names for a date ("MON", "Monday", "August 22, 1998"), or indicators like end-of-month and start-of-month. However, you can also have columns that only make sense in your environment.
For instance, based on the question, yours might have a next-work-day column that would point to the key for the day in question. That way you can customize it further to take into account holidays or other non-working days.
The DW folks are adamant about using meaningless keys (that is, don't just use a truncated date as the key, use a generated key), but you can decide that for yourself.
The Date Dimension Toolkit has code to generate your own tables in various DBMS and it has CSV data for several years worth of dates.
您需要创建一个 SQL 函数来为您执行此转换。
you need to create a SQL Function that does this transformation for you.
这与 Brian 的大部分类似,只是由于括号不匹配而无法编译,并且我将 IF 更改为不包含选择。 需要注意的是,我们在这里使用 DateNAME 而不是 datePART,因为 datePART 取决于 SET DATEFIRST 设置的值,该值设置一周的第一天。
This is mostly like Brian's except it didn't compile due to mismatched parens and I changed the IF to not have the select in it. It is important to note that we use DateNAME here rather than datePART because datePART is dependent on the value set by SET DATEFIRST, which sets the first day of the week.
这是我的想法,可以清楚地清理,但可以将其作为起点:
This is off the top of my head and can be clearly cleaned up but use it as a starting point:
查找 CASE 语句和 DATEPART 语句。 您将需要将 dw 参数与 DATEPART 一起使用来获取表示星期几的整数。
Look up the CASE statement and the DATEPART statement. You will want to use the dw argument with DATEPART to get back an integer that represents the day of week.
你可以用这个:
you could use this:
听起来像 CASE 表达式。 我不知道 SQL Server 的正确数据操作,但基本上它看起来像这样:
如果您想检查周末,您可以在 ELSE 之前添加额外的 WHEN 子句。
Sounds like a CASE expression. I don't know the proper data manipulations for SQL Server, but basically it would look like this:
If you wanted to check for weekend days you could add additional WHEN clauses before the ELSE.
我假设您还希望将周六和周日提前到下周一。 如果不是这种情况,请从 (1,2,3,4,5) 中取出 1,并删除最后一个 when 子句。
您也可以在一行中完成:
I'm assuming that you also want Saturday and Sunday to shift forward to the following Monday. If that is not the case, take the 1 out of (1,2,3,4,5) and remove the last when clause.
You can also do it in one line:
尝试
Try
我将这样做。 如果您将在其他地方使用它,我确实推荐像上面这样的功能。
Here is how I would do it. I do recommend a function like above if you will be using this in other places.