在SQL Server中转换日期
我正在尝试在SQL Server中执行查询。我无法过滤日期。输出总是相同的,数据不按日期过滤 日期的格式如下:在SQL Server中转换日期,sql,sql-server,Sql,Sql Server,我正在尝试在SQL Server中执行查询。我无法过滤日期。输出总是相同的,数据不按日期过滤 日期的格式如下: 29-12-2020 16:38 31-12-2020 17:43 我尝试进行以下所有操作,但均未成功: select start_date from table_1 where start_date between '01-01-2021 00:00:00' and '31-01-2021 00:00:00' select start_date from tabl
29-12-2020 16:38
31-12-2020 17:43
我尝试进行以下所有操作,但均未成功:
select
start_date
from table_1
where start_date between '01-01-2021 00:00:00' and '31-01-2021 00:00:00'
select
start_date
from table_1
where start_date between '01-01-2021 00:00' and '31-01-2021 00:00'
select
start_date
from table_1
where start_date between '01-01-2021' and '31-01-2021'
我也试着去投,但我运气不好:
select
cast(start_date as date) as start_date
from table_1
where start_date between '2021-01-01' and '2021-01-31'
有人能帮我吗?
你有一个字符串。我强烈建议把它定为某种日期/时间。以下转换工程:
select convert(datetime, left(str, 10), 105) + convert(datetime, right(str, 5))
from (values ('29-12-2020 16:38')) v(str);
一个操作是计算列:
alter table table_1 start_date_dt as
( convert(datetime, left(start_date, 10), 105) + convert(datetime, right(start_date, 5)) )
然后可以将此值用于
where
子句。或者更好。修正数据!当有适当的数据类型时,不要将值存储在字符串中。表1中开始日期字段的数据类型是什么?无论是“未解决”还是“我运气不佳”都不是可用的问题描述。您到底得到了哪些输出/错误?为什么这些错误或不足?]如果您对日期文字使用明确的文字值,yyyyMMdd
或yyyy-MM-ddThh:MM:ss
会发生什么?数据未得到过滤,那么我会建议您的列不是日期(和时间)数据类型。这是一个需要修复的严重设计缺陷。