SQL Select range for nvarchar column - sql

How do I select from tables where the column_a in range 55 - 42000.
Note : column_a's datatype is in nvarchar
I have try like this, but no luck
SELECT
SoftwareName, SoftwareImageTeaser, SoftwarePrice, SoftwareDescription
FROM
View_Software_Listing_With_Category
WHERE
(SoftwarePrice >= '55' AND SoftwarePrice <= '42000')

SELECT SoftwareName,SoftwareImageTeaser,SoftwarePrice,SoftwareDescription
FROM View_Software_Listing_With_Category
WHERE (CAST(CAST(SoftwarePrice AS FLOAT) AS INT) BETWEEN 55 AND 42000

Try this,As your SoftwarePrice column is nvarchar, you need to convert it in Integer for comparing with integer values. Then you can use between clause for filtering records.
SELECT SoftwareName,SoftwareImageTeaser,SoftwarePrice,SoftwareDescription
FROM View_Software_Listing_With_Category
WHERE Cast(SoftwarePrice as Int) between 55 AND 42000

Related

How to convert to decimal when data is number but not convert when data is text?

I am using SQL Server 2008 r2.
There are 31 fields (D1-D31) as varchar and keep 2 format data as number and text such as 'SD' and '123.456'.
I use the command for Data Base Expert of Crystal Report like this.
select
case
when D1 in('SD') then d1
when D1 is NULL then ''
else convert(decimal(7,2),d1)
end
.
.
.
case D2-D31
from Rec2019 where ID ='P0009'
It's show Error converting data type varchar to numeric.
How to fix it ? please..
A column has one datatype so you cannot place DECIMAL and VARCHAR values inside same column. I think you want to round numeric-ish values. In that case you can use the following:
CASE
WHEN ISNUMERIC(d1) = 1 AND d1 NOT IN ('+', '-', '$') THEN CAST(CAST(d1 AS DECIMAL(7, 2)) AS VARCHAR(9))
WHEN d1 IS NULL THEN ''
ELSE d1
END
Demo on db<>fiddle
For SQL Server 2008, you can check using ISNUMERIC, where as SQL Server 2012+ have TRY_CONVERT or TRY_CAST to make it simple like following.
TRY_CONVERT(decimal(7,2), D1)
Since ISNUMERIC had some small downsides, for instance with scientific notation of numbers, I would use TRY_CAST, like this: (note: SQLServer 2012+)
CREATE TABLE #T (ColA VARCHAR (10))
INSERT INTO #T VALUES ('1'), ('A'), ('2');
SELECT *, TRY_CAST (ColA AS DECIMAL (4,2)) AS Numb
FROM #T
Sidenote: One column has one data-type, you cannot combine datatypes in one column in SQL Server.
To convert when the data is number, and not convert when the data is text, you can check the type with the isnumeric function.
select
case when isnumeric(D1) then convert(decimal(7,2), D1) else null end as D1_asnumeric
from XXX

How to add a column in a select statement and use ir in the where clause

I'm trying to execute a select statement in my database but it keeps showing me this message:
Conversion failed when converting the varchar value 'Edad' to data
type int.
The statement is the following:
select nroSocio, categoriaId, convert(int,DATEDIFF(d, (FechaNacimiento), getdate())/365.25) as 'Age'
from Socios
Where ('Age'< 16 and categoriaId='711141DB-2D9C-E411-941D-00155DDA4202')
or ('Age' > 14 and categoriaId='6F1141DB-2D9C-E411-941D-00155DDA4202')
I've tried using CONVERT(INT,'Age') but the error message is the same. Also, I tried with 'Age' > '14', this way the query is executed but it doesn't work okay, it's seems that 'Age' > '14' is always true.
I'm using sql server 2012 by the way.
'Age'< 16 is comparing the string literal "Age" to a number, which is nonsensical.
In addition, you can't reference computed columns in a where clause, because the where clause is evaluated before the computed column.
You could do a subquery:
select * FROM
(
SELECT
nroSocio,
categoriaId,
convert(int,DATEDIFF(d, (FechaNacimiento), getdate())/365.25) as [Age]
from Socios
) x
Where (Age < 16 and categoriaId='711141DB-2D9C-E411-941D-00155DDA4202')
or (Age > 14 and categoriaId='6F1141DB-2D9C-E411-941D-00155DDA4202')
use this derived table in your query
Select * from (
select
nroSocio,
categoriaId,
DATEDIFF(yy, FechaNacimiento, getdate()) as Age
from Socios ) t
Where (Age< 16 and categoriaId='711141DB-2D9C-E411-941D-00155DDA4202')
or (Age > 14 and categoriaId='6F1141DB-2D9C-E411-941D-00155DDA4202')

Convert Varchar with Exponent to Decimal Data type

I have column Amount with 10,000 records and its look like this.
1.24E4
27.27E1
3.25E2
and etc.
Now I need to update the column to make this result
12400
272.7
325
I need to know how to convert and update the column from varchar to decimal.
Take note that my Column Amount is in Varchar Data type.
select convert(float, '1.24E4')
select convert(float, '27.27E1')
select convert(float, '3.25E2')
Simply update with a cast to FLOAT:
UPDATE YOUR_TABLE
SET YOUR_COLUMN = CAST(YOUR_COLUMN AS FLOAT)
WHERE....your condition if there

Problems with relational operators in a varchar field in sqlite

I have this syntax sql who should pick up all the records in the table where the 'stock_ quantity' (Varchar Column) column is less than the 'minimum_amount' (Varchar Column) column:
SELECT * FROM fazerem_xius WHERE stock_quantity < minimum_amount ORDER
BY name_products ASC
My syntax have a problem, if the column 'stock_quantity' stores the value 10, and column 'minimum_amount' stores the value 3, This record has been returned by the database, why is this happening and how to solve this type of problem?
Convert the string to a number before comparing. For instance with a mathematical operation
SELECT * FROM fazerem_xius
WHERE stock_quantity * 1 < minimum_amount * 1
ORDER BY name_products ASC
It's because string values are compared alphabetically. When you compare the string 10 to the string 3, SQLite checks the first characters first. 1 is before 3 in the alphabet, that's why the result of '10' < '3' is true.
Use CAST() to cast them to numbers:
SELECT * FROM fazerem_xius WHERE CAST(stock_quantity AS INTEGER) < CAST(minimum_amount AS INTEGER) ORDER BY name_products ASC

SQL, How to convert VARCHAR to bigint?

I have a field that is VARCHAR(6) I am trying to insert it into another table of type bigint it is giving me an error
(Error Converting from data type varchar to bigint
here is what i am doing
CONVERT(bigint, seconds) as seconds
Can anybody help with this issue?
This is the answer
(CASE
WHEN
(isnumeric(ts.TimeInSeconds) = 1)
THEN
CAST(ts.TimeInSeconds AS bigint)
ELSE
0
END) AS seconds
an alternative would be to do something like:
SELECT
CAST(P0.seconds as bigint) as seconds
FROM
(
SELECT
seconds
FROM
TableName
WHERE
ISNUMERIC(seconds) = 1
) P0
I think your code is right. If you run the following code it converts the string '60' which is treated as varchar and it returns integer 60, if there is integer containing string in second it works.
select CONVERT(bigint,'60') as seconds
and it returns
60