Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am trying to count the number of records in the Face column that is between 0 and 25000. The dataset has Face as a char(20) so I needed to convert it to an integer before making any boolean expression. Here's my code below:
SELECT count(*)
FROM tablename
WHERE cast(face AS integer) > 0 and cast(face AS integer) <=25000;
I get an error, saying
Conversion failed when converting the varchar value '0.0' to data type int.
Face column contains null values and records like '0.0' and '0.0000'
I've looked online and haven't found anything useful on how to convert varchar to integer. Help anyone?
If your column has decimal points in it, your mistake is trying to interpret it as an integer:
select count(*)
from tablename
where cast(face AS float) > 0.0 and cast(face AS float) <= 25000.0
Is Face always guaranteed to be numeric? Hopefully so or any approach won't work very well for you.
Since you have decimal values, your best bet is to cast it as a float and then do your comparison:
SELECT count(*)
FROM tablename
WHERE cast(face AS float)> 0 and cast(face AS float) <=25000;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have a column with a varchar(255) format.
The column has number values and NULL values.
I need to convert the column to an integer format, but it fails because of the NULL values.
I have looked it up online and this issue is stated many places. But I can't seem to find the right solution. The solution has to work in a Microsoft SQL server enviroment.
I have tried this:
CAST(varcharname AS INT) AS varname
CAST(CASE WHEN varcharname IS NOT NULL THEN varcharname ELSE NULL END AS INT) AS varname
CAST(NULLIF(varcharname , '') AS INT) AS varname
CAST(NULLIF(varcharname , NULL) AS INT) AS varname
The problem is not the null values, and I can prove it:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c6734d0967eae022b0af2ee3a2b694db
More likely there is something else in the column that fails the conversion. Perhaps some whitespace? Remember that an empty string is not the same as NULL, and a string with only whitespace is not the same as an empty string.
Try this:
cast(nullif(rtrim(varcharname),'') as int)
One other thing to consider is if there is more data in the column and you are restricting results with a WHERE clause.
For example, let's say you have this data:
id
varcharname
1
'1'
2
NULL
3
'3'
4
'4'
5
'Five'
You may have a query with WHERE clause condition like this:
SELECT cast(varcharname as int) WHERE id <> 5
hoping to get results like this:
~
1
NULL
3
4
This solution will often (not always!) still fail, because the database may decide it's more efficient to apply the cast before the WHERE clause.
In this case (or if the earlier solution don't work) you can use TRY_CAST().
However, rather than use TRY_CAST() to merely smooth over your errors, first look to find out which rows/values are causing the errors (so you can fix them, and also fix whatever upstream process saved the bad data):
SELECT [id], varcharname
FROM [MyTable]
WHERE TRY_CAST(varcharname as int) IS NULL and varcharname IS NOT NULL
Finally, remember it's very poor practice to use varchar columns to store number (or date) data in the first place, to the point I consider such schemas to be broken.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Why does the first code throw an error, but the second code block does not?
DECLARE #Decimal DECIMAL(4, 4)
SELECT #Decimal = 1.98761
SELECT #Decimal
GO
DECLARE #Decimal DECIMAL(5, 4)
SELECT #Decimal = 1.98761
SELECT #Decimal
You are misinterpreting precision and scale.
DECIMAL(4,4) has four digits of precision and . . . they are all to the right of the decimal place. So, it can contain values between 0.0000 and 0.9999.
The value 1.98761 exceeds that range. Hence, an error.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am getting Divide by zero error for the following statement.
select ((((57151130.0000000000)+(57612530.0000000000))/2)/((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000)))*366
The fact that you are supplying literal values is a little odd, however, one method of avoiding a divide by 0 error is using NULLIF (Transact-SQL).
NULLIF takes 2 parameters. If the values of the 2 expressions for the parameters are the same value, then NULLIF returns NULL, otherwise it returns the value of the first expression. For example (in literal terms):
SELECT NULLIF(1,0), NULLIF('a' + 'b' + 'c','abc');
This returns the values 1 and NULL. For your query, the format would therefore be:
SELECT (((57151130.0000000000+57612530.0000000000)/2)/NULLIF(12548020.0000000000-34321580.0000000000+21773560.0000000000,0))*366
Note I have removed several of the parenthesis as there's no need to wrap every value/expression in a pair. Doing so will likely lower readability.
Then, if the expression under the divisor has the value 0, the value NULL will be returned instead. Considering that NULL represents an unknown value, and {expr}/0 is certainly an unknown value as well, the value would be the most appropriate.
If you then need to represent the NULL value in a particular way, you can wrap the whole expression in a further ISNULL.
In addition, you can use CASE to check zero values statement:
select (
(((57151130.0000000000)+(57612530.0000000000))/2)
/
CASE WHEN((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000)) = 0 THEN NULL
ELSE ((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000))
END
)*366
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
How to write unsigned right shift operator in sql server? The expression is like value >>> 0
Here is the e.g. -5381>>>0 = 4294961915
T-SQL has no bit-shift operators, so you'd have to implement one yourself. There's an implementation of a bitwise shifts here: http://dataeducation.com/bitmask-handling-part-4-left-shift-and-right-shift/
You'd have to cast your integer to a varbinary, use the bitwise shift function and cast back to integer and (hopefully) hey-presto! There's your result you're expecting.
Implementation and testing is left as an exercise for the reader...
Edit - To try to clarify what I have put in the comments below, executing this SQL will demonstrate the different results given by the various CASTs:
SELECT -5381 AS Signed_Integer,
cast(-5381 AS varbinary) AS Binary_Representation_of_Signed_Integer,
cast(cast(-5381 AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Big_Integer,
cast(cast(-5381 AS varbinary) AS bigint) AS Signed_Integer_Transposed_onto_Big_Integer,
cast(cast(cast(-5381 AS varbinary) AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Integer_Trasposed_onto_Big_Integer
Results:
Signed_Integer Binary_Representation_of_Signed_Integer Binary_Representation_of_Signed_Big_Integer Signed_Integer_Transposed_onto_Big_Integer Binary_Representation_of_Signed_Integer_Trasposed_onto_Big_Integer
-------------- -------------------------------------------------------------- -------------------------------------------------------------- ------------------------------------------ ------------------------------------------------------------------
-5381 0xFFFFEAFB 0xFFFFFFFFFFFFEAFB 4294961915 0x00000000FFFFEAFB
SQL Server does not support unsigned integers, so your value doesn't fit in the range of INT.
You can get a true binary interpretation by:
SELECT CAST(-5381 AS VARBINARY)
If you're happy to work within the confines of floating point arithmetic, you could do:
SELECT -5381.0 + (POWER(2, 30) * 4.0)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am querying part numbers from an Oracle (JDE) database. If I use the clause WHERE Item LIKE 'AS-%', it correctly returns all the items that begin with 'AS-'. However, when I try to narrow that set by instead using the clause WHERE Item LIKE 'AS-%A' in order to find all parts matching the pattern and ending with an 'A', I get no results, even though they do exist!
What gives?
When you think that your query is misidentifying rows based on your understanding of the rows' values, examine the values using the DUMP() function.
This will tell you the exact contents of the cell, including any characters that you cannot see on the display.
I doubt if there is some space or non printable character present at the end of column values.
'AS-%A' must work if value actualy start with AS- and ends with A
To check, try to query using trim(Item) like 'AS-%A'
Perhaps some control characters at end of the data that you cannot see. Try using regexp_like:
with x as (
select 'AS-123B' as col from dual
UNION
select 'AS-456A' as col from dual
UNION
select 'AS-789A' || chr(0) as col from dual
)
select * from x
where regexp_like (col, 'AS-(.*)A[[:cntrl:]]?')
Output:
COL
AS-456A
AS-789A