在sql server中'order by'中的错误 [英] error in 'order by' in sql server

查看:105
本文介绍了在sql server中'order by'中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有选择查询,我需要按升序排序。列具有varchar数据类型,因此我需要将其转换为int。我的查询工作正常但有一些情况,当用户需要输入这样的值'121-1'所以它在选择查询上给出错误'将varchar值'121-2'转换为数据类型int时转换失败。'所以即使输入这些值,也有任何解决方案可以忽略此错误('121- 1' )。 ?以下是我的查询

 选择([Casetype] +  '  /' +  convert  varchar  50 ),CaseNo)+ '  / ' +  convert  varchar  50 ),YEAR(GETDATE()))) as  CaseNo,
CaseNo_ID,
转换 varchar 20 ),[fileDate], 103 as DateOfFilling,
DisrtictFrom,
tbl_RecordRequisition.CompName,
tbl_RecordRequisition.RespName,
CaseStage,
convert varchar 20 ),NextDate, 103 as NextDate,
tbl_RecordRequisition.Remarks
from tbl_RecordRequisition
订单 left (CaseNo_ID, 2 asc
right (CaseNo_ID, 4 asc
CAST((替换(替换(CaseNo_ID,(CaseNo_ID, 3 ),' '),(CaseNo_ID, 5 ),' ')) as int asc



请帮助

解决方案

即使输入这些值('121-1'),也有任何解决方案可以忽略此错误。

否。



这是预期的行为 - 如果你想使用整数,那么使用整数列,或者你的情况可能是两列来建立一个范围。

您可以使用SUBSTRING独立地分解每个数字,但从长远来看,正确存储它们的工作要少得多!


两个答案 [ ^ ]。使用它!


以下查询帮助了我



 订单   left (CaseNo_ID, 2  asc  right (CaseNo_ID, 4  asc ,(选择 CAST((tbl_RecordRequisition.CaseNo) as   int 其中​​ tbl_RecordRequisition.CaseNo  喜欢 ' % - %' asc  





全部谢谢


I have select query which i need to order by ascending order. Column has varchar datatype so i need to convert it to int. My query is working fine but there is some situation when the user needs to enter such values '121-1' so it gives an error on select query 'Conversion failed when converting the varchar value '121-2' to data type int.' So is there any solution to ignore this error even on entering such values('121-1'). ? Following is my query

select ([Casetype] +'/'+ convert(varchar(50),CaseNo) +'/'+ convert(varchar(50),YEAR(GETDATE())) )as CaseNo ,
    CaseNo_ID,
    convert(varchar(20),[fileDate],103) as DateOfFilling,
    DisrtictFrom,
    tbl_RecordRequisition.CompName,
    tbl_RecordRequisition.RespName,
    CaseStage,
    convert(varchar(20),NextDate,103) as NextDate,
    tbl_RecordRequisition.Remarks
from tbl_RecordRequisition
order by left(CaseNo_ID,2) asc ,
    right(CaseNo_ID,4) asc,
    CAST((replace(replace(CaseNo_ID,left(CaseNo_ID,3),''),right(CaseNo_ID,5),'')) as int) asc


please help

解决方案

"So is there any solution to ignore this error even on entering such values('121-1')."
No.

That is expected behaviour - if you want to use integers, then use integer columns, or in your case probably two columns to establish a range.
You could use SUBSTRING to break out each number independently, but storing them properly is a lot less work in the long run!


Two answers[^] to your question have been posted on SO. Use it!


Following query helped me

order by left(CaseNo_ID,2) asc ,right(CaseNo_ID,4) asc, (select CAST((tbl_RecordRequisition.CaseNo) as int)  where tbl_RecordRequisition.CaseNo not like '%-%' ) asc



Thanks all


这篇关于在sql server中'order by'中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆