Use of Between operator in the context of varchar values - sql

In an sql where condition there is a value like below
select * from tabl
where
code not between 'A00' and 'B99'
how does this work? till now i was thinking it works only with numeric and date

The same way as ORDER BY works on varchar values.
Every char has its ASCII code and sort order, so every value can be compared by its ASCII code.

The BETWEEN operator will treat these as varchars, and apply its comparison rules accordingly:
BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression.
SQL uses a list of the ASCII codes to determine which to sort where
and in what order.

Related

How to remove trailing zeroes in snowflake?

I have a column that is in INT type. I want to remove all the trailing zeroes and only have the number. The example below, should follow be: 3,10,20,20. I cannot have the 4 zeroes at the end.
Is there a way to do this?
You could try casting your numeric data to integer, e.g.
SELECT AS_INTEGER(col) AS col
FROM yourTable;
the 3dp is how floating point numbers print, thus you can cast to number SELECT column_name::number FROM table
If your data is a string/varient type, and not all values stored in your column always casts cleanly, in Snowflake you can hit error, with the TO_NUMBER or ::number forms, thus [TRY_TO_NUMBER][2] form should be used.

How to format a SSRS field with 2-digits of procession when the data is a mix of formats

I have data from a Stored Procedure that looks like this and needs to display on my report with 2-digits of precision. The data is all NVARCHAR but there is a mix of rows that looks numeric or says 'Max'.
How do I display only 2 digits of precision? I tried the following which works great on the numeric looking data but errors on 'Max'.
=FormatNumber(Fields!TBS.Value, 2)
Use an IIF to determine if the value is a number and IIF so, format it.
=IIF(ISNUMERIC(Fields!TBS.Value), FormatNumber(Fields!TBS.Value, 2), Fields!TBS.Value)
IsNumeric:
Returns a Boolean value indicating whether an expression can be
evaluated as a number.
MS SSRS Description

How to retrieve the column with mandatory decimal point in Sql 2008?

I am retrieving a numeric value from one of the column in Sql. However I have to retrieve it by three decimal point (eg below).
Though if the value in the table is in the decimal point, then its giving the result as expected. But in case the value is not in decimal format and only an integer it must display .000
Below is the example.
Value in table= 2
Retrieval Value = 2
Required Value = 2.000
As per your requirement convert your incoming value in Decimal(18,3). like given below,
select convert(decimal(18,3),2)
To really show a value in a particular format, you can convert the value to a string. One method uses the str() function:
str(x, 18, 3)
Note that you can also cast() the decimal:
select cast(cast(x as decimal(18, 3)) as varchar(19))

Oracle - select * where column>5

I am trying to do a comparison based on a column. Let's say, if column>5.
select * where column>5
The column contains non digits. I thought Oracle allows one to compare strings (like Java).
Apparently this is not allowed.
ORA-01722: invalid number
01722. 00000 - "invalid number"
Is there a way to do comparisons with non numeric fields?
Thanks
Yes, you have to put the 5 in quotes :
select * from table where column > '5'
To shed a bit more light on why it doesn't work.
When using a number literal 5 instead of a character literal '5' Oracle does an implicit data type conversion and tries to convert all values in your table to a number. That's why you get that error.
You should never rely on implicit data type conversion. That is bound to give you trouble sometime.
Now if you correctly compare a character literal ('5') against a character column, no data type conversion is needed and no error occurs.
However: if you expect Oracle to actually do a numeric comparison then you are mistaken. Character string are based on ASCII values. Therefor the (character) value '10' is lower than the (character) value '2' because the first character '1' is ranked lower than '2'.
If the column is varchar2, then this:
select * from some_table where some_column > 5
... does an implicit conversion of all the column values to numbers, so you're really doing:
select * from some_table where to_number(some_column) > 5
It's the to_number() that's causing the ORA-01722, even though you can't see it, when it hits a value that is not numeric. The function being called on the column value also stops any index being used (oversimplifying a bit).
You can stop it failing, and let it use the index if there is one, by doing where some_column > '5' as other have said, or where some_column > to_char(5). But you need to be careful doing the comparison as it will still be a string comparison, not a numeric one; so '10' will not be seen as > '5'; and your NLS sorting parameters might produce results you aren't expecting. Or more importantly, someone else's NLS parameters - when you put this live for example - might product results you aren't expecting and which don't match the ones you got in your environment. See the documentation for more.
You should use number columns to hold numeric values, date columns to hold dates, etc., and varchar2 only to hold text values.
you can use to_char function
select * from table where column > to_char(5)
You are missing the table:
select * where FROM tablename column>5
But this only works if column is a number. If not, you can't use >.
To compare strings, you can use LIKE or STRCMP(), check examples of them HERE.
As stated by #Gerrat, you can also use > and < but the types of both sides must be compatible (number with number or text with text). To find more about it, check THIS.
Be aware that in text comparison it will compare each character individually so '11' will be < that '2'.

Select all rows where a varchar column converts to a decimal

I have a varchar column that has generally has a decimal value, but some times there is some garbage text characters in that field.
Is it possible to filter in the WHERE clause for rows that sucessfully convert to a decimal value?
I am using sql-server 2005
One way is the ISNUMERIC function:
select * from YourTable where ISNUMERIC(col1) = 1
There's one gotcha: isnumeric returns 1 whenever a string can be converted to any numeric type, including money. For example, say you have rows using varying decimal separators, like 7.9 and 7,9. Both will convert to money, and isnumeric returns 1 for both of them. But only one converts to decimal, depending on the SQL Server language settings.