本文介绍了如何设置日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是新的sql sever 2008 r2。其实我有这样的查询 DECLARE @ FROM varchar ( 50 ), @ TO VArchar ( 50 ); SET @ FROM = ' - 07-01'; SET @ TO = ' - 06-30' SELECT StudentDATE = convert(日期,转换( varchar ( 50 ),年份(StudentDATE)-1)+ @ FROM),StudentDATE = convert( Date ,转换( varchar ( 50 ),年份(StudentDATE))+ @ TO)来自学生组 按年( StudentDATE)订单 按年(StudentDATE)i得到输出正确的方式。 但我想要过几个月也是utomatically为我写了这样的查询 DECLARE @ FROM varchar ( 50 ), @ TO VArchar ( 50 ); SET @ FROM = ' - 01'; SET @ TO = ' - 30' SELECT StudentDATE = convert(日期,转换( varchar ( 50 ),年份(StudentDATE)-1)+月份(StudentDATE)+ @ FROM),StudentDATE = convert( Date ,转换( varchar ( 50 ),year(StudentDATE))+ month(StudentDATE)+ @ TO)来自学生组 按年(StudentDATE)订单 按年( StudentDATE) 我收到这样的错误信息 转换失败w母鸡将日期和/或时间转换为字符串.. 请帮助我.. 解决方案 使用您的第二个选项尝试此查询 选择 转换(日期, convert ( varchar ( 50 ),强制转换((年(StudentDATE)-1)为 varchar )+ ' - ' + cast(月(StudentDATE) as varchar )+ @ FROM)), convert (日期, convert ( varchar ( 50 ),cast(年(StudentDATE) as varchar )+ ' - ' + cast(月(StudentDATE) as varchar )+ @TO)) Hi i am new sql sever 2008 r2. Actually i am having a query like this DECLARE @FROM varchar(50),@TO VArchar(50);SET @FROM ='-07-01';SET @TO ='-06-30' SELECT StudentDATE=convert(Date,Convert(varchar(50),year(StudentDATE)-1)+@FROM),StudentDATE=convert(Date,Convert(varchar(50),year(StudentDATE))+@TO) from Student Group By year(StudentDATE) order by year(StudentDATE)i am getting output correct way .but i want to pass months also automatically for that i was wrote query like this DECLARE @FROM varchar(50),@TO VArchar(50);SET @FROM ='-01';SET @TO ='-30'SELECT StudentDATE=convert(Date,Convert(varchar(50),year(StudentDATE)-1)+month(StudentDATE)+@FROM),StudentDATE=convert(Date,Convert(varchar(50),year(StudentDATE))+month(StudentDATE)+@TO) from Student Group By year(StudentDATE) order by year(StudentDATE)I am get error message like this conversion failed when converting date and /or time to character to string .. please help me .. 解决方案 Try this query with your second optionselect convert(Date,convert(varchar(50),cast((year(StudentDATE)-1) as varchar )+ '-' +cast(month(StudentDATE)as varchar )+@FROM)),convert(Date,convert(varchar(50),cast(year(StudentDATE)as varchar )+ '-' +cast(month(StudentDATE)as varchar )+@TO)) 这篇关于如何设置日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-09 21:17