我如何按月或按周搜索数据 [英] How I Search Data By Month Or Week

查看:135
本文介绍了我如何按月或按周搜索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经存储了员工工作加入记录,并且还将加入日期(dd / mm / yyyy)存储在此处。

现在我想通过加入月和周来搜索员工。

我怎么做?

解决方案

查看这个例子

  DECLARE   @ Date  Datetime = getdate()
SELECT
@ Date Supplied_Date
,month( @ Date )Month_Number
,datename(月, @ Date )Month_Name
,datepart(周, @ Date )Week_Of_Year
,datediff(周,dateadd(周,datediff(周, 0 ,dateadd(月,datediff(月, 0 @ date ), 0 )), 0 ), @ date - 1 )+ 1 Week_Of_Month



快乐编码!

:)


假设有'joindate'和'表中名为'employee'的empname'。要查找在12月或一年的第24周加入的员工,请尝试以下方法:

 SELECT empname FROM employee WHERE DATEPART(MONTH,joindate)='12'OR DATEPART(WEEK,joindate)='24'



如果你想同时满足这两个条件,你应该将'OR'改为'AND'。 / BLOCKQUOTE>

i have stored employee job joining records,and also stored joining date(dd/mm/yyyy) in table here.
now i want search employee by joining months and week.
how i do this?

解决方案

check this example

DECLARE @Date Datetime= getdate()
SELECT 
  @Date Supplied_Date 
, month(@Date) Month_Number
, datename(month,@Date) Month_Name
, datepart(week,@Date) Week_Of_Year
, datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, @date), 0)), 0), @date - 1) + 1 Week_Of_Month


Happy Coding!
:)


Assuming there are a 'joindate' and 'empname' in the table called 'employee'. To find employees that join either in December or on the 24th week of the year, try this:

SELECT empname FROM employee WHERE DATEPART(MONTH, joindate) = '12' OR DATEPART(WEEK, joindate) = '24'


You should change the 'OR' to 'AND' if you want both conditions to be met at the same time.


这篇关于我如何按月或按周搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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