SQL 服务器函数获取特定日期和月份而不考虑年份

SQL Server function to a get particular date and month regardless year

我正在尝试编写 if 块语句。

条件是我想在用户输入特定日期(如 2 月 14 日情人节)时执行特定操作。但年份应该无关紧要。我不希望条件受年份限制。

例如

 @UserDate SMALLDATETIME

IF(@UserDate='2015/02/14')
Begin
    Some expression
End
Else
    Some other expression

现在,由于您终于添加了 TSQL 标记,我可以做一些小事 search

Microsoft 已经对这些内容进行了详细的记录 well,所以这里只是摘录。您可能想要编写一个 存储过程 来执行您的条件操作并 INSERT 生成结果数据:

Syntax

IF Boolean_expression 
     { sql_statement | statement_block } 
[ ELSE 
     { sql_statement | statement_block } ] 

Arguments

Boolean_expression

Is an expression that returns `TRUE` or `FALSE`. If the Boolean expression contains a `SELECT` statement, the `SELECT` statement must

be enclosed in parentheses.

{ sql_statement| statement_block }

Is any Transact-SQL statement or statement grouping as defined by using a statement block. Unless a statement block is used, the IF or ELSE condition can affect the performance of only one Transact-SQL statement.

To define a statement block, use the control-of-flow keywords `BEGIN` and `END`.

编辑: 做另一个非常复杂的事情 google search 我发现 DATEPART 非常符合你的问题。

可以利用DATEPART函数提取情人节对应的月日:

DECLARE @UserDate SMALLDATETIME
IF(DatePart(mm, @UserDate) = 2 AND DatePart(dd, @UserDate) = 14)
BEGIN
    /* some expression */
END