Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,

I would like to know how to split data like 1.2.3 to 1.2 in sql...


Thanks in Advance

seshu
Posted
Comments
walterhevedeich 12-May-11 6:14am    
do you mean trim data?

try this


declare @Variable varchar(50)
set  @Variable= '1.2.3'
declare @RowData varchar(50)
Set @RowData = Substring(@Variable,Charindex('.',@Variable)-3,len(@Variable))
print @RowData
 
Share this answer
 
v2
use below script
select SUBSTRING(string,startpoint,endpoint);

select SUBSTRING('1.2.3',1,3);
o/p 1.2
 
Share this answer
 
I think you will need a combination of SUBSTRING[^] and CHARINDEX[^] - it depends on your data format, which only you will know!
 
Share this answer
 
Comments
#realJSOP 12-May-11 9:27am    
Ya know why they didn't 5 you, right? It's because you didn't provide code and do their work for them.
OriginalGriff 12-May-11 9:41am    
:laugh: Probably true...
Manfred Rudolf Bihy 12-May-11 10:47am    
My 5+
Better now? :)
OriginalGriff 12-May-11 10:49am    
:laugh: All better now - I can relax!
There are many ways you can do this like using SubString(), or CharIndex() or Split().

You can use regular expression also

string str = "1.2.3";
pattern = @"(?<name>\d\.\d+?)";
m = Regex.Match(str, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (m.Success)
{
    Response.Write("Name:" + m.Groups["name"].Value + "<br />");
}
</name>


Using Split
string[] s = str.Split('.');
if(s.Length >=2)
     str = s[0] + "." + s[1];
 
Share this answer
 
Comments
Dylan Morley 12-May-11 7:31am    
He needs it in SQL

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