0

テーブルのLeaveFromフィールドとLeaveToフィールドに、葉の日時を保存しています。

LeaveFrom =05/26/201212:00:00およびLeaveTo=06/30/2012..など

フロントエンドから私は月と年だけを通過しています。それで、同じものの検索条件はどうなるでしょう。

月と年の検索基準が必要です。特定の月に休暇を申請した/または欠席した人の数。

皆さんのおかげで..ほぼ全員が期待に近かった...ありがとう、私が必要としているのは「ヒント」でした..皆さんの皆さんにたくさん感謝します..

4

7 に答える 7

1

これを試して:

where   (MONTH(LeaveFrom) = @Month 
        and YEAR(LeaveFrom) = @Year )
         OR
        (and MONTH(LeaveTo) = @Month 
        and YEAR(LeaveTo) = @Year )
于 2012-08-01T10:31:31.897 に答える
1

特定の月/年に休暇中の一連の人々を取得したいと仮定します。

一部のユーザーが役立つと思われる更新された完全なコードを次に示します。

declare @dateFrom datetime  
declare @dateTo datetime 
set @dateFrom = DateAdd(day, 0, DateAdd(month, @Month - 1, 
                DateAdd(Year, @Year-1900, 0)))
set @dateTo = DATEADD("month", 1, @dateFrom)
SELECT * FROM @yourtable WHERE LeaveFrom < @dateTo AND LeaveTo >= @dateFrom

選択した行ごとに日付を計算するのではなく、日付を一度だけ計算するようにしています。上記のコードも、単一の SQL ステートメントに簡単に変換できます。読みやすいように、このように書いています。

于 2012-08-01T10:29:11.233 に答える
0

私が知る限り、LeaveFromとLeaveToの間で選択した月と重複するすべての行が必要です。ドキュメントとしていくつかのサンプル日付を提供しました。

declare @yourtable table(LeaveFrom datetime, LeaveTo datetime)
insert @yourtable values('2012-05-26 12:00:00', '2012-06-30')
insert @yourtable values('2012-01-26 12:00:00', '2012-12-30')
insert @yourtable values('2012-01-26 12:00:00', '2012-04-30')
insert @yourtable values('2012-05-26 12:00:00', '2012-12-30')
insert @yourtable values('2012-01-26 12:00:00', '2012-05-30')
insert @yourtable values('2012-06-26 12:00:00', '2012-12-30')
insert @yourtable values('2011-01-01 12:00:00', '2011-01-01')

declare @month tinyint = 5
declare @year int = 2012

-- calculate a data corresponding to month and year (2012-05-01)
declare @date datetime= dateadd(month, (@year-1900) * 12 + @month-1, 0)

select * from @yourtable
where datediff(month , LeaveFrom, @date) between 0 and datediff(month , LeaveFrom, LeaveTo)

編集:同じ結果を持つ別の可能な方法:

select * from @yourtable
where @date between dateadd(month, datediff(month, 0, LeaveFrom), 0) 
and dateadd(month, datediff(month, 0, LeaveTo), 0) 

結果:

LeaveFrom               LeaveTo
----------------------- -----------------------
2012-05-26 12:00:00.000 2012-06-30 00:00:00.000
2012-01-26 12:00:00.000 2012-12-30 00:00:00.000
2012-05-26 12:00:00.000 2012-12-30 00:00:00.000
2012-01-26 12:00:00.000 2012-05-30 00:00:00.000
于 2012-08-01T11:19:46.993 に答える
0

これを試して。パフォーマンス的には、これが最適です

declare @month int, @year int
select @month=6, @year=2012

select columns from table
where 
LeaveFrom>=Dateadd(month,(@year-1900)*12+@month-1,0) and
LeaveTo<Dateadd(month,(@year-1900)*12+@month,0)

また、さまざまな方法で dateserial 関数をシミュレートする方法を知る必要がある場合もあります http://beyondrelational.com/modules/2/blogs/70/posts/15788/10-ways-to-simulate-dateserial-function.aspx

于 2012-08-01T11:31:14.967 に答える
0
DECLARE @month int
DECLARE @year int
SET @month = 7
SET @year = 2012

SELECT * FROM table
WHERE 
(DATEPART(yy, LeaveFrom) = @year
AND DATEPART(mm, LeaveFrom) = @month) OR 
(DATEPART(mm, LeaveTo) = @month AND DATEPART(yy, LeaveTo) = @Year)
于 2012-08-01T10:39:12.783 に答える
0

DECLARE @Temp TABLE ( ID INT IDENTITY(1, 1) ,LeaveFrom DATETIME ,LeaveTo DATETIME ,UserName VARCHAR(100) )

INSERT INTO @Temp VALUES ( '2015-01-01' ,'2015-01-05' ,'Ramesh' ) ,( '2015-01-07' ,'2015-01-09' ,'Suresh' ) ,( '2015-01-03' ,'2015-01-06' ,'Dinesh' ) ,( '2015-01-15' ,'2015-01-25' ,'Kamlesh' ) ,( '2015-01-12 ' ,'2015-01-17' ,'モハン' )

SELECT COUNT(*) FROM @Temp WHERE '2015' BETWEEN YEAR(LeaveFrom) AND YEAR(LeaveTo) AND '1' BETWEEN MONTH(LeaveFrom) AND MONTH(LeaveTo)

于 2015-01-21T13:01:48.820 に答える
0
SELECT * FROM {your table name}
WHERE LeaveFrom >= CAST({input month} + '/1/' + {input year} AS DATETIME) AND
    LeaveTo <= DATEADD(dd, -1, DATEADD(mm, 1, CAST({input month} + '/1/' + {input year} AS DATETIME)))

さらに正確に言うと、1 日ではなく 1 分を差し引くこともできます。

SELECT * FROM {your table name}
WHERE LeaveFrom >= CAST({input month} + '/1/' + {input year} AS DATETIME) AND
    LeaveTo < DATEADD(mm, 1, CAST({input month} + '/1/' + {input year} AS DATETIME))

あなたはあなたが望むものについて皆を混乱させ始めていますが、あなたの最後のコメントに基づいて、ここにクエリがあります.

SELECT * FROM {your table name}
WHERE MONTH(LeaveFrom) = {input month} AND YEAR(LeaveFrom) = {input year}
于 2012-08-01T10:29:52.983 に答える