我需要编写一个存储过程来允许某人搜索数据库.但是,我得到的只是月份和年份的整数.数据库有月份和年份字段.但我不知道如何设置比较.
I need to write a stored procedure to allow someone to search a db. However, all I get are ints for month and year. And the db has month and year fields. But I can't figure out how to set up the comparison.
例如:我得到 2008 年 3 月和 2010 年 6 月.
Ex: I get March 2008 and June 2010.
我需要在数据库中搜索日期(由月份和年份字段指定)在这两个日期之间的记录.
I need to searhc the database for records where the date, as specified by the month and year fields, are between thoese two dates.
编辑
给定两个 Date 输入,我如何找到落在这些日期之间的所有记录?每条记录只有代表年和月的整数.
Given two Date inputs, how do I find all records that fall between those dates? Each record only has integers representing year and month.
假设您在 SQL Server 中提供了名为 @StartDate 和 @EndDate 的日期变量:
Assuming you are provided Date variables called @StartDate and @EndDate in SQL Server:
SELECT
*
FROM
MyTable
WHERE
-- yields "200901 between 200801 and 201104" on inputs 01-01-2008, 04-01-2011
Convert(VarChar(10), MyTable.Year) + Replace(Str(MyTable.Month, 2), ' ', '0')
BETWEEN
Convert(VarChar(10), YEAR(@StartDate)) + Replace(Str(MONTH(@StartDate), 2), ' ', '0')
AND
Convert(VarChar(10), YEAR(@EndDate)) + Replace(Str(MONTH(@EndDate), 2), ' ', '0')
参考资料
Replace(Str(MyTable.Month, 2), ' ' , '0')Replace(Str(MyTable.Month, 2), ' ' , '0')这篇关于如何仅与月份和年份进行比较,而不是完整日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
将每个子标记转换为具有多个分隔符的单列-SQLConverting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(将每个子标记转换为具有多个分隔符的单列-SQ
如何从多个表创建视图?How can I create a view from more than one table?(如何从多个表创建视图?)
根据前一行内的计算值创建计算值Create calculated value based on calculated value inside previous row(根据前一行内的计算值创建计算值)
如何将表格的前两列堆叠成一列,但也仅将第三How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何将表格的前两列堆
递归 t-sql 查询Recursive t-sql query(递归 t-sql 查询)
将月份名称转换为日期/月份编号(问题和答案的组Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(将月份名称转换为日期/月份编号(问题和答案的组合