关于取日期的问题,帮忙看一下。

临摹 2008-01-24 10:09:56
表中日期,是VARCHAR格式的。
2008-2-5
2008-2-3
2008-2-4
2008-2-7
2008-2-8
2008-2-9
2008-2-10
2008-2-11
2008-2-12
2008-2-13
我用SELECT MIN(DATESTAR) FROM TABLE时,取出的结果是2008-2-10。
怎么让取出的结果是2008-2-3。
...全文
69 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdsxlj 2008-01-24
  • 打赏
  • 举报
回复
发快了6分钟
yangjiexi 2008-01-24
  • 打赏
  • 举报
回复
只不过是你发快了些!!这是一种思路!!
sdsxlj 2008-01-24
  • 打赏
  • 举报
回复
楼上抄的好快
yangjiexi 2008-01-24
  • 打赏
  • 举报
回复

declare @tb table(sdate varchar(10))
insert into @tb
select '2008-2-5'
union
select '2008-2-3'
union
select '2008-2-4'
union
select '2008-2-8'
union
select '2008-2-9'
union
select '2008-2-11'
union
select '2008-2-12'
union
select '2008-2-13'


select sdate from @tb
where cast(sdate as datetime)=(select min(cast(sdate as datetime)) from @tb)
青锋-SS 2008-01-24
  • 打赏
  • 举报
回复
declare @t table(datestar varchar(100))      
insert into @t select '2008-2-5'
insert into @t select '2008-2-3'
insert into @t select '2008-2-4'
insert into @t select '2008-2-7'
insert into @t select '2008-2-8'
insert into @t select '2008-2-9'
insert into @t select '2008-2-10'
insert into @t select '2008-2-11'
insert into @t select '2008-2-12'
insert into @t select '2008-2-13'

select * from @t a where cast(a.datestar as datetime)=(select min(cast(datestar as datetime)) from @t b)

datestar
----------------------------------------------------------------------------------------------------
2008-2-3

(1 行受影响)
青锋-SS 2008-01-24
  • 打赏
  • 举报
回复
晕了
wzy_love_sly 2008-01-24
  • 打赏
  • 举报
回复
declare @tb table (dt varchar(20))
insert into @tb select '2008-2-5'
insert into @tb select '2008-2-3'
insert into @tb select '2008-2-4'
insert into @tb select '2008-2-7'
insert into @tb select '2008-2-8'
insert into @tb select '2008-2-9'
insert into @tb select '2008-2-10'
insert into @tb select '2008-2-11'
insert into @tb select '2008-2-12'
insert into @tb select '2008-2-13'
select convert(varchar(10),min(cast(dt as datetime)),120) as dt from @tb


2008-02-03

用convert转
leo_lesley 2008-01-24
  • 打赏
  • 举报
回复

---例子 不知道是不是你要的
create table tb(datestar varchar(10))
insert tb
select '2008-2-5'
union select '2008-2-3'
union select '2008-2-4'
union select '2008-2-7'
union select '2008-2-8'
union select '2008-2-9'
union select '2008-2-10'
union select '2008-2-11'
union select '2008-2-12'
union select '2008-2-13'


select top 1 datestar from tb order by year(datestar),month(datestar),day(datestar)


drop table tb
sdsxlj 2008-01-24
  • 打赏
  • 举报
回复
datestar
----------------------------------------------------------------------------------------------------
2008-2-3

(1 行受影响)

sdsxlj 2008-01-24
  • 打赏
  • 举报
回复
declare @t table(datestar varchar(100))      
insert into @t select '2008-2-5'
insert into @t select '2008-2-3'
insert into @t select '2008-2-4'
insert into @t select '2008-2-7'
insert into @t select '2008-2-8'
insert into @t select '2008-2-9'
insert into @t select '2008-2-10'
insert into @t select '2008-2-11'
insert into @t select '2008-2-12'
insert into @t select '2008-2-13'

select * from @t a where cast(a.datestar as datetime)=(select min(cast(datestar as datetime)) from @t b)
pt1314917 2008-01-24
  • 打赏
  • 举报
回复

要这样?

select datename(yy,min(datestar))+'-'+ltrim(datepart(mm,min(datestar)))+'-'+ltrim(datepart(dd,min(datestar))) from table
临摹 2008-01-24
  • 打赏
  • 举报
回复
楼上的好快,是我没说清楚,因为在修改程序时候使用的,最好取出后的字符
还是2008-2-3,不要把格式改变了,因为后面程序用到的都是这种格式。
青锋-SS 2008-01-24
  • 打赏
  • 举报
回复
select min(convert(char(10),datestar,120)) from table
wzy_love_sly 2008-01-24
  • 打赏
  • 举报
回复
select min(cast(datestar as datetime)) from table

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧