简体   繁体   中英

Convert a date in nvarchar to date format in a table on the SQL Server

Here is what I have done:

select top 1000 convert(nvarchar(50), cast([date written] as datetime), 1) 
from practicetable
where convert(nvarchar(50), cast([date written] as datetime), 1) = '09/07/16'

Although this query works on a small scale anytime I try to select more than 1000 records the query fails and I get this error:

Conversion failed when converting datetime from character string

Why can't I select more than 1,000 records?

Always risky to save dates as a string. Since you don't have Try_Convert() in 2008, I would recommend that you

select distinct [date written] from practicetable order by 1

to see which dates are not formatted correctly, and scrub as necessary

The query is failing because it is trying to convert a string to a date, but the string does not fit the date format. In other words, the 'Conversion failed when converting datetime from character string'. You should take a closer look at the underlying data to determine which record is causing the issue.

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