June 12, 2015 at 7:47 am
I have such Function:
IF EXISTS (SELECT * FROM sys.objects WHERE name = 'TwoDigitsNumber' AND type = 'FN')
DROP FUNCTION MinimumOFThree;
GO
CREATE FUNCTION TwoDigitsNumber(@a int)
RETURNS nvarchar(20)
AS
BEGIN
DECLARE @STR nvarchar(20);
DECLARE @first int;
DECLARE @second int;
SET @first = @a / 10;
SET @second = @a % 10;
if (@first < @second)
BEGIN
SET @STR = 'second';
END
else if (@first > @second)
BEGIN
SET @STR = 'first';
END
else
BEGIN
SET @STR = 'equal';
END
RETURN @STR;
END
DECLARE @a int;
SET @a = 52;
DECLARE @STR nvarchar;
EXEC @STR = TwoDigitsNumber @a;
SELECT @STR;
GO
The only first letter 'f', 's', 'e' is inserted in value instead 'first', 'second', 'equal'.
Why ? How can i insert whole string ? Help me, please.
June 12, 2015 at 7:58 am
Don't use nvarchar. Use nvarchar(size).
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
June 12, 2015 at 8:01 am
I use nvarchar(20) everywhere, but it doesn't help (
June 12, 2015 at 8:01 am
Phil hit the nail on the head. With a declared variable, if you don't specify a size for your string, the default length is 1.
June 12, 2015 at 8:03 am
June 12, 2015 at 8:07 am
Where must I specify the length more then 1 symbol ?
June 12, 2015 at 8:21 am
Thank you !
June 12, 2015 at 8:37 am
No problem. Glad I could help.
June 12, 2015 at 9:27 am
koles2004 (6/12/2015)
I have such Function:IF EXISTS (SELECT * FROM sys.objects WHERE name = 'TwoDigitsNumber' AND type = 'FN')
DROP FUNCTION MinimumOFThree;
GO
CREATE FUNCTION TwoDigitsNumber(@a int)
RETURNS nvarchar(20)
AS
BEGIN
DECLARE @STR nvarchar(20);
DECLARE @first int;
DECLARE @second int;
SET @first = @a / 10;
SET @second = @a % 10;
if (@first < @second)
BEGIN
SET @STR = 'second';
END
else if (@first > @second)
BEGIN
SET @STR = 'first';
END
else
BEGIN
SET @STR = 'equal';
END
RETURN @STR;
END
DECLARE @a int;
SET @a = 52;
DECLARE @STR nvarchar;
EXEC @STR = TwoDigitsNumber @a;
SELECT @STR;
GO
The only first letter 'f', 's', 'e' is inserted in value instead 'first', 'second', 'equal'.
Why ? How can i insert whole string ? Help me, please.
Read about inline table-valued functions. Scalar UDFs have performance issues. Something like this:
DECLARE @a int
SET @a = 25
SELECT Result = CASE WHEN [first] < [second] THEN 'second' WHEN [first] > [second] THEN 'first' ELSE 'equal' END
FROM (SELECT [first] = @a / 10, [second] = @a % 10) d
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 12, 2015 at 9:53 am
Yea, Chris, I was wondering about the purpose of the function myself. I figured it was an example to illustrate the point. Or maybe that was hope on my part. ๐
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply