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

时间:2019-06-20 13:34:08

标签: sql sql-server reporting-services type-conversion

数据类型为TEXT,输入为'20 / 11/2017',使用MAX或MIN时会忽略月份。我正在尝试将其转换为月份的日期格式,以供考虑。

CAST AND CONVERT似乎不起作用,因为以下错误返回

  

第241条消息,第16级,状态1,第13行
  从字符串转换日期和/或时间时转换失败。

代码:

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;

最终结果本质上将是MAX或MIN日期,而所有三个组成部分都是日期,月份和年份。因此,如果用户输入了两个日期,即17/05/2018和17/04/2018,那么如果使用MAX,则应该显示第一个日期。

2 个答案:

答案 0 :(得分:1)

使用CONVERT时可以使用格式代码,甚至可以使用TRY_CONVERT来防止无效日期的错误。我还改进了您的代码,使其更简单,更高效。

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;

答案 1 :(得分:0)

在SQL方面,这对Luis的回答略有改进:

###before modification###
 <html><body><p class="section-para">We would be happy to hear from you, <strong>Please fill in the form below</strong> or mail us your requirements on <br/><span class="text-red">contact@xyz.com</span></p></body></html> 

###after p tag###
 <html><body>
p tag: We would be happy to hear from you, <strong>Please fill in the form below</strong> or mail us your requirements on <br/><span class="text-red">contact@xyz.com</span>
</body></html> 

###after strong tag###
 <html><body>
p tag: We would be happy to hear from you, \Bold: Please fill in the form below \Bold or mail us your requirements on <br/><span class="text-red">contact@xyz.com</span>
</body></html> 

也就是说,您的问题是您的数据不是您认为的格式。因此,类型转换的问题。就像@marc_s在评论中说的那样,您应该使用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; 。并且您当然不应该使用不推荐使用的类型,例如DATE(除非您真的只是指TEXT并且不知道存在不推荐使用的VARCHAR()类型)。如果您将值存储在字符串中(因为存在不同的类型),则应使用标准格式YYYYMMDD或YYYY-MM-DD。

您可以通过运行以下命令找到这些值:

TEXT

这可以帮助您修复数据。

修复数据后,还可以修复类型:

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;