subtraction of prime zeros from an alphanumeric field - sql

Good evening ,
I have an alphanumeric field and I want after the letters to subtract the zeros that it has
my base is azure and my field is a bit like that, the number is 10 digits but I want to subtract the zeros to make it smaller in report. my field is SALDOC.RELDOCSDATE DEAP0000013169 3/12/2021

If your database support REGEXP_REPLACE, then you may try:
SELECT REGEXP_REPLACE('GFRE000005268', '([A-Z]+)0*([1-9][0-9]*)', '\1\2')
Here is a MySQL demo. The actual syntax on your database might be slightly different.

It would depend on the requirement.
You can use this if you are sure that there is always just 4 letter prefix.
This would just convert the numbers to INT to remove those zeros from the start.
SELECT CONCAT(LEFT('GFRE000005268',4),CONVERT(int,(REPLACE('GFRE000005268',LEFT('GFRE000005268',4),''))))

Related

Searching on Last Decimal Place

In the SQL (SQL Server 2019) database I am working, the vendor has introduced a rounding bug and I'm trying to track down all instances of this issue.
On a decimal field with 10 decimal places eg. 42.5500000001, I want to search where only the last decimal place is not equal to zero. I'm struggling with the code on this one.
Any assistance is appreciated.
Select the last value in your column from the RIGHT and check to see if it’s not equal to zero.
select * from test_table where right(test_column, 1) <> 0
db<>fiddle here.

subtracting in SQL Server

I have a table in SQL Server where I have the scores for some competencies, I have one score for the standard and one for the actual score. For instance S25 is the actual score and C25 is the standard for the score. I need to find the difference between the two so I can see who was above and below the standard and cannot figure out how to get the subtract to work. THe way I tried was
Select (S25) - (C25) AS 25_Score
Which did not work
If table starts with a number, bracket it, and that might work. What error do you get?
select (S25)-(C25) AS [25_Score]
from table_name
Your query should work if your columns are a numeric datatype.
The only issue I see is you are starting the alias with a number. You will need to escape the number value with a square bracket:
Select (S25) - (C25) AS [25_Score]
from yt;
See Demo
It may be that the column is of varchar so you have to convert
select convert(int,[S25])-convert(int,[C25]) AS [25_Score]
from table_name

SQL Server - Select between two number ranges in a varchar field with text and numbers

I have a table with ranges of numbers from 0-2000000. I can use a between statement to get the records of things if I do a BETWEEN 50 AND 100. That works.
But the problem I'm running into is since this column is a varchar if there are any records with a something in the string as "1FD32", I can't select between it. SQL server can't convert the "BETWEEN 50 AND 100" because it is looking for an int. How can i get all the records between these ranges, and ignore any with letters in them?
If you want to do a numeric comparison, the only way to guarantee no error is to use the CASE statement. This is the only SQL statement that guarantees the order of valuation (when not used with aggregation functions).
So, here is code that will work:
select *
from t
where (case when isnumeric(col) = 1 then cast(col as bigint) end) between 20 and 100
Note that if your numbers contain decimal points, then you will want to use DECIMAL or FLOAT instead.

Functioning of MAX in t-SQL for INT and VARCHAR Datatypes

Last morning, my manager gave me a new database where I had to write few SQL Stored procedures. There was a column MMs which was meant for numeric values so I had the understanding that Sql Function MAX will work on it. But this was not the case.
It was a varchar column holding numeric values randomly stored from 1 to 41. I was assuming that my SQL Statement: "SELECT MAX(MMS) FROM tbl" will give me 41 as the result but it came out to be 9.
Later I invested that MAX when used with string datatypes takes the first numeric letter from the column and then gives the MAX out of them.
Does anybody has the idea why it is happening?
From here:
http://msdn.microsoft.com/en-uk/library/ms187751.aspx
For character columns, MAX finds the highest value in the collating
sequence.
If you want to sort the contents of your column numerically, you can do something like:
ORDER BY len(mms), mms

SQL select order by decimal precision desc

I'm interested in finding the most precise decimal values in a database field. I'd like to be able to sort results by descending precision. Is this possible?
e.g.
10.1781253
12345.12435
89.763
1.1
2
You need to order by the substring of the part after any dot. This is going to be a DB specific SQL query and since you didn't mention which one you're using I can't give a detailed SQL example.