由于数据库存的是整个字符串组到一起了,c#代码是这个样子的。
public static string time(datetime dt) { return dt.year.tostring() + ((convert.toint32(dt.month) < 10) ? "0" + dt.month.tostring() : dt.month.tostring()) + ((convert.toint32(dt.day) < 10) ? "0" + dt.day.tostring() : dt.day.tostring()) + ((convert.toint32(dt.hour) < 10) ? "0" + dt.hour.tostring() : dt.hour.tostring()) + ((convert.toint32(dt.minute) < 10) ? "0" + dt.minute.tostring() : dt.minute.tostring()) + ((convert.toint32(dt.second) < 10) ? "0" + dt.second.tostring() : dt.second.tostring()); }
在sqlserver中存储的实际值是:20190416124941。那么直接转换?
所以在sqlserver中查询的时候我们要进行转化,因为在mssql中进行转换需要是有标准的 例如/ : 等符号。那么我们就进行截取吧。
convert(datetime, left(q.yl01,4)+'-'+substring(q.yl01,5,2)+'-' +substring(q.yl01,7,2))>=dateadd(mm,-3,getdate())
最后就完事了。
select distinct top 1 z.*, max(case when s.col_name_en = 'dwtt_crack_act' then s.actual else null end) as [断裂数量实绩值], max(case when s.col_name_en = 'dwtt_dft_act' then s.actual else null end) as [挠度实绩值],q.yl01 from (select z.entrustment,z.test_item_code,z.sample_no from hb_m0lmzl z where test_item_code = 'qf' group by z.entrustment,z.test_item_code,z.sample_no having max(case when z.col_name_en = 'cast_no' then z.actual else null end) = '' ) as z left join hb_m0lmq1 q on q.sample_lot_no = z.entrustment and q.test_item_code = z.test_item_code and z.sample_no = q.sample_no left join hb_lmm0sj s on s.test_item_code = z.test_item_code and s.sample_no = z.sample_no and s.entrustment = z.entrustment where convert(datetime, left(q.yl01,4)+'-'+substring(q.yl01,5,2)+'-' +substring(q.yl01,7,2))>=dateadd(mm,-3,getdate()) group by z.entrustment,z.test_item_code,z.sample_no,q.yl01 order by q.yl01