473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Substring in SQL server

Hi Folks...

I have a string in the format '1.2.3.4.5.6'. I have to write a generic function that will take integer as paramater and will return the string as follows for the above input.
  • ParamaterValue : Output.
  • 1 : 1
  • 2 : 1.2
  • 3 : 1.2.3
  • 4 : 1.2.3.4
Sep 15 '09 #1
2 3783
ck9663
2,878 Expert 2GB
Sounds like an assignment :)

What do you have so far?

--- CK
Sep 16 '09 #2
Its not an assignment.I have come up with the below solution.

CREATE FUNCTION [dbo].[StringToTable]
(
@inputString nvarchar(max),
@separator char (1),
@tokens int
)
RETURNS nvarchar(4000)--@ResultTable TABLE ( [String] nvarchar(max) )
AS
BEGIN
DECLARE @ResultTable TABLE ( [String] nvarchar(max) )
DECLARE @ResultVar nvarchar(4000)
DECLARE @stringToInsert nvarchar (max)
DECLARE @cnt int

WHILE LEN(@inputString) > 0
BEGIN
SET @StringToInsert = LEFT(
@inputString,
ISNULL(NULLIF(CHARINDEX(@separator, @inputString) - 1, -1),
LEN(@inputString)
)
)
SET @InputString = SUBSTRING(@InputString,

ISNULL
(NULLIF
(CHARINDEX(@separator, @InputString),
0),
LEN(@InputString)) + 1,
LEN(@InputString))

INSERT INTO @ResultTable
(
[String]
)
VALUES
(
@StringToInsert
)

END

SELECT @cnt = count(*) from @ResultTable

If @cnt>= @tokens
BEGIN
SELECT @ResultVar = STUFF((SELECT top (@tokens) '.' + [String] FROM @ResultTable
FOR XML PATH('')),1, 1, '') ;
END
ELSE
BEGIN
SELECT @ResultVar = NULL
END
RETURN @ResultVar;
END

----------------------
You can execute the above function as
select [dbo].[StringToTable]('1.1.2.3','.',2) will return '1.1'

I am trying to modify the function without the use of @ResultTable TABLE
Paramater.
Sep 16 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an...
1
by: sysindex | last post by:
I am trying to find a way to dynamically retrieve the substring starting point of an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
2
by: Michele Fondry via .NET 247 | last post by:
hello. I have a webform form app in vb.Net that uses a SQL query. I am trying to use the substring function, but get the following error: Description: An unhandled exception occurred during the...
5
by: btober | last post by:
I can't seem to get right the regular expression for parsing data like these four sample rows (names and addresses changed to ficticious values) from a text-type column: Yolanda Harris, 38, of...
3
by: David Greenberg | last post by:
Hi I have a character field, varchar(60), and I want to replace only a part of it, a set substring which is always in the same set positions. Does anyone have an Sql statement or algorithm/sp ...
8
by: chudson007 | last post by:
I need help capturing information from a free text field. The 10 examples below contain examples of the information I am trying to capture. In each cell I am trying to capture the number between...
1
by: db55 | last post by:
This script doesn't work. Why? UPDATE SET = LTRIM(SUBSTRING(, 1, convert(bigint, CHARINDEX(',', Comments)-1))) WHERE NOT( IS NULL) AND LEN() > 8 Basically, I'm trying to...
11
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the...
2
by: falconite | last post by:
In a perl script, I'm reading in data from a mysql server database that has some fields that could be millions of bytes long (DNA sequences). However, I am interested only in the first 1000 or so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.