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
Solved

SQL Function

Posted on 2013-05-10
3
411 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

837 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