简体   繁体   English

如何将文本转换为在SQL中输入为日期的日期?

[英]How to convert text to date entered as a date in sql?

Data type is TEXT and entered as '20/11/2017' and when using MAX or MIN it ignores the month. 数据类型为TEXT,输入为'20 / 11/2017',使用MAX或MIN时会忽略月份。 I am trying to convert it into a date format for month to be considered as well. 我正在尝试将其转换为日期格式,以考虑月份。

CAST AND CONVERT do not seem to work as the following error returns 由于出现以下错误,CAST AND CONVERT似乎不起作用

Msg 241, Level 16, State 1, Line 13 消息241,第16层,状态1,第13行
Conversion failed when converting date and/or time from character string. 从字符串转换日期和/或时间时转换失败。

Code: 码:

SELECT     
    user_id, 
    record_id
    --CAST(onSale AS date) AS onSale
    --CONVERT(DATE, onSale) AS onSale, 
    --CONVERT(DATE, OffSale) AS OffSale
FROM (SELECT  user_id, 
             record_id,
             (SELECT MAX(value) AS Expr1
             FROM      UPLOADS.LINES AS SUH WITH (NoLock, ReadUncommitted)
             WHERE  (field_id = 4782) AND (record_id = UR.record_id)) AS onSale,
             (SELECT MAX(value) AS Expr1
             FROM      UPLOADS.LINES AS SUH WITH (NoLock, ReadUncommitted)
             WHERE  (field_id = 4783) AND (record_id = UR.record_id)) AS OffSale
        FROM   UPLOADS.RECORDS AS UR WITH (NoLock, ReadUncommitted)
        WHERE      (module_id = 18)) AS DATA;

The end result would essentially be the MAX or MIN date with all three components being date,month and year. 最终结果本质上将是MAX或MIN日期,而所有三个组成部分均为日期,月份和年份。 So if the user has entered two dates being 17/05/2018 and 17/04/2018 then the first should be shown if MAX is used. 因此,如果用户输入了两个日期,即17/05/2018和17/04/2018,则如果使用MAX,则应显示第一个日期。

You can use a format code when using CONVERT, and you can even use TRY_CONVERT to prevent errors from invalid dates. 使用CONVERT时,可以使用格式代码,甚至可以使用TRY_CONVERT来防止来自无效日期的错误。 I also improved your code to make it simpler and more efficient. 我还改进了您的代码,使其更简单,更高效。

SELECT  [user_id], 
        record_id,
        MAX(CASE WHEN SUH.field_id = 4782 THEN TRY_CONVERT( DATE, SUH.value, 103) END) AS onSale,
        MAX(CASE WHEN SUH.field_id = 4783 THEN TRY_CONVERT( DATE, SUH.value, 103) END) AS OffSale
FROM  UPLOADS.RECORDS AS UR 
JOIN  UPLOADS.LINES AS SUH ON SUH.record_id = UR.record_id
WHERE  module_id = 18
GROUP BY [user_id], 
        record_id;

This is a slight improvement on Luis's answer in terms of the SQL: 这在Luis的SQL回答上有一点改进:

SELECT ur.[user_id], ur.record_id,
       MAX(CASE WHEN SUH.field_id = 4782 THEN TRY_CONVERT( DATE, SUH.value, 103) END) AS onSale,
       MAX(CASE WHEN SUH.field_id = 4783 THEN TRY_CONVERT( DATE, SUH.value, 103) END) AS OffSale
FROM  UPLOADS.RECORDS UR LEFT JOIN
      UPLOADS.LINES AS SUH
      ON SUH.record_id = UR.record_id AND
         SUH.field_id IN (4782, 4783)
WHERE ur.module_id = 18
GROUP BY ur.[user_id], ur.record_id;

That said, your problem is that your data is not in the format you think it is. 就是说,您的问题是您的数据不是您认为的格式。 Hence, the problem with type conversions. 因此,类型转换的问题。 As @marc_s says in a comment, you should be using SQL native and appropriate types which in this case is DATE . 正如@marc_s在评论中所说,您应该使用SQL本机类型和适当的类型,在这种情况下为DATE And you certainly should not be using deprecated types, such as TEXT (unless you really just mean VARCHAR() and don't realize that there is a deprecated TEXT type). 而且,您当然不应该使用不推荐使用的类型,例如TEXT (除非您真的只是说VARCHAR()并且不知道存在不推荐使用的TEXT类型)。 If you are storing values in strings (because there are different types), you should use the standard format, either YYYYMMDD or YYYY-MM-DD. 如果将值存储在字符串中(因为存在不同的类型),则应使用标准格式YYYYMMDD或YYYY-MM-DD。

You can find these values by running: 您可以通过运行以下命令找到这些值:

select suh.value
from uploads.lines suh
where suh.field_id in (4782, 4783) and
      try_convert(date, suh.value, 103) is null and
      suh.value is not null;

This can help you fix your data. 这可以帮助您修复数据。

After you have fixed your data, you can also fix the type: 修复数据后,还可以修复类型:

update uploads.lines
    set suh.value = convert(varchar(255), convert(date, suh.value, 103), 112);  -- this will convert the value to the default date format

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

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