如何只查询sd日期的dd / MM部分

时间:2018-02-26 16:15:39

标签: sql database

您好我想从sql数据库查询一些生日数据。我只想找到一个匹配dd / MM的记录,我该怎么做? DOB的格式为:YYYY-MM-dd 00:00:00.000

 sql = "SELECT * 
        FROM [myDb].[dbo].[TblPeople] 
        WHERE txtLeavingDate IS NULL 
          AND intNCYear Between'7' AND '13' 
          AND txtDOB like'07/27'";

3 个答案:

答案 0 :(得分:1)

假设您使用的是SQL Server,则可以使用DATEPART

select *
from [myDb].[dbo].[TblPeople]
where txtLeavingDate is null
    and intNCYear between 7 and 13
    and datepart(day, txtDOB) = 27
    and datepart(month, txtDOB) = 07
;

答案 1 :(得分:1)

使用year()和month()`函数很简单。更重要的是,您需要注意单引号的使用。不要在数字上使用单引号,仅用于日期和字符串常量:

SELECT * 
FROM [myDb].[dbo].[TblPeople] 
WHERE txtLeavingDate IS NULL AND
       intNCYear Between 7 AND 13 AND
       month(txtDOB) = 7 AND
       day(txtDOB) = 27;

答案 2 :(得分:0)

  select *from [myDb].[dbo].[TblPeople]where txtLeavingDate is null and intNCYear between 7 and 13
and datepart(day, txtDOB) = 27
and datepart(month, txtDOB) = 07

both the above answers were correct, thanks for the advice!