SQL转换日期时间没有小时,订购问题也

时间:2013-02-28 08:47:45

标签: sql casting

SELECT 

incident.createDate,
LEFT(incident.createDate,11)as EntryDate,
ContactDetails.cityCode,
patient.insurance,

FROM ......

WHERE 
incident.createDate Between Convert(smalldatetime, '01/07/2012', 103) and Convert(smalldatetime, '31/12/2012', 103) 
and servicecharges.serviceid = 31 
and ServiceRequestDescription LIKE '05.06%'
and chargeDescr = 'Ε0 50.01'
and incident.status != 6
order by createDate ASC, contactdetails.cityCode ASC, patient.insurance ASC

作为输出我得到了

15642     2012-08-03 11:45:07.153       Aug  3 2012         103      15

我想得到的是

15642     2012-08-03      103        15

EntryDate如上所示投射日期,所以我不想要它。订购也错了。它使用日期正确地命令输出,但是城市代码和保险不正确..

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT 
...
CONVERT(Varchar, incident.createDate, 103) as EntryDate,
ContactDetails.cityCode,
patient.insurance,

FROM ......

答案 1 :(得分:0)

对于Sql-Server:我认为没有direct format来获取 ONLY yyyy-mm-dd。您可以尝试 120/121 ,然后获取 left 10 characters ,如下所示。小demo here

select left(convert(varchar,incident.createDate,120),10) as EntryDate