SQL Server 2005 Weird varchar Behavior - sql-server

This SQL Server 2005 T-SQL code:
DECLARE #Test1 varchar;
SET #Test1 = 'dog';
DECLARE #Test2 varchar(10);
SET #Test2 = 'cat';
SELECT #Test1 AS Result1, #Test2 AS Result2;
produces:
Result1 = d
Result2 = cat
I would expect either
The assignment SET #Test1 =
'dog'; to fail because there isn't
enough room in #Test1
Or the SELECT to return 'dog' in the Result1 column.
What is up with #Test1? Could someone please explain this behavior?

Let me answer with some quotes from the SQL Server documentation.
char and varchar
varchar[(n)]
...
When n is not specified in a data definition or variable declaration statement, the default length is 1.
Converting Character Data
When character expressions are converted to a character data type of a different size, values that are too long for the new data type are truncated.
So, your varchar is declared as a varchar(1), and the implicit conversion in your SET statement (from a string literal of length 3 to a varchar(1)) truncates dog to d.

the varchar is defaulting to length one
DECLARE #Test1 varchar;
try this, which will uses a simple function that takes a sql_variant and returns the data type info back:
CREATE FUNCTION [dbo].[yourFunction]
(
#InputStr sql_variant --can not be varchar(max) or nvarchar(max)
)
returns
varchar(8000)
BEGIN
DECLARE #Value varchar(50)
--can use SQL_VARIANT_PROPERTY(#InputStr,'BaseType') to determine given datatype
--do whatever you want with #inputStr here
IF #InputStr IS NULL
BEGIN
SET #value= 'was null'
END
ELSE IF SQL_VARIANT_PROPERTY(#InputStr,'BaseType')='varchar'
BEGIN
--your special code here
SET #value= 'varchar('+CONVERT(varchar(10),SQL_VARIANT_PROPERTY(#InputStr,'MaxLength '))+') - '+CONVERT(varchar(8000),#InputStr)
END
ELSE IF SQL_VARIANT_PROPERTY(#InputStr,'BaseType')='datetime'
BEGIN
--your special code here
SET #value= 'datetime - '+CONVERT(char(23),#InputStr,121)
END
ELSE IF SQL_VARIANT_PROPERTY(#InputStr,'BaseType')='nvarchar'
BEGIN
--your special code here
SET #value= 'nvarchar('+CONVERT(varchar(10),CONVERT(int,SQL_VARIANT_PROPERTY(#InputStr,'MaxLength '))/2)+') - '+CONVERT(varchar(8000),#InputStr)
END
ELSE
BEGIN
--your special code here
set #value= 'unknown!'
END
RETURN #value
END
GO
DECLARE #Test1 varchar;
SET #Test1 = 'dog';
DECLARE #Test2 varchar(10);
SET #Test2 = 'cat';
SELECT #Test1 AS Result1, #Test2 AS Result2;
select [dbo].[yourFunction](#test1)
output:
Result1 Result2
------- ----------
d cat
(1 row(s) affected)
-------------------
varchar(1) - d
(1 row(s) affected)
moral of the story, don't be lazy, specify a length on all of your varchar values!!!

Related

SQL Server - Hasbytes in function gives different result then in select

I created a function like this:
CREATE FUNCTION dbo.HashCheeseName (#CheeseName NVARCHAR(40))
RETURNS VARCHAR(40)
AS BEGIN
DECLARE #Salt VARCHAR(25)
DECLARE #CheeseName NVARCHAR(40)
DECLARE #output VARCHAR(40)
SET #Salt = '123abc11aa'
SET #output = HASHBYTES('SHA2_256', CAST(#CheeseName as VARCHAR(40)) + #Salt)
RETURN #output
END
;
When I just run
SELECT HASHBYTES('SHA2_256', CAST('SwissCheese' as VARCHAR(40)) + #Salt)
I get an expected result like 0xF456D41144584064AC5456B7E3...
However, when I run the function in a query
SELECT dbo.HashCheeseName('SwissCheese')
I get a result like this: h:Mó!yýŠù’p» ªu_aøP¾æhw
Any ideas on why it would result in something like this? At first sight it looks like a conversion issue, but I don't see the problem
What you are doing there isn't the same. HASHBYTES returns a varbinary (Hashbytes (Transact-SQL) - Return Values) where as your function is returning a varchar. Those are different datatypes. As a result, the last line of your function is effectively:
SET #output = CONVERT(varchar(40),HASHBYTES('SHA2_256', CAST(#CheeseName as VARCHAR(40)) + #Salt));
The varchar representation of a varbinary will not be the same. Either return a varbinary in your function, or your test SELECT with a CONVERT to a varchar(40).
Edit: To confirm, the solution is to simply change the return type of your function and variable:
CREATE FUNCTION dbo.HashCheeseName (#CheeseName NVARCHAR(40))
RETURNS varbinary(8000)
AS BEGIN
DECLARE #Salt VARCHAR(25);
DECLARE #CheeseName NVARCHAR(40);
DECLARE #output varbinary(8000) ;
SET #Salt = '123abc11aa';
SET #output = HASHBYTES('SHA2_256', CAST(#CheeseName as VARCHAR(40)) + #Salt);
RETURN #output;
END

Weird result coverting from varchar to varbinary and converting back -- sql server 2008

Here is the stored procedure:
CREATE PROCEDURE dbo.TestTestTest AS
BEGIN
DECLARE #ProcedureIdForTracking varbinary(128) = CONVERT(varbinary(128), ##procid)
DECLARE #ProcedureNameForTracking varbinary(128) = CONVERT(varbinary(128), OBJECT_NAME(##procid))
SELECT ##procid AS originalProcid, #ProcedureIdForTracking, CONVERT(bigint, #ProcedureIdForTracking) AS ConvertBackId
SELECT OBJECT_NAME(##procid) AS originalName, #ProcedureNameForTracking, CONVERT(varchar(1000),
#ProcedureNameForTracking) AS ConvertBackName
SET CONTEXT_INFO #ProcedureNameForTracking
END
I can recover the ##procid from the converting, but not the stored procedure name. Any idea?Anything wrong with the OBJECT_NAME function?
The result:
Object names are of the datatype SYSNAME (A synonym for NVARCHAR(128)), you are therefore converting from one datatype to binary then back to another, so you are not doing the reverse. You can demonstrate this fairly simply:
DECLARE #ProcName SYSNAME = 'dbo.TestTestTest'
DECLARE #VarBin VARBINARY(128) = CONVERT(VARBINARY(128), #Procname);
SELECT Inccorect = CONVERT(VARCHAR(1000), #VarBin),
Correct = CONVERT(NVARCHAR(128), #VarBin);
Which yields:
Inccorect Correct
------------------------------
d dbo.TestTestTest
Furthermore, NVARCHAR requires 2 bytes per character, so VARBINARY(128) is not long enough to store it, again, this can be demonstrated:
DECLARE #ProcName SYSNAME = REPLICATE('|', 128);
DECLARE #VarBin VARBINARY(128) = CONVERT(VARBINARY(128), #Procname);
SELECT Len1 = LEN(#ProcName),
Len2 = LEN(CONVERT(NVARCHAR(128), #VarBin));
Which gives:
Len1 Len2
-----------------
128 64
So in you would need to adjust your types and lengths:
DECLARE #ProcedureIdForTracking varbinary(128) = CONVERT(varbinary(128), ##procid)
DECLARE #ProcedureNameForTracking varbinary(256) = CONVERT(varbinary(128), OBJECT_NAME(##procid))
SELECT ##procid AS originalProcid, #ProcedureIdForTracking, CONVERT(bigint, #ProcedureIdForTracking) AS ConvertBackId
SELECT OBJECT_NAME(##procid) AS originalName, #ProcedureNameForTracking, CONVERT(nvarchar(128),
#ProcedureNameForTracking) AS ConvertBackName
SET CONTEXT_INFO #ProcedureNameForTracking

Update field so that parameter in SQL has 16 character length

I am writing a stored procedure that will dummy some credit card data (please note that this is not live!) It is for internal purposes only. This sp runs, but it is only printing out a subset of 10 character (numeric) lengths and not 16. Does anyone have any insight?
Here is my SProc:
DECLARE #RESULT int
DECLARE #cc as varchar(50) = (SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)))
UPDATE trans
SET trans_CCNUM =(SELECT stuff(#cc,1,LEN(#cc)-4,REPLICATE('x', LEN(#cc)-5)))
where LEN(trans_ccNum) = 16;
PRINT #RESULT
Here are the results
dateCreated trans_CCNUM
2014-09-11 16:55:13.800 xxxx9328
If your example above, #cc is only going to be 9 or 10 characters long...
DECLARE #cc as varchar(50) =
(SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)))
SELECT #cc,len(#cc)
select stuff(#cc,1,LEN(#cc)-4,REPLICATE('x', LEN(#cc)-5))
That is why you are only seeing 9/10 characters
Try changing the INT to BIGINT and you should be OK

TSQL UDF To Split String Every 8 Characters

Someone decided to stuff a bunch of times together into a single column, so the column value might look like this:
08:00 AM01:00 PM
And another column contains the date in the following format;
20070906
I want to write a UDF to normalize this data in a single SQL query, so I can get back 2 rows of datetime type for the above example
2007-09-06 08:00:00.000
2007-09-06 13:00:00.000
The conversion to datetime type is simple...but I need to split the time part every 8 characters to get the individual time out.
Anyone know of an existing UDF to do this?
Try this, it'll split your string into chunks of the specified lenth:
create function SplitString
(
#str varchar(max),
#length int
)
returns #Results table( Result varchar(50) )
AS
begin
declare #s varchar(50)
while len(#str) > 0
begin
set #s = left(#str, #length)
set #str = right(#str, len(#str) - #length)
insert #Results values (#s)
end
return
end
For example:
select * from dbo.SplitString('08:00 AM01:00 PM', 8)
Will give this result:
Result
08:00 AM
01:00 PM
There is a bug in the query above, the below query fixes this.
Also, I have made the returned table contain a sequence column so that it is possible to determine what sequence the split is in:
CREATE function SplitString
(
#str varchar(max),
#length int
)
RETURNS #Results TABLE( Result varchar(50),Sequence INT )
AS
BEGIN
DECLARE #Sequence INT
SET #Sequence = 1
DECLARE #s varchar(50)
WHILE len(#str) > 0
BEGIN
SET #s = left(#str, #length)
INSERT #Results VALUES (#s,#Sequence)
IF(len(#str)<#length)
BREAK
SET #str = right(#str, len(#str) - #length)
SET #Sequence = #Sequence + 1
END
RETURN
END

Is there a way to make a TSQL variable constant?

Is there a way to make a TSQL variable constant?
No, but you can create a function and hardcode it in there and use that.
Here is an example:
CREATE FUNCTION fnConstant()
RETURNS INT
AS
BEGIN
RETURN 2
END
GO
SELECT dbo.fnConstant()
One solution, offered by Jared Ko is to use pseudo-constants.
As explained in SQL Server: Variables, Parameters or Literals? Or… Constants?:
Pseudo-Constants are not variables or parameters. Instead, they're simply views with one row, and enough columns to support your constants. With these simple rules, the SQL Engine completely ignores the value of the view but still builds an execution plan based on its value. The execution plan doesn't even show a join to the view!
Create like this:
CREATE SCHEMA ShipMethod
GO
-- Each view can only have one row.
-- Create one column for each desired constant.
-- Each column is restricted to a single value.
CREATE VIEW ShipMethod.ShipMethodID AS
SELECT CAST(1 AS INT) AS [XRQ - TRUCK GROUND]
,CAST(2 AS INT) AS [ZY - EXPRESS]
,CAST(3 AS INT) AS [OVERSEAS - DELUXE]
,CAST(4 AS INT) AS [OVERNIGHT J-FAST]
,CAST(5 AS INT) AS [CARGO TRANSPORT 5]
Then use like this:
SELECT h.*
FROM Sales.SalesOrderHeader h
JOIN ShipMethod.ShipMethodID const
ON h.ShipMethodID = const.[OVERNIGHT J-FAST]
Or like this:
SELECT h.*
FROM Sales.SalesOrderHeader h
WHERE h.ShipMethodID = (SELECT TOP 1 [OVERNIGHT J-FAST] FROM ShipMethod.ShipMethodID)
My workaround to missing constans is to give hints about the value to the optimizer.
DECLARE #Constant INT = 123;
SELECT *
FROM [some_relation]
WHERE [some_attribute] = #Constant
OPTION( OPTIMIZE FOR (#Constant = 123))
This tells the query compiler to treat the variable as if it was a constant when creating the execution plan. The down side is that you have to define the value twice.
No, but good old naming conventions should be used.
declare #MY_VALUE as int
There is no built-in support for constants in T-SQL. You could use SQLMenace's approach to simulate it (though you can never be sure whether someone else has overwritten the function to return something else…), or possibly write a table containing constants, as suggested over here. Perhaps write a trigger that rolls back any changes to the ConstantValue column?
Prior to using a SQL function run the following script to see the differences in performance:
IF OBJECT_ID('fnFalse') IS NOT NULL
DROP FUNCTION fnFalse
GO
IF OBJECT_ID('fnTrue') IS NOT NULL
DROP FUNCTION fnTrue
GO
CREATE FUNCTION fnTrue() RETURNS INT WITH SCHEMABINDING
AS
BEGIN
RETURN 1
END
GO
CREATE FUNCTION fnFalse() RETURNS INT WITH SCHEMABINDING
AS
BEGIN
RETURN ~ dbo.fnTrue()
END
GO
DECLARE #TimeStart DATETIME = GETDATE()
DECLARE #Count INT = 100000
WHILE #Count > 0 BEGIN
SET #Count -= 1
DECLARE #Value BIT
SELECT #Value = dbo.fnTrue()
IF #Value = 1
SELECT #Value = dbo.fnFalse()
END
DECLARE #TimeEnd DATETIME = GETDATE()
PRINT CAST(DATEDIFF(ms, #TimeStart, #TimeEnd) AS VARCHAR) + ' elapsed, using function'
GO
DECLARE #TimeStart DATETIME = GETDATE()
DECLARE #Count INT = 100000
DECLARE #FALSE AS BIT = 0
DECLARE #TRUE AS BIT = ~ #FALSE
WHILE #Count > 0 BEGIN
SET #Count -= 1
DECLARE #Value BIT
SELECT #Value = #TRUE
IF #Value = 1
SELECT #Value = #FALSE
END
DECLARE #TimeEnd DATETIME = GETDATE()
PRINT CAST(DATEDIFF(ms, #TimeStart, #TimeEnd) AS VARCHAR) + ' elapsed, using local variable'
GO
DECLARE #TimeStart DATETIME = GETDATE()
DECLARE #Count INT = 100000
WHILE #Count > 0 BEGIN
SET #Count -= 1
DECLARE #Value BIT
SELECT #Value = 1
IF #Value = 1
SELECT #Value = 0
END
DECLARE #TimeEnd DATETIME = GETDATE()
PRINT CAST(DATEDIFF(ms, #TimeStart, #TimeEnd) AS VARCHAR) + ' elapsed, using hard coded values'
GO
If you are interested in getting optimal execution plan for a value in the variable you can use a dynamic sql code. It makes the variable constant.
DECLARE #var varchar(100) = 'some text'
DECLARE #sql varchar(MAX)
SET #sql = 'SELECT * FROM table WHERE col = '''+#var+''''
EXEC (#sql)
For enums or simple constants, a view with a single row has great performance and compile time checking / dependency tracking ( cause its a column name )
See Jared Ko's blog post https://blogs.msdn.microsoft.com/sql_server_appendix_z/2013/09/16/sql-server-variables-parameters-or-literals-or-constants/
create the view
CREATE VIEW ShipMethods AS
SELECT CAST(1 AS INT) AS [XRQ - TRUCK GROUND]
,CAST(2 AS INT) AS [ZY - EXPRESS]
,CAST(3 AS INT) AS [OVERSEAS - DELUXE]
, CAST(4 AS INT) AS [OVERNIGHT J-FAST]
,CAST(5 AS INT) AS [CARGO TRANSPORT 5]
use the view
SELECT h.*
FROM Sales.SalesOrderHeader
WHERE ShipMethodID = ( select [OVERNIGHT J-FAST] from ShipMethods )
Okay, lets see
Constants are immutable values which are known at compile time and do not change for the life of the program
that means you can never have a constant in SQL Server
declare #myvalue as int
set #myvalue = 5
set #myvalue = 10--oops we just changed it
the value just changed
Since there is no build in support for constants, my solution is very simple.
Since this is not supported:
Declare Constant #supplement int = 240
SELECT price + #supplement
FROM what_does_it_cost
I would simply convert it to
SELECT price + 240/*CONSTANT:supplement*/
FROM what_does_it_cost
Obviously, this relies on the whole thing (the value without trailing space and the comment) to be unique. Changing it is possible with a global search and replace.
There are no such thing as "creating a constant" in database literature. Constants exist as they are and often called values. One can declare a variable and assign a value (constant) to it. From a scholastic view:
DECLARE #two INT
SET #two = 2
Here #two is a variable and 2 is a value/constant.
SQLServer 2022 (currently only as Preview available) is now able to Inline the function proposed by SQLMenace, this should prevent the performance hit described by some comments.
CREATE FUNCTION fnConstant() RETURNS INT AS BEGIN RETURN 2 END GO
SELECT is_inlineable FROM sys.sql_modules WHERE [object_id]=OBJECT_ID('dbo.fnConstant');
is_inlineable
1
SELECT dbo.fnConstant()
ExecutionPlan
To test if it also uses the value coming from the Function, I added a second function returning value "1"
CREATE FUNCTION fnConstant1()
RETURNS INT
AS
BEGIN
RETURN 1
END
GO
Create Temp Table with about 500k rows with Value 1 and 4 rows with Value 2:
DROP TABLE IF EXISTS #temp ;
create table #temp (value_int INT)
DECLARE #counter INT;
SET #counter = 0
WHILE #counter <= 500000
BEGIN
INSERT INTO #temp VALUES (1);
SET #counter = #counter +1
END
SET #counter = 0
WHILE #counter <= 3
BEGIN
INSERT INTO #temp VALUES (2);
SET #counter = #counter +1
END
create index i_temp on #temp (value_int);
Using the describe plan we can see that the Optimizer expects 500k values for
select * from #temp where value_int = dbo.fnConstant1(); --Returns 500001 rows
Constant 1
and 4 rows for
select * from #temp where value_int = dbo.fnConstant(); --Returns 4rows
Constant 2
Robert's performance test is interesting. And even in late 2022, the scalar functions are much slower (by an order of magnitude) than variables or literals. A view (as suggested mbobka) is somewhere in-between when used for this same test.
That said, using a loop like that in SQL Server is not something I'd ever do, because I'd normally be operating on a whole set.
In SQL 2019, if you use schema-bound functions in a set operation, the difference is much less noticeable.
I created and populated a test table:
create table #testTable (id int identity(1, 1) primary key, value tinyint);
And changed the test so that instead of looping and changing a variable, it queries the test table and returns true or false depending on the value in the test table, e.g.:
insert #testTable(value)
select case when value > 127
then #FALSE
else #TRUE
end
from #testTable with(nolock)
I tested 5 scenarios:
hard-coded values
local variables
scalar functions
a view
a table-valued function
running the test 10 times, yielded the following results:
scenario
min
max
avg
scalar functions
233
259
240
hard-coded values
236
265
243
local variables
235
278
245
table-valued function
243
272
253
view
244
267
254
Suggesting to me, that for set-based work in (at least) 2019 and better, there's not much in it.
set nocount on;
go
-- create test data table
drop table if exists #testTable;
create table #testTable (id int identity(1, 1) primary key, value tinyint);
-- populate test data
insert #testTable (value)
select top (1000000) convert(binary (1), newid())
from sys.all_objects a
, sys.all_objects b
go
-- scalar function for True
drop function if exists fnTrue;
go
create function dbo.fnTrue() returns bit with schemabinding as
begin
return 1
end
go
-- scalar function for False
drop function if exists fnFalse;
go
create function dbo.fnFalse () returns bit with schemabinding as
begin
return 0
end
go
-- table-valued function for booleans
drop function if exists dbo.tvfBoolean;
go
create function tvfBoolean() returns table with schemabinding as
return
select convert(bit, 1) as true, convert(bit, 0) as false
go
-- view for booleans
drop view if exists dbo.viewBoolean;
go
create view dbo.viewBoolean with schemabinding as
select convert(bit, 1) as true, convert(bit, 0) as false
go
-- create table for results
drop table if exists #testResults
create table #testResults (id int identity(1,1), test int, elapsed bigint, message varchar(1000));
-- define tests
declare #tests table(testNumber int, description nvarchar(100), sql nvarchar(max))
insert #tests values
(1, N'hard-coded values', N'
declare #testTable table (id int, value bit);
insert #testTable(id, value)
select id, case when t.value > 127
then 0
else 1
end
from #testTable t')
, (2, N'local variables', N'
declare #FALSE as bit = 0
declare #TRUE as bit = 1
declare #testTable table (id int, value bit);
insert #testTable(id, value)
select id, case when t.value > 127
then #FALSE
else #TRUE
end
from #testTable t'),
(3, N'scalar functions', N'
declare #testTable table (id int, value bit);
insert #testTable(id, value)
select id, case when t.value > 127
then dbo.fnFalse()
else dbo.fnTrue()
end
from #testTable t'),
(4, N'view', N'
declare #testTable table (id int, value bit);
insert #testTable(id, value)
select id, case when value > 127
then b.false
else b.true
end
from #testTable t with(nolock), viewBoolean b'),
(5, N'table-valued function', N'
declare #testTable table (id int, value bit);
insert #testTable(id, value)
select id, case when value > 127
then b.false
else b.true
end
from #testTable with(nolock), dbo.tvfBoolean() b')
;
declare #testNumber int, #description varchar(100), #sql nvarchar(max)
declare #testRuns int = 10;
-- execute tests
while #testRuns > 0 begin
set #testRuns -= 1
declare testCursor cursor for select testNumber, description, sql from #tests;
open testCursor
fetch next from testCursor into #testNumber, #description, #sql
while ##FETCH_STATUS = 0 begin
declare #TimeStart datetime2(7) = sysdatetime();
execute sp_executesql #sql;
declare #TimeEnd datetime2(7) = sysdatetime()
insert #testResults(test, elapsed, message)
select #testNumber, datediff_big(ms, #TimeStart, #TimeEnd), #description
fetch next from testCursor into #testNumber, #description, #sql
end
close testCursor
deallocate testCursor
end
-- display results
select test, message, count(*) runs, min(elapsed) as min, max(elapsed) as max, avg(elapsed) as avg
from #testResults
group by test, message
order by avg(elapsed);
The best answer is from SQLMenace according to the requirement if that is to create a temporary constant for use within scripts, i.e. across multiple GO statements/batches.
Just create the procedure in the tempdb then you have no impact on the target database.
One practical example of this is a database create script which writes a control value at the end of the script containing the logical schema version. At the top of the file are some comments with change history etc... But in practice most developers will forget to scroll down and update the schema version at the bottom of the file.
Using the above code allows a visible schema version constant to be defined at the top before the database script (copied from the generate scripts feature of SSMS) creates the database but used at the end. This is right in the face of the developer next to the change history and other comments, so they are very likely to update it.
For example:
use tempdb
go
create function dbo.MySchemaVersion()
returns int
as
begin
return 123
end
go
use master
go
-- Big long database create script with multiple batches...
print 'Creating database schema version ' + CAST(tempdb.dbo.MySchemaVersion() as NVARCHAR) + '...'
go
-- ...
go
-- ...
go
use MyDatabase
go
-- Update schema version with constant at end (not normally possible as GO puts
-- local #variables out of scope)
insert MyConfigTable values ('SchemaVersion', tempdb.dbo.MySchemaVersion())
go
-- Clean-up
use tempdb
drop function MySchemaVersion
go

Resources