I need to do some math with SQL Server GUIDs in a trigger and I'm having difficulty figuring out how to convert a uniqueidentifier to a numeric(38,0).
One potential problem: my understand is that both of these datatypes are 16-byte "integers". If I'm wrong here, please correct me.
Otherwise, how would I go about this conversion? I've tried CAST and CONVERT and keep getting Explicit conversion from data type uniqueidentifier to numeric is not allowed. as an error message whenever I try. I'd really like to not have to parse each character and do hex math in a UDF to do this.
Is this possible?
Here's my script to repro this real quick:
DECLARE #guid uniqueidentifier
SET #guid = NEWID()
DECLARE #a numeric(38,0)
SET #a = 2
PRINT CAST(#guid AS numeric(38,0)) -- fails
PRINT #guid / #a -- also fails
Unfortunately, I haven't stumbled on a conversion from a hexadecimal value in a VARCHAR to a NUMERIC short of looping through one digit at a time.
declare #GUID as UniqueIdentifier = NewId()
declare #Binary as VarBinary(64) = #GUID
declare #String as VarChar(64) = Convert( VarChar(64), #Binary, 2 )
select #GUID as 'GUID', #Binary as 'Binary', #String as 'String'
Related
I have a VARCHAR which will return the value something like this '40/4'.
Is there a way to convert this VARCHAR to an INT? I tried casting but it is not working.
Sample:
Declare #test varchar(6) = '40/4'
Select cast (#test as int) * 4
Expected: 40
Actual:
Conversion failed when converting the varchar value '40/4' to data type int
Note: The value 40/4 is something coming from a message saved in one of the systems and that cannot be changed.
Appreciate any help on this.
CAST/CONVERT won't work because '40/4' is not a number, its a string that happens to represent a mathematical expression.
One way to solve this is by using dynamic SQL e.g.
declare #test varchar(6) = '40/4';
declare #sql nvarchar(max) = 'select ' + #test + ' * 4';
exec sp_executesql #sql;
Returns: 40
If you're just after the first valid integer value before the / (or just the first non-numeric character) you can try
Declare #test varchar(6) = '40/4'
Select Try_Convert(int,Left(#test,IsNull(NullIf(PatIndex('%[^0-9]%',#test),0),6)-1))
I refer the following number with datatype bigInt.
DECLARE #Number Bigint = '269687584'
When I convert the datatype to Varbinary(4), it returns 0x10131B20.
But when I calculate the length of the Varbinary data, it returns only 3.
I didn't know exact what wrong.
In SQL Server database, I want to write an query to convert Bigint to
Varbinary(4), but is converted as only as Varbinary(3).
Here is what I have tried:
DECLARE #Number BIGINT = '269687584'
SELECT
#Number,
CONVERT(VARBINARY(4), #Number),
LEN(CONVERT(VARBINARY(4), #Number))
Use DATALENGTH instead:
DECLARE #Number BIGINT ='269687584'
SELECT #Number, CONVERT(VARBINARY(4), #Number), DATALENGTH(CONVERT(VARBINARY(4), #Number))
There is nothing wrong. You should check lenght using datalength:
DECLARE #Number Bigint ='269687584'
SELECT #Number, CAST(#Number AS Varbinary(4)), DATALENGTH(CAST(#Number AS Varbinary(4)))
Simple question - why when I print the value of the #len variable in the query below would I be getting the value 1, instead of 12 (the number of characters in the specified string)?
DECLARE #string varchar
DECLARE #index int
DECLARE #len int
DECLARE #char char(1)
SET #string = 'content loop'
SET #index = 1
SET #len= LEN(#string)
print #len
Your declaration of #string is wrong. You have no length on the varchar.
Try this:
declare #string varchar(255); -- or whatever
You just learned that the default in this situation is 1.
This is clearly specified in the documentation. As a further note, MS SQL seems to make this rather complicated:
When n is not specified in a data definition or variable declaration
statement, the default length is 1. When n is not specified when using
the CAST and CONVERT functions, the default length is 30.
The right habit is to always include the length when using varchar or nvarchar.
You need to give the variable #string an actual length. Print the variable #string and it will probably return 'C'.
Becvause varChar without a length specification is taken as varChar(1)
replace varchar with varChar(30) or varChar(max)
I have a weird problem.
In my query I select 5 columns which two of them are nvarchar in the form of numeric (only contains number and precision point), and other three are uniqueIdentifier and nvarchar.
I want to cast these two columns to Float in my select statement but I'm getting an error
Cannot cast nvarchar to float.
I checked the format of all these values many many times. Trust me, they are fine.
But when I just select these two columns and cast them to float the query runs successfully.
I appreciate any help on this subject.
I can paste the query here too. but the whole query is more then 100 lines and this will be frustrating to write and read!
Definitely going to need more info from you before answering. Can you post some of your code (or a small reproduction of your issue)? Perhaps your table definition as well?
Since all of the values are numeric, why leave the column as an nvarchar?
Finally, does any of your data contain a dollar sign ($)?
This works:
DECLARE #Text nvarchar(100)
SET #Text = '1234.567'
SELECT CONVERT(float, #Text) AS ColumnValue
So does this:
DECLARE #Text nvarchar(100)
SET #Text = ' 1234.567 '
SELECT CONVERT(float, #Text) AS ColumnValue
But this does not:
DECLARE #Text nvarchar(100)
SET #Text = '$1234.567'
SELECT CONVERT(float, #Text) AS ColumnValue
Not sure without seeing your query, but I think this should work:
SELECT CONVERT(Float(2), LTRIM(RTRIM(<nVarchar Column>))) AS Amount FROM ......
This is a follow up to my previous question, in t-sql
SELECT SCOPE_IDENTITY()
returns a BIGINT, I did the following to get it to return an INT:
DECLARE #X INT
INSERT ...
SELECT #X = SCOPE_IDENTITY()
-- if i don't include the line below, it will return a BIGINT
SELECT #X
Why does it return a BIGINT unless I do SELECT #X at the end?
p.s. turns out
SELECT #X = SCOPE_IDENTITY()
doesn't return anything, it just sets #x
The statement
SELECT #X = SCOPE_IDENTITY()
is an assignment statement. As in most programming languages, an assignment statement is executed by first evaluating the right hand side. In this case the right hand side evaluates to a bigint. When the value of #X gets the resulting bigint, there is an implicit type conversion, because #X is a different type (int) than the value it's receiving.
SQL is a typed language, and the type of an expression (such as SCOPE_IDENTITY() here) depends on the expression, not on what happens to the expression's value after evaluation.
Analogy:
DECLARE #i INT;
SET #i = 3.2 + 0.2;
You wouldn't suggest that 3.2 + 0.2 is an integer, would you? It's 3.4, a decimal. Only because of the assignment is there an implicit conversion to INT.
There's no magic in most programming languages.
SELECT SCOPE_IDENTITY()
returns a BIGINT as you have seen
SELECT #X = SCOPE_IDENTITY()
returns BIGINT and casts it into INT variable #X
So you are returning BIGINT SCOPE_IDENTITY and also concurrently casting it to INT and setting that result to #X.
Returning #X returns the INT result.
Just some interesting reading on the subject.
SQL Server 7.0 Books Online also
stated: "It is recommended that SET
#local_variable be used for variable
assignment rather than SELECT
#local_variable."