Creating a TSQL Shortcut for Converting to String - sql

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
)

Related

Why I cannot get a result value from T-SQL function

I'm trying to learn function in SQL Server, and I don't know why I cannot get a proper result from a T-SQL function.
The query what creates function is:
CREATE FUNCTION yuri_func1
(#valDate VARCHAR(10))
RETURNS VARCHAR
AS
BEGIN
DECLARE #valWeekday VARCHAR(10);
SET #valWeekday = DATENAME(WEEKDAY, #valDate);
RETURN #valWeekday;
END
And the other query is
select dbo.yuri_func1('2017-12-29') as [요일]
but the only result I got is just
Blank. (="")
But when I executed function like this,
select DATENAME(WEEKDAY, '2017-12-29')
the result was
MONDAY
I still don't get that why they return different results.
Does anybody know why?
This is because you should be accepting DateTime as a parameter in your function and not varchar
create Function yuri_func1 (#valDate DateTime) --Wrong parameter type
RETURN VARCHAR(10) -- No proper sizing of return type
AS
BEGIN
declare #valWeekday varCHAR(10);
Set #valWeekday = DATENAME(WEEKDAY,#valDate);
return #valWeekday;
END
GO

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')

Evaluating expression in SQL with XQuery (without stored procedure)

I am trying to evaluate an expression in a function, and it happens that XQuery is not able to get string from the SQL variable
DECLARE #XML AS XML = ''
DECLARE #Formula AS NVARCHAR(MAX) = '1+1'
SELECT #XML.query(#Formula)
Using ...
SELECT #XML.query('sql:variable("#Formula")')
... just returns a string 1+1, and not the sum.
Is there a way to make this work in SQL (without using stored procedures, because those will not run form within the function)?
Thanks in advance!

Using expressions as a parameter for a SQL Server function

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

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.