I am trying to run a SQL query and its output of a column should contain value till 5 decimals but it's by default rounding till 2 digits. I am using the below given query. Can someone please help me with this.
declare #VITG decimal(18,5)
select Left(wount(#VITG,18,5)) as VITG
from DBFFile
select CAST(MyNymber AS DECIMAL(18,5))
This will work
select convert(decimal(18,5),ColumnName) as VITG
from DBFFile
As your not using T-sql Datasourse you should do this is code for ex in c# you have to do some thing like this.
var columnValue=From_Query_Result_As_String;
if(columnValue.Substring(columnValue.IndexOf(".")+1).Length>=5)
{
columnValue=Convert.ToDecimal(columnValue.Substring(0,columnValue.IndexOf(".")+6));
}
Related
I have a few codes as you see in the screenshot below.
I want my database only to take code that has exactly 6 digits. The code which has more than 6 digits is expected to be deleted/ignored.
How do I write my SQL query for the same?
I was thinking to use Patindex but couldn't succeed in it.
I suggest using SQL Server's enhanced LIKE operator here:
SELECT *
FROM yourTable
WHERE Code LIKE '[0-9][0-9][0-9][0-9][0-9][0-9]';
One of DB2 table column value is appearing as 0.3901369869709015 and we need to compare this value against my expected value as 0.390136986970901. I tried to get the value from DB2 by using Decimal/Dec method. With that the value is getting round off and appearing as 0.390136986970902. Could you please help me to correct the below query that i am using to extract data from my DB2 table.
SELECT DECIMAL(UV_FIELDSCOREMAP,15,15) AS UV_FIELDSCOREMAP From cvsinst.uv_occ WHERE CASEID = '20170720'
Use TRUNCATE(UV_FIELDSCOREMAP, 15) instead.
I have some case about 8.89291 value in SQL database.
In this case if I'm running:
select cast(ROUND(8.89291,0) as float)
The output is : 9
What I'm actually looking for is
The output is : 8.9
How can I get this value?
Will you guys please help me?
Your precision is 0 so its giving the whole value.
select cast(round(8.89291, 1) as float)
If you are open to casting, then you don't also need to call ROUND. Consider casting to NUMERIC(10,1):
SELECT CAST(8.89291 AS NUMERIC(10,1));
Demo
(demo for SQL Server, but the above would run on many other databases as well)
You had selected 0 that mean float function will treat as Integer ,for the float you have to put 1 instead of 0 ,please check below one.
select cast(ROUND(8.89291,1) as float)
You can use
SELECT CAST(ROUND(8.89291,2) AS NUMERIC(12,1))
or
SELECT CAST(8.89291 AS NUMERIC(12,1))
I have data like this
1234500010
1234500020
1234500021
12345600010
12345600011
123456700010
123456700020
123456710010
The pattern is
1-data(varian 3-7 digit number) + 2-data(any 3 digit number) + 3-data (any 2 digit number)
I want to create SQL to get 1-data only.
For example I want to get data 12345
I want the result only
1234500010
1234500020
1234500021
If I using "like",
select *
FROM data
where ID like '12345%' `
I will get all the data with 12345, 123456 and 1234567
If I using equal, I will only get one specific data.
Can I combine like and equal together to get result like what I want?
select * FROM data where data = '12345 + any 2-data(3 digit) + any 3-data(2 digit)'
Anyone can help?
Addition : Sorry if I didn't mention the data type and make some miss communication. The data type is in char. #Gordon answers and the others not wrong. It works for number and varchar. but not works for char type. Here I post some pic for char data type. Oracle specification for char data type is a fixed lenght. So if I input less than lenght the remain of it will be change into a space.
Thank you very much. Hope someone can help for this
Since your datatype is CHAR, Gordon's answer is not working for you. CHAR adds trailing spaces for the strings less than maximum limit. You could use TRIM to fix this as shown. But, you should preferably store numbers in the NUMBER type and not CHAR or VARCHAR2, which will create other problems sooner or later.
select *
from data
where trim(ID) like '12345_____';
I think you want:
select *
from data
where ID like '12345_____' -- exactly 5 _
Here is a rextester demonstrating the answer.
You really can't combine equality and LIKE. But you can use a regular expression to do this kind of searching, with the REGEXP_LIKE function:
SELECT *
FROM DATA
WHERE REGEXP_LIKE(ID, '^12345[0-9]{3}[0-9]{2}');
But if I understand correctly, for your 1-data you really want a 3 to 7 digit number:
SELECT *
FROM DATA
WHERE REGEXP_LIKE(ID, '^[0-9]{3,7}[0-9]{3}[0-9]{2}');
Oracle regular expression docs here
SQLFiddle here
Best of luck.
I think this gives you the solution you want,
create table data(ID number(15));
insert into data values(1234500010);
insert into data values(1234500020);
insert into data values(1234500021);
insert into data values(12345600010);
insert into data values(12345600011);
insert into data values(123456700010);
insert into data values(123456700020);
insert into data values(123456710010);
select * from data where ID like '12345_____'
// After 5_ underscore are exactly 5 , any 3 digits from 2-data(3 underscores) and 2 digits from 3-data(2 underscores)
You'll be getting(OUTPUT) :
ID
1234500010
1234500020
1234500021
3 rows returned in 0.00 seconds
Here is the query i am running
Select (100/18)*18
The answer i get is 90. I have tried declaring each of the numbers as decimals also, and i get the value 100.0008, but when i calculate with variables as floats the answer is correct at '100' so does anyone have any idea why SQL calculates like this?
Because it will first evelute the paranthesis
So
Select (100/18)*18
will (100/18) = 5.555555 but it assume both number as int so it will cast the result as int so it will return 5
so now 5*18 = 90
If you want the correct result do this instead Select (100*18)/18
to get the desired result you should try something like this:
Select CEILING((100/18.0)*18)
when you do this,
Select (100/18)*18
sql server consider operation as integer division and take select 100/18 as 5 instaed of 5.55555....
Try to express the numders ac decimal numbers
SELECT (100.0/18)*18
and
SELECT ROUND((100.0/18)*18,2)
...
you can try this
select abs(18*100/18)