Solved

SQL Function

Posted on 2013-05-10
3
416 Views
Last Modified: 2013-05-10
Hi,

I have a column name that contains values like
http://myServer/Lists/myform/DispForm.aspx?ID=6, RAB12EGH234
http://myServer/Lists/myform/DispForm.aspx?ID=5, CBTRR2345

I just need to get the formName which is the last string value after, and space.

I have used this to get the desired result:
rtrim(substring(FormName, charindex(', ', FormName, 0) + 2, 50))

Now I want to create a SQL function where I could pass in that Column Name and get the desired result. How do I do that? TIA.
0
Comment
Question by:sspb485
3 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39156238
declare @test varchar(100)
declare @index int
set @test = 'http://myServer/Lists/myform/DispForm.aspx?ID=5, CBTRR2345'

set @index = CHARINDEX(' ', @test)

select SUBSTRING(@test, @index + 1, len(@test) - @index)



@test would be the column value you're passing in.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 39156257
CREATE FUNCTION [dbo].[getFormName](@p_column varchar)
RETURNS VARCHAR(MAX)
AS
BEGIN
declare @FormName varchar(max)
set @FormName = rtrim(substring(@p_column,charindex(', ',@p_column, 0)+2,50))
RETURN @FormName;
END;

select dbo.getFormName(yourcolumn)
from yourtable;
0
 

Author Comment

by:sspb485
ID: 39156340
Hi awking00,

I did create the function however it returned blank when I did select dbo.getFormName(yourcolumn)
from yourtable;

Any clue? Thanks.

Nvm, I found the mistake. There's no length of varchar for @p_column. Thanks.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question