Show data where the value is after the decimal point - sql

I would like to do (select value from table where...) where the value of a given experiment is not an integer, but it is decimal

You can use FLOOR function to do this. It will round up your numbers, so you can pick only this that are not integers.
create table #t (i decimal(12,6))
insert into #t values (1), (1.1)
select * from #t where FLOOR(i) <> i

You can do as
CREATE TABLE T( Val DECIMAL(10, 2));
INSERT INTO T VALUES
(10.10), (10);
SELECT *
FROM T
WHERE CAST(Val AS INT) <> Val
Returns: 10.10

Related

SQL query should return percentage but returns error

I have the following lines of code:
ROUND((((SUM(VALOR_2)) - SQLTMP.VALOR_1) / SQLTMP.VALOR_1) * 100, 2)
I was hoping it would return a percentage, but it returns an ERROR instead... Any ideas on what's wrong?
Depending on your inputs you can try addapting this solution :
CREATE TABLE #TMP (
val1 int,
val2 int
);
INSERT INTO #TMP
VALUES (1,2),(1,3),(1,4),(2,5),(2,6)
GO
-- Your code begins here
WITH tmp_table AS (
SELECT
val1 AS val1,
SUM(COLAESCE(val2, 0)) AS sum_val2
FROM #TMP
GROUP BY val1,val2
)
SELECT ROUND((sum_val2 - val1)/val1,2) FROM tmp_table;
-- Your code ends here
GO
DROP TABLE #TMP
select
round(v1/(v2*1.0), 2) as pct
from table
-- multiplying by 1.0 converts the int to decimal, round limits it to 2 places.

Amount Calculation using Precision

I have a salary table which has Amount and Amount Precision columns.
Using precision I want to get the actual amount.
Please help me to calculate actual amount using precision.
use POWER(). The multiplication with 1.0 is required to convert your Amount in integer into decimal
ActualAmount = Amount * 1.0 / Power(10, AmountPrecision)
You can use the following query:
select CAST(Amount as double precision) / power(10, AmountPrecision) from AmountTest
Assuming AmountTest is the name of the table. You can replace it with the name given by you.
DECLARE #T TABLE (
Amount INT,
AmountPrecision INT
)
INSERT INTO #T VALUES(1,1),(51,1),(51,2),(934,3),(1024,2)
SELECT
*,
CAST(AMOUNT AS FLOAT)/(CONCAT(1,REPLICATE(0,AMOUNTPRECISION))) AS ACTUALAMOUNT
FROM #T
You can also try the divide by just creating value using REPLICATE() function.
CREATE TABLE Data (id int, AmountPrecision int)
INSERT INTO Data (id, AmountPrecision) VALUES
(1, 1)
, (51, 1)
, (51, 2)
, (934, 3)
, (1024, 2)
Select id
, Cast('1' + REPLICATE('0', AmountPrecision) as int) as DivideBy
, Cast(id * 1.0 / Cast('1' + REPLICATE('0', AmountPrecision) as int)
as float) as FormattedNumber
from Data
Live db<>fiddle demo.

Convert a letter into a number

I am building the back end of a web application which is processing a significant portion of data and the front end developers are looking for a stable integer code to use in joining data.
The current integer values they are trying to use are surrogate keys which will change going forward leading to a number of problems.
Each table has a alphanumeric code and I am looking for a way in which I could convert this into a stable int.
EG convert a code 'AAAA' into 1111 or MMMM into 13131313
Could anyone tell me if this is at all possible.
Thanks,
McNets' comment seems to be a very good approach...
If you can be sure, that you have
plain ASCII characters
Not more than 4 letters
You might cast the string to VARBINARY(4) and cast this to INT:
DECLARE #dummy TABLE(StrangeCode VARCHAR(10));
INSERT INTO #dummy VALUES
('AAAA'),('MMMM'),('ACAC'),('CDEF'),('ABCD');
SELECT CAST(CAST(StrangeCode AS VARBINARY(4)) AS INT)
FROM #dummy;
The result
1094795585
1296911693
1094926659
1128547654
1094861636
If you need bigger number, you might go up to BIGINT
A way is using CTE like this:
;with tt(i, c1, c2) as (
select 1, c, replace(c,char(65), 1)
from yourTable
union all
select i+1, c1, c2= replace(c2,char(65+i), i+1)
from tt
where i < 26
)
select c1, cast(c2 as bigint) num
from tt
where i = 26;
As McNets suggests, create a second table:
create table IntCodes (id INT IDENTITY(1,1), UserCode VARCHAR(50) NOT NULL)
insert into IntCodes (UserCode)
select distinct UserCode
from MyTable
You'll need a trigger:
create trigger Trg_UserCode
on MyTable
after insert as
begin
insert into IntCodes (UserCode)
select i1.UserCode
from INSERTED i1
where i1.UserCode not in (select ic.Usercode from IntCodes ic)
end
Now, as part of the query:
select t1.*, t2.id as IntCode
from MyTable t1
inner join IntCodes t2
on t1.UserCode = t2.UserCode
This means that you won't need to worry about updating the existing columns

how to pick only last string

I have data like this I have seen functions and Substring and LEFT ,RIGHT also
but it is not serving my purpose
declare #t table (val varchar(50))
INSERT INTO #t(val)values ('E-001GHDEM120ENDORSEMENT'),
('E-001GHDEM120Renewal'),
('E-001GHDEM120Adjustment'),
('E-001GHDEM120ENDORSEMENT')
select * from #t
output
ENDORSEMENT
Renewal
Adjustment
ENDORSEMENT
I need to use that statement in where condition to filter records
Try this select
DECLARE #t TABLE
(
val VARCHAR(50)
);
INSERT INTO #t
(val
)
VALUES
('E-001GHDEM120ENDORSEMENT'
),
('E-001GHDEM120Renewal'
),
('E-001GHDEM120Adjustment'
),
('E-001GHDEM120ENDORSEMENT'
);
SELECT REVERSE(SUBSTRING(REVERSE(val), 0, PATINDEX('%[^a-zA-Z]%', REVERSE(val)))) AS val
FROM #t;
Try This. From your example here is what i understood.
select right(val,patindex('%[0-9]%', reverse(val))-1)
from #t

Is there a way to return more than 1 row in select without using existing tables

Simple question, just out of curiosity.
For example select 1,2,3 that will show table with one column and three rows.
Something like this: select values(1),(2),(3)
*with one select statement
An example for my comment in your post.
DECLARE #TABLE TABLE (ONE INT, TWO INT, THREE INT)
INSERT INTO #TABLE VALUES (1,2,3)
SELECT UP.COL, UP.VALUE
FROM #TABLE
UNPIVOT (VALUE FOR COL IN (ONE,TWO,THREE)) UP
Query:
DECLARE #t TABLE (i1 INT, i2 INT, i3 INT)
INSERT INTO #t VALUES (1, 2, 3)
SELECT t.*
FROM #t
CROSS APPLY (
VALUES(i1), (i2), (i3)
) t(value)
Output:
value
-----------
1
2
3
Additional info:
http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html
As it appears there is a simple code that I've been searching for:
select n from (values (1),(2),(3)) D(c);