Take thousand value in SQL - sql

I have values like below I need to take only the thousand value in sql.
38,635.123
90,232.89
123,456.47888
I need to take result as below.
635
232
456

SELECT CONVERT(INT,YourColumn) % 1000
FROM dbo.YourTable

Cast it as an int so that we not only drop the decimal places off, but also ensure integer division takes place:
SELECT CAST(YourColumn as int) % 1000
The % operator (modulo) essentially divides the left side by the right side and returns the remainder. So, if we divide 123,456 by 1000, using integer division, the result would be 123 with a remainder of 456. Using the % operator, we just get the 456 part returned.

Another method:
select right(cast(11.500 as int), 3) --> 11
select right(cast(38635.123 as int), 3) --> 635

Assuming your value is numeric, you can use modulo arithmetic:
select cast(col as int) % 1000
(You can also use bigint or a decimal value if an integer is not big enough to hold the value.)
If it is not numeric, then you can use string operations to get the three digits before the period or after the comma (assuming one or the other is always present).

Try this query
SELECT RIGHT(ROUND(REPLACE(YourColumn ,',','') ,0,-1),3)

Related

SQL Query to compare the first X characters of 2 fields in a table

Say I have a table named 'Parts'. I am looking to create a SQL query that compares the first X characters of two of the fields, let's call them 'PartNum1' and 'PartNum2'. For example, I would like to return all records from 'Parts' where the first 6 characters of 'PartNum1' equals the first 6 characters of 'PartNum2'.
Parts
PartNum1
PartNum2
12345678
12345600
12388888
12345000
12000000
14500000
the query would only return row 1 since the first 6 characters match. MS SQL Server 2017 in case that makes a difference.
If they are strings, use left():
left(partnum1, 6) = left(partnum2, 6)
This would be appropriate in a where, on, or case expression. Note that using left() would generally prevent the use of indexes. If this is for a join and you care about performance, you might want to include a computed column with the first six characters.
you can try something like this. I am assuming datatype as integer. You can set size of varchar based on length of fields.
select *
from Parts
WHERE SUBSTRING(CAST(PartNum1 AS VARCHAR(max)), 1,6) = SUBSTRING(CAST(PartNum2 AS VARCHAR(max)), 1,6)
You can go for simple division to see if the numerator matches for those partnumbers.
DECLARE #table table(partnum int, partnum2 int)
insert into #table values
(12345678, 12345600)
,(12388888, 12345000)
,(12000000, 14500000);
select * from #table where partnum/100 = partnum2/100
partnum
partnum2
12345678
12345600

How to multiply string value to longint in SQL

I have the below data which I want to multiply together, column A times column B to get column C.
A has datatype string and B has datatype long.
A B
16% 894
15% 200
I have tried this expression in query cast(A as int)*B but it is giving me an error.
You can try below way -
select cast(left(A, patindex('%[^0-9]%', A+'.') - 1) as int)*B
from tablename
You need to remove the '%' symbol before attempting your cast. And assuming you are actually wanting to calculate the percentage, then you also need to divide by 100.00.
cast(replace(A,'%','') as int)/100.00*B
Note: You need to use 100.00 rather than 100 to force decimal arithmetic instead of integer. Or you could cast as decimal(9,2) instead of int - either way ensures you get an accurate result.
You may well want to reduce the number of decimal points returned, in which case cast it back to your desired datatype e.g.
cast(cast(replace(A,'%','') as int)/100.00*# as decimal(9,2))
Note: decimal(9,2) is just an example - you would use whatever precision and scale you need.
The syntax of the cast in SQL Server is CAST(expression AS TYPE);
As you cannot convert '%' to an integer so you have to replace that with an empty character
as below:
SELECT cast(replace(A,'%','') AS int);
Finally you can write as below:
SELECT (cast(replace(A,'%','') AS int)/100.00)*B as C;

Subtract then divide SQL without rounding

Want to subtract one column from another then divide by first and show as a percentage
TRYING THIS
Select SUM(GoodReadCount)As 'GoodReads'
,'GoodRead%' = ((SUM (GoodReadCount)-SUM (NoReadCount))/SUM (GoodReadCount))
,SUM(NoReadCount)AS 'NoReads'
,SUM(NoInformationCount)AS'NoInfo'
,SUM(LabelConflictCount)AS'Label'
,SUM(TrackingErrorRecircCount)AS'TrackError'
,SUM(ConfirmCount)as'confirmed'
,SUM(RecircCount)as 'recirc'
FROM [DB].[DB].[dbo].[SorterStats]
WHERE RecordedPeriod Between '6/19/2017 01:00:00AM' and '6/23/2017 23:59:00PM'
Getting this result
GoodReads GoodRead% NoReads NoInfo Label TrackError confirmed recirc
21202 0 317 2 5170 8565 13007 12881
Would like to see this result
GoodReads GoodRead% NoReads NoInfo Label TrackError confirmed recirc
21202 98.5% 317 2 5170 8565 13007 12881
DATA TYPES ARE INT
Put the subtraction in parens so it is executed before the division.
(A-B)/C
Also cast/convert the datatypes to DECIMAL(4,2) to avoid the result being automatically rounded.
Either
cast((SUM (GoodReadCount)-SUM (NoReadCount)) as decimal(8,2))*100/ cast(SUM (GoodReadCount)as decimal(4,2))
or
cast(cast((SUM (GoodReadCount)-SUM (NoReadCount))*100.00/SUM (GoodReadCount) as decimal(4,1)) as varchar(10)) + '%'
Would do the trick. If you divide it and then later cast it there is no use because the divided number is an integer and when you cast it, you are simply adding two zeros after the decimal point

Error to use 1.3 in sql query

I am using the following query.
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal).
but it returns the field number 2 result instead of 1.6
Please let me know how can I do it?
Here:
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal(2, 1))
You must specify number of digits in your decimal when casting, an number of digits after coma (I set them to 2 digits decimal with 1 after coma). You can easily test such casting by simply writing query:
SELECT Cast( 1.6 as decimal(2,1))
this will produce you effect of your casting. If you don't include (2,1) part, it will be automatically rounded to 2.

Get an accurate percentage calculation

I am trying to divide one number by another in a SQL view. I'm dividing two columns that are each of type int, and the problem is that its giving me a rounded output, rather than an accurate one.
Here is my select:
SELECT numberOne, numberTwo, (numberOne*100/NULLIF(numberTwo, 0)) as PctOutput
This is working, but when it dives 3 by 37 its giving me 8 instead of 8.108.
How would I modify this to give me an accurate answer? Do I need to cast the numbers out of ints? if so - into what?
Try an implicit cast:
SELECT
numberOne,
numberTwo,
((1.0*numberOne)*100/NULLIF(1.0*numberTwo, 0)) as PctOutput
**--Cast the denominator as Float**
SELECT 3 * 100 / NULLIF(CAST(37 AS FLOAT),0) -- 8.10810810810811
**--Multiply by 1.0 in either numerator OR denominator**
SELECT 3 * 100 / NULLIF(37 * 1.0,0) -- 8.108108
SELECT 3.0 * 100 / NULLIF(37,0) -- 8.108108
**--Convert it to decimal**
SELECT CONVERT(DECIMAL(25,13), 3) * 100 /
NULLIF(CONVERT(DECIMAL(25,13), 37),0) -- 8.108108108
You need to cast the numbers from INT into either float or decimal.
If you use literals, they will likely be decimal (NUMERIC), not float (http://stackoverflow.com/questions/1072806/sql-server-calculation-with-numeric-literals)
Note that if you use decimal, you should be aware of the rules of scale and precision in division if you have numbers near the boundaries where you might lose precision:
http://msdn.microsoft.com/en-us/library/ms190476.aspx
In SQL Server the result is always of the same type as the inputs. So yes you do need to convert the inputs.
I generally convert using convert(decimal(9,2),numberOne) but depending you may want to convert(real,numberOne) or use a different level of precision.