本文介绍了在SQL Server中显示前10个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的查询如下 选择currentyearcooperative,案例CHARINDEX(' - ',currentyearcooperative)当0然后currentyearcooperative else SUBSTRING( currentyearcooperative,1,CHARINDEX(' - ',currentyearcooperative) - 1)结束FarmerCoNo, 案例CHARINDEX(' - ',currentyearcooperative)当0然后currentyearcooperative其他SUBSTRING(currentyearcooperative,CHARINDEX(' - ', currentyearcooperative)+ 1,LEN(currentyearcooperative))结束FarmerCoName 来自tbl_farmerregistration_ivorycoast,其中farmersapcode ='1147215' 当我运行上面的时候我得到如下输出 currentyearcooperative FarmerCoNO FarmerCoName FANYOUDALA(DIAWALA )FANYOUDALA(DIAWALA)FANYOUDALA(DIAWALA) 从上面我想要的输出如下 b $ b currentyearcooperative FarmerCoNO FarmerCoName FANYOUDALA(DIAWALA)FANYOUDALA FANYOUDALA i想显示第一个FarmerCoNo和FarmerCoName的10个字符。 来自上面的sql查询我必须做出哪些更改才能得到我的上述输出。 我尝试过: 我的查询如下 选择currentyearcooperative,案例CHARINDEX(' - ',currentyearcooperative)当0然后currentyearcooperative否则SUBSTRING(currentyearcooperative,1,CHARINDEX(' - ',currentyearcooperative) - 1)结束FarmerCoNo, 案例CHARINDEX(' - ',currentyearcooperative)当0然后currentyearcooperative其他SUBSTRING(currentyearcooperative,CHARINDEX(' - ',currentyearcooperative)+ 1,LEN(currentyearcooperative))结束FarmerCoName 来自tbl_farmerregistration_ivorycoast其中farmersapcode ='1147215' 当我运行ab时ove我输出如下 currentyearcooperative FarmerCoNO FarmerCoName FANYOUDALA(DIAWALA)FANYOUDALA(DIAWALA)FANYOUDALA (DIAWALA) 从上面我想要的输出如下 currentyearcooperative FarmerCoNO FarmerCoName FANYOUDALA(DIAWALA)FANYOUDALA FANYOUDALA i想要显示FarmerCoNo和FarmerCoName的前10个字符。 来自上面的sql查询我必须做些什么改变才能得到我的以上输出。My query as followsselect currentyearcooperative,case CHARINDEX('-', currentyearcooperative) when 0 then currentyearcooperative else SUBSTRING(currentyearcooperative, 1, CHARINDEX('-', currentyearcooperative) - 1) end FarmerCoNo,case CHARINDEX('-', currentyearcooperative) when 0 then currentyearcooperative else SUBSTRING(currentyearcooperative, CHARINDEX('-', currentyearcooperative) + 1, LEN(currentyearcooperative)) end FarmerCoNamefrom tbl_farmerregistration_ivorycoast where farmersapcode = '1147215'When i run the above i get output as followscurrentyearcooperative FarmerCoNO FarmerCoNameFANYOUDALA(DIAWALA)FANYOUDALA (DIAWALA)FANYOUDALA (DIAWALA)From the above i want output as followscurrentyearcooperative FarmerCoNO FarmerCoNameFANYOUDALA(DIAWALA) FANYOUDALA FANYOUDALAi want to display the first 10 characters of FarmerCoNo and FarmerCoName.from the above sql query what changes i have to made to get my above output.What I have tried:My query as followsselect currentyearcooperative,case CHARINDEX('-', currentyearcooperative) when 0 then currentyearcooperative else SUBSTRING(currentyearcooperative, 1, CHARINDEX('-', currentyearcooperative) - 1) end FarmerCoNo,case CHARINDEX('-', currentyearcooperative) when 0 then currentyearcooperative else SUBSTRING(currentyearcooperative, CHARINDEX('-', currentyearcooperative) + 1, LEN(currentyearcooperative)) end FarmerCoNamefrom tbl_farmerregistration_ivorycoast where farmersapcode = '1147215'When i run the above i get output as followscurrentyearcooperative FarmerCoNO FarmerCoNameFANYOUDALA(DIAWALA)FANYOUDALA (DIAWALA)FANYOUDALA (DIAWALA)From the above i want output as followscurrentyearcooperative FarmerCoNO FarmerCoNameFANYOUDALA(DIAWALA) FANYOUDALA FANYOUDALAi want to display the first 10 characters of FarmerCoNo and FarmerCoName.from the above sql query what changes i have to made to get my above output.推荐答案 字符串函数(Transact-SQL)| Microsoft Docs [ ^ ] LEFT 可能适合您.. 。String Functions (Transact-SQL) | Microsoft Docs[^]LEFT may be the one for you...您需要SUBSTRING功能: SQL Server SUBSTRING()函数 [ ^ ] - 如果它总是只有10个字符,那么它就是不重要的。如果它直到第一个空间,那么您还需要CHARINDEX函数: SQL Server CHARINDEX()函数 [ ^ ] 在你的情况下,你需要更仔细地查看你的数据 - 它正在修剪为' - ',所以除非在每一行和正确的地方存在,你将得不到你想要的。由于您的输出似乎比您要求的更长,或许您需要检查您的输入以决定做什么 - 我们无法做到这一点,因为我们无法访问您的数据。 我建议创建TRIMTO用户函数并调用它而不是内联代码:它使得阅读其余查询变得更容易,并且应该比重复使用CHARINDEX更有效。此外,它更容易调试和正确。You need the SUBSTRING function: SQL Server SUBSTRING() Function[^] - if it's always just 10 chars, then it's trivial. If it's "up to the first space" then you need the CHARINDEX function as well: SQL Server CHARINDEX() Function[^]In your case, You need to look more closely at your data - it's trimming to '-' so unless that exists in each row and in the right place you won't get what you want. Since your output seems longer that you are asking for, perhaps you need to review your inputs to decide what to do - we can't do that as we have no access to your data.I would suggest creating TRIMTO user function and calling that instead of inlining the code though: it makes it a lot easier to read the rest of your query, and should be more efficient than repeatedly using CHARINDEX. Plus it's easier to debug and get right. 这篇关于在SQL Server中显示前10个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!