Using expressions as a parameter for a SQL Server function - sql

SQL Server comes with a number of string functions, such as RIGHT(), which accepts an Expression as a parameter so that it can accept either a varchar or nvarchar value.
How do I create my own custom function to do the same?
I am not a SQL Server expert, so a simple example with syntax would help.
Thank You

Here is a simple example:
CREATE FUNCTION myUDF (#input nvarchar(255))
RETURNS nvarchar(255)
AS
BEGIN
-- function logic here
declare #output nvarchar(255)
select #output = 'The value passed in was: ' + #input
return #output
End
GO
select dbo.myUDF('I wrote a function!')

Here you can try this way.
How to: Create and Run a CLR SQL Server User-Defined Function

Related

I am new to SQL Server. I created a function which action like charindex

I am new to SQL server. I am trying to create a scalar-valued function which will act like charindex.
IF EXISTS(SELET * FROM sysobjects
WHERE id = OBJECT_ID(N'My_search_string')
AND xtype IN (N'FN', N'IF', N'TF'))
DROP FUNCTION My_Search_String
GO
CREATE FUNCTION My_Search_String
(#target_string varchar,
#string varchar)
RETURNS INT
AS
BEGIN
DECLARE #result int;
SET #result = CHARINDEX(#target_string, #string)
RETURN #result
END
GO
but I am not so sure why when I call the function like
select dbo.My_Search_string('cd','abcde')
go
it return 0 as result.
Thank you so much for your help :)
have a great day guys
In SQL Server, you need to specify lengths on string datatypes. If you don't, it assumes length 1.
Therefore your create function line should look like
Create Function My_Search_String(#target_string varchar(50),#string varchar(50))

Defining a function in sql server

I want to declare a function in SQL SERVER, It is the beginning of my code, but I get this error: 'CREATE FUNCTION' must be the only statement in the batch.
CREATE FUNCTION FindingMobileOrTelephone ( #Number nchar(11) )
RETURNS nvarchar
AS
BEGIN
DECLARE #ItIsMobile nvarchar;
DECLARE #ItIsTelephone nvarchar;
RETURNS #ItIsMobile;
END;
what is the problem?!
try this
Last Statement Should be RETURN not RETURNS
CREATE FUNCTION FindingMobileOrTelephone ( #Number nchar(11) )
RETURNS nvarchar
AS
BEGIN
DECLARE #ItIsMobile nvarchar;
DECLARE #ItIsTelephone nvarchar;
RETURN (#ItIsMobile);
^
END
CREATE FUNCTION/TABLE should be the first statement in the script you're executing.
One option was already mentioned. Adding GO kind of splits the script into separate ones.
However, if you have this within BEGIN END block this won't work since you can't split this block with GO.
An option here is to wrap CREATE statment into a string literal and use EXEC or sp_executesql
-- your script
EXEC('CREATE FUNCTION ... END')

Creating a TSQL Shortcut for Converting to String

I'd like to write a user-defined function on SQL Server 2012 that converts any value to String/Text.
When i inspect SQL Server, the type of Convert function parameter is "expression", but I don't seem to be able to use this type for my own user-defined function. Is there any other alternative?
CREATE FUNCTION [dbo].[Text]
(
#value expression
)
RETURNS nvarchar(max)
AS
BEGIN
RETURN Convert(nvarchar(max), #value);
END
I receive the following error message:
Column, parameter, or variable #1: Cannot find data type expression.
Found the answer in case anyone is interested:
CREATE FUNCTION [dbo].[Text]
(
#value SQL_VARIANT
)
RETURNS nvarchar(max)
AS
BEGIN
RETURN Convert(nvarchar(max), ISNULL(#value,''));
END
Seems to do what I need it to.
Why you want to with the built in function, I don't know. However, the data type you want is sql_variant
CREATE FUNCTION [dbo].[Text]
(
#value sql_variant
)

Passing a function as argument to a stored procedure

Is there any way to send functions as parameters to stored procedures?
create procedure stp_dummy
#input nvarchar(255)
as select #input
exec stp_dummy a_function_that_returns_string('abracadabra')
(Of course that I know that the function can be called previously but I would like to know if the direct solution is available.)
I know this isn't the answer you're looking for, but the only way to do this is to declare a local variable, assign the function value to this, and use this as your parameter value:
DECLARE #input nvarchar(255)
SET #input = a_function_that_returns_string('abracadabra')
EXEC stp_dummy #input=#input
With SQL Server 2008, this can be done in 2 lines:
DECLARE #input nvarchar(255) = a_function_that_returns_string('abracadabra')
EXEC stp_dummy #input=#input
No, this is a limitation of SQL Server. You'll have to do something like what Curt demonstrated.

'TRIM' is not a recognized built-in function name

I have created simple function
create function TRIM(#data varchar(20)) returns varchar(100)
as
begin
declare #str varchar(20)
set #str = rtrim(ltrim(#data))
return #str
end
I am executing in the below way.
declare #s varchar(25)
set #s = ' Amru '
select TRIM(#s)
I am getting the following error.
Msg 195, Level 15, State 10, Line 3
'TRIM' is not a recognized built-in function name.
Could any one please help me find the issue?
//use RTrim instead of Trim sql 2008
RTrim(ColumnName)
like this
select RTrim(a.ContactName) + ' ' + RTrim(a.City) as Name_City from customers as a
declare #s varchar(25)
set #s = ' Amru '
select RTRIM(LTRIM(#s))
You can use this way also without schema :)
You need to use the Schema prefix when calling user defined functions. In your case this will most likely be "dbo".
Change your select statement to:
declare #s varchar(25)
set #s = ' Amru '
select dbo.TRIM(#s)
The error is that 'TRIM' is not a built-in function in SQL Server (as the error message suggests :) )
You can either wrap it with both LTRIM and RTRIM instead, or create your own TRIM function that does the same.
SQL server tries to parse TRIM as a built-in function. To call user-defined function you should put schema prefix in front of the function call. Try something like:
declare #s varchar(25)
set #s = ' Amru '
select dbo.TRIM(#s)
Since dbois a default schema prefix.
If you want to change your schema, declare the function as follow: (note the schema prefix in front of function name)
create function dbo.TRIM(#data varchar(20)) returns varchar(100)
as
begin
--function body
end