简体   繁体   中英

SQL - Greater than date query not working

I am using the SQL query below and I would like to get only the records where the column "Last Update Date" is greater than or equal to 01/01/2016. Basically only records in 2016. When I use the query below, I don't get any errors but I am still getting every date returned not just 2016. Is there something wrong with my syntax?

SELECT
  NPI,
  [Entity Type Code],
  [Replacement NPI],
  [Employer Identification Number (EIN)],
  [Provider Organization Name (Legal Business Name)],
  [Provider Last Name (Legal Name)],
  [Provider First Name],
  [Provider Middle Name],
  [Provider Name Prefix Text],
  [Provider Name Suffix Text],
  [Provider Credential Text],
  [Provider Other Organization Name],
  [Provider Other Organization Name Type Code],
  [Provider Other Last Name],
  [Provider Other First Name],
  [Provider Other Middle Name],
  [Provider Other Name Prefix Text],
  [Provider Other Name Suffix Text],
  [Provider Other Credential Text],
  [Provider Other Last Name Type Code],
  [Provider First Line Business Mailing Address],
  [Provider Second Line Business Mailing Address],
  [Provider Business Mailing Address City Name],
  [Provider Business Mailing Address State Name],
  [Provider Business Mailing Address Postal Code],
  [Provider Business Mailing Address Country Code (If outside U S )],
  [Provider Business Mailing Address Telephone Number],
  [Provider Business Mailing Address Fax Number],
  [Provider First Line Business Practice Location Address],
  [Provider Second Line Business Practice Location Address],
  [Provider Business Practice Location Address City Name],
  [Provider Business Practice Location Address State Name],
  [Provider Business Practice Location Address Postal Code],
  [Provider Business Practice Location Address Country Code (If outside U S )],
  [Provider Business Practice Location Address Telephone Number],
  [Provider Business Practice Location Address Fax Number],
  [Provider Enumeration Date],
  [Last Update Date]
FROM Data
WHERE ([Healthcare Provider Taxonomy Code_1] IN ('122300000X', '1223G0001X'))
AND ([Last Update Date] >= '01/01/2016')

Use instead of ([Last Update Date] >= '01/01/2016') the following:

YEAR( [Last Update Date]) = 2016

to get all the records that updated in 2016

在以下情况下使用Datetime:

and CONVERT(datetime, [Last Update Date], 121) >= CONVERT(datetime,'01/01/2016',121)

Use this:

WHERE CONVERT(datetime, [Last Update Date], 101) >= '2016-01-01'

Note that the format mask you want is 101 , which corresponds to data in the format mm/dd/yyyy (see documentation ). This assumes that you are storing your dates in this format, telling by the format you used in your original WHERE clause while comparing.

Also note that you don't need to use CONVERT on the RHS of the comparsion in the above expression, because SQL Server should be able to deal with a date literal assuming it is in a standard format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM