Cannot cast nvarchar to float - sql

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 ......

Related

Conversion of number not match with length of datatype in SQL Server

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

Empty string should be NULL when converting to float

Consider the following TSQL code:
declare #a nvarchar(500) = N''
select try_convert(float, #a)
The output is:
0
I need the output to be NULL.
I can do this:
declare #a nvarchar(500) = N''
select case #a
when '' then null
else try_convert(float, #a)
end
and it works just fine.
However, this is just a mock-up. In my real life scenario, instead of #a, there are over 200 NVARCHAR(500) columns, either floats or zero length strings. I need a quick way of converting zero-length strings to NULL (and everything else to float), possibly without having to build 200 separate CASE statements.
I'm not really thrilled with relying the rather inexplicable differences between try_parse() and try_convert()/try_cast(). Instead, I would go for:
try_convert(float, nullif(#a, ''))
This also has the advantage of being quite explicit in what you are trying to accomplish.
You should use Try_parse instead
declare #a nvarchar(500) = N''
select try_parse( #a as float)
returns
NULL
See working demo

Difference in the checksum SQL statements

What is the difference between the 2 different statements below? Please explain the output.
SELECT CHECKSUM(CONVERT(NVARCHAR,30))
Result : 51136012
DECLARE #AA NVARCHAR
SET #AA= CONVERT(NVARCHAR,30)
SELECT CHECKSUM(#AA)
Result: 38
The default lengths are different. In the first, it is something like 32. But when you do:
declare #aa nvarchar;
The default length is 1. So, the second is only using the first character.
In SQL Server, always use lengths with the varchar() types:
SELECT CHECKSUM(CONVERT(NVARCHAR(255), 30))
DECLARE #AA NVARCHAR(255);
SET #AA= CONVERT(NVARCHAR(255), 30);
SELECT CHECKSUM(#AA);

How to do math with GUIDs in SQL Server

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'

Error in Dynamic SQL Command which using Insert by Select Statement

I am using this procedure to select the values from different tables (each week each table) and Insert them in one temporary table and get them as output by selecting the table at the end of the procedure...
CREATE proc salessample (#tablename varchar(50), #did varchar(50))
as
begin
create table #salesdetail(sno int identity, This_Week_Left float,
This_Week_Right float, Last_Week_Left float,
Last_Week_Right float, Paid_Left float,
Paid_Right float, Paid float, orbitrate float)
DECLARE #SQLString NVARCHAR(500), #i int;
DECLARE #SQLString2 NVARCHAR(500);
set #i=1
--while(#i < 2)
--begin
set #SQLString = N'insert into #salesdetail(This_Week_Left,This_Week_Right,Last_Week_Left,Last_Week_Right,Paid_Left,Paid_Right,Paid,orbitrate) select a.This_Week_Left,a.This_Week_Right,a.Last_Week_Left,a.Last_Week_Right,a.Paid_Left,a.Paid_Right,a.Paid,b.orbitrate from Power.Week_'+#tablename +'_table a, Power.Orbit_Rates b where a.m_distributor_id ='+#did+' and b.week_details ='+convert(numeric(18),#tablename);
exec sp_executesql #SQLString
set #i =#i + 1
--end
select * from #salesdetail
end
Thanks in advance.
What is the error message that you are getting. You question is impossible to answer in its current form.
As far as i can see it could be one of the following potential problems:
conversioons between unicode (nvarchar) and non-unicode (varchar)
Converting to a number and then appending to a string:
... and b.week_details ='+convert(numeric(18),#tablename);
Converting to numeric(18)
Some other insertion error that I can't see
Break the dynamic SQL statement down into parts, and view it before attempting to exec it. I.e. print or select the string. You can the copy it and run it standalone to see if there are any problems.
EDIT
From your comment below, I can see that your problem is that you are converting a string to a number and then attempting to append it to a string. Try this instead:
... and b.week_details ='+ #tablename
Thanks U all...... For all answers and comments.....
I got Cleared My error..
I forgot to give single quotes for where clause data in Dyanmic command...
like this ....
''' Text ''' = 'Text'....
the reason Why I got error was it accepts the parameter and executes in the procedure.
but not for all values. I gave table name as input .
Now its Working . Thank s Once again