Solved

SQL Function

Posted on 2013-05-10
3
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need more granular date groupings 4 44
Creating a View from a CTE 15 46
SQL Query 9 27
SQL Server Agent "Access Denied" Error 3 35
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

734 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