Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My query as follows


select 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 FarmerCoName
from tbl_farmerregistration_ivorycoast where farmersapcode = '1147215'


When i run the above i get output as follows

currentyearcooperative FarmerCoNO FarmerCoName

FANYOUDALA(DIAWALA) FANYOUDALA (DIAWALA) FANYOUDALA (DIAWALA)


From the above i want output as follows

currentyearcooperative FarmerCoNO FarmerCoName

FANYOUDALA(DIAWALA) FANYOUDALA FANYOUDALA


i 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 follows


select 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 FarmerCoName
from tbl_farmerregistration_ivorycoast where farmersapcode = '1147215'


When i run the above i get output as follows

currentyearcooperative FarmerCoNO FarmerCoName

FANYOUDALA(DIAWALA) FANYOUDALA (DIAWALA) FANYOUDALA (DIAWALA)


From the above i want output as follows

currentyearcooperative FarmerCoNO FarmerCoName

FANYOUDALA(DIAWALA) FANYOUDALA FANYOUDALA


i 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.
Posted
Updated 20-Aug-18 19:51pm

String Functions (Transact-SQL) | Microsoft Docs[^]

LEFT may be the one for you...
 
Share this answer
 
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900