How to reduce scale value of a column - sql

I have an requirement to reduce the column value to number(5) from number(6). For this i do not want to alter the column and instead just wanted to update the data using SQL query.
Example:
From to
123456 12345
135790 13579
i.e. i just wanted to remove the last digit.

If you are looking to remove the last digit of a number, you can use :
floor(original_value/10)
If you want to remove the last digits only from numbers that have exactly 6 digits, you want :
decode(length(original_value), 6, floor(original_value/10), original_value)

Related

How do you do multiple substrings for a field in Teradata?

I have a field to pull account numbers which have different lengths and I want to pass the last four digits of the account number. The dilemma I am having is that since they are different lengths I am having trouble in substringing the fields. The standard length is 11 digits but there are accounts with 9 digits and 7 digits.
How do I substring those values in multiple substrings to capture all the account last 4 digits in one query?
This currently what I have:
SELECT SUBSTRING(ACCT_NBR,7,4) AS BNK_ACCT_NBR
FROM NAMEOFTABLE;
I want to have additional substrings to capture the account numbers that don't have 11 digits similar to
SUBSTRING(ACCT_NBR,5,4)
SUBSTRING(ACCT_NBR,4,4)
The results should look like:
76587990891 - 0891
654378908 - 8908
45643456 - 3456
Can you please help me in figuring out how I can do that?
Thanks.
Is ACCT_NBR a VarChar or an INT?
VarChar:
Right (ACCT_NBR,4)
Substr(ACCT_NBR,Char_Length(x)-3)
INT:
ACCT_NBR MOD 10000

SQLite3 Order by highest/lowest numerical value

I am trying to do a query in SQLite3 to order a column by numerical value. Instead of getting the rows ordered by the numerical value of the column, the rows are ordered alphabetically by the first digit's numerical value.
For example in the query below 110 appears before 2 because the first digit (1) is less than two. However the entire number 110 is greater than 2 and I need that to appear after 2.
sqlite> SELECT digit,text FROM test ORDER BY digit;
1|one
110|One Hundred Ten
2|TWO
3|Three
sqlite>
Is there a way to make 110 appear after 2?
It seems like digit is a stored as a string, not as a number. You need to convert it to a number to get the proper ordering. A simple approach uses:
SELECT digit, text
FROM test
ORDER BY digit + 0

CHECK constraint for string to contain a certain amount of digits as well as certain digits. (Oracle SQL)

I have a column, number where I need a length constraint (say 11 digits) as well as to assert the existence of some certain numbers. Let us say the first four digits need to be '1234' and the fifth in the range'6-9'. I am using a varchar type so I also need to assert numbers. With some research here is what I have been able to come up with:
CHECK (REGEXP_LIKE(number, '^1234\d{6}$'))
In this way I have been able to check the number of digits (11), the first 4 starting numbers and number values. However, I cannot fit the fifth number which needs to be between 6 and 9 into this expression.
Thanks in advance
Try this.
CHECK (REGEXP_LIKE(number, '^1234[6-9]\d{6}$'))

Access Update Query First 5 digits, but last number goes up one

I am using Access Update Query to change a column to the first 5 digits, I got that part. But I ALSO need the last digit to go up one. So if its 12345 I need it to be 12346.
This is what I have so far:
Left([Num],5)
Try this:
CLng(Left([Num], 5)) + 1
The CLng is only necessary if the original column isn't already a number field.

How do I get the last three digits of a varying length column?

I want to write a select statement and get the last three digits of all of the rows in a column for which the length varies.
Any ideas on how I can achieve this?
Hypothetical column:
12312398098098
127865275
I want the resulting column to have the values:
Resulting column after the script:
098
275
SELECT RIGHT(CONVERT(VARCHAR(4000), [hypothetical column]), 3) FROM table;
(Added a convert in case this is a numeric column.)