简体   繁体   English

SQL 查询将 varchar 转换为日期时间

[英]SQL query converting varchar to datetime

SELECT distinct 
    (Client_Name),
    AMC_Expiry as date,
    Dealer_Name,
    Location,
    Product_Type, Product_Name,
    Quantity, 
    Install_Date, Warranty_Expiry, AMC_Expiry,
    mail_sent,
    billing_by, callhandled_by   
FROM 
     amc 
WHERE   
    (datediff(day, cast(convert(datetime, AMC_Expiry, 103) as datetime),  
                   cast(convert(datetime, GETDATE(), 103) as datetime)) > '-30'   
    and datediff(day, cast(convert(datetime, AMC_Expiry, 103) as datetime),
                      cast(convert(datetime, GETDATE(), 103) as datetime))  < 1)     
    and mail_sent = 'N'   
    or (datediff(day, cast(convert(datetime, Warranty_Expiry, 103) as datetime),
                      cast(convert(datetime, GETDATE(), 103) as datetime)) > '-30'
    and datediff(day, cast(convert(datetime, Warranty_Expiry, 103) as datetime),
                      cast(convert(datetime, GETDATE(), 103) as datetime))  < 1) 
    and mail_sent = 'N'

First, you seem to misunderstand select distinct .首先,您似乎误解了select distinct DISTINCT is not a function. DISTINCT不是函数。 It is part of a clause that applies to all columns.它是适用于所有列的子句的一部分。

Second, you are doing absurd things like:其次,你在做一些荒谬的事情,比如:

cast(convert(datetime,GETDATE(),103) as datetime))

Why would you be taking a datetime value, converting it to a datetime and then re-convert it to a datetime .为什么要获取datetime值,将其转换为datetime ,然后将其重新转换为datetime

In addition, you have a comma in the WHERE clause, which makes no sense.此外,您在WHERE子句中有一个逗号,这是没有意义的。 And, you have integer constants in single quotes.而且,您有单引号中的整数常量。

Your query has more problems than the one you mention.您的查询比您提到的问题更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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