Find values with 2 or more consecutive numeric characters (SQL) - sql-server

I have a column of values that sometimes have consecutive numbers, how can I get all values that are 2+ consecutive numeric characters? For example:
value
-------
car1339
foo3bar9
there10yes
hellothere
What would the SQL statement be to get the following?
car1339
there10yes

SQL DEMO
SELECT *
FROM Table1
WHERE value LIKE '%[0-9][0-9]%' ;

Related

Snowflake float type column out of range error

I got Numeric value out of range error when trying to insert two values into a Float type column.
create or replace table num_test(float_num float); -- create table and column
INSERT INTO num_test (float_num)VALUES (1.0528618730874378E10), (-3.694822225952521E-13);
The error i got shows: "Numeric value '10528618730.874378' is out of range."
But when i try to insert these two values separately, it work fine.
INSERT INTO num_test (float_num)VALUES (1.0528618730874378E10); -- ok
INSERT INTO num_test (float_num)VALUES (-3.694822225952521E-13); -- ok
I couldn't see any out of range issues on the snowflake document for the values I tried to insert.
If I had to guess what the problem was, it would be that "the guess of the "type" based on the first value in the VALUES is smaller than the type of the second value".
So if we just try and select those values with zero target problems:
select column1, system$typeof(column1)
from VALUES
(1.0528618730874378E10),
(-3.694822225952521E-13);
triggers.
Numeric value '10528618730.874378' is out of range
One at a time we get:
COLUMN1
SYSTEM$TYPEOF(COLUMN1)
-0.0000000000003695
NUMBER(29,28)[SB16]
COLUMN1
SYSTEM$TYPEOF(COLUMN1)
10,528,618,730.874378
NUMBER(17,6)[SB8]
So sure enough the two random "numbers" are cast to two different types, and these are deemed "too different". Thus my guess was correct.
What to do about this:
So we inline cast them:
select column1, system$typeof(column1)
from VALUES
(1.0528618730874378E10::double),
(-3.694822225952521E-13::double)
;
we get
COLUMN1
SYSTEM$TYPEOF(COLUMN1)
10,528,618,730.8744
FLOAT[DOUBLE]
-0.0000000000003695
FLOAT[DOUBLE]
so the values are safe if we first tell the DB what they are, and avoid the auto guessing code.
thus in your context:
INSERT INTO num_test (float_num)VALUES
(1.0528618730874378E10::double),
(-3.694822225952521E-13::double)
;
number of rows inserted
2

How to get max Value Unit from field Value Unit based on first value before comma separated?

I work on SQL server 2012 I face issue :i can't get first max value from value Unit based on First value before comma .
as example This value Unit below :
1.89, 2.625, 3.465
I will get first value before comma separated as 1.89 then if this number is max value return full number
exist on Value Unit
create table #finaltable
(
partid int,
ValueUnit nvarchar(50)
)
insert into #finaltable(partid,ValueUnit)
values
(2532,'1.71, 2.375, 3.135'),
(2532,'1.89, 2.625, 3.465')
select * from #finaltable
How to get first max value from field ValueUnit based on first value before comma separated ?
Expected Result returned :
1.89, 2.625, 3.465
because 1.89 is maximum number from 1.71 then I returned full number
I agree with the comments, your design is bad. For more on that, you should also read "Is storing a delimited list in a database column really that bad?".
But well, you can use patindex() to get the position of the comma and then extract the first number representation with left(). convert() it to some decimal, order by it and take the TOP 1 row.
SELECT TOP 1
*
FROM #finaltable
ORDER BY convert(decimal(4, 3), left(valueunit, patindex('%,%', valueunit) - 1)) DESC;
You may need to tweak the conversion to a decimal. I don't know what maximum length and precision you may need.

How to mask some digit using SQL Select

I have a column named Number
Suppose a value is 12345678997
and I want to output as *******8997, that means all leading digits will be masked by * except the last 4 digits
how can I achieve this using SQL Server Select ?
Try this:
declare #i bigint = '12345678997'
select stuff(#i,1,len(#i)-4,'*******')
Output:
*******8997
Using REPLICATE you can generate a string with a given number of the same character.
Then just add the last four digits to that.
select
Number,
concat(replicate('*',len(Number)-4), right(Number,4)) as MaskedNumber
from YourTable

T-SQL: Get max numeric value in char field having both numbers and letters

In a SQL Server 2008 database table I have a char field that has both numbers and letters. I.e.:
TST
842
UUT
124
674
XTM
763
I need to find the maximum integer in that field. So in the above example I would return "842".
I know how to test for whether the value is numeric or not (ISNUMERIC()function), but I can't figure out how, using that test, to return the maximum integer value.
SELECT MAX(yourcol) FROM T WHERE ISNUMERIC(yourcol)=1 ;
Please try this:
SELECT MAX(field)
FROM table
WHERE ISNUMERIC(field) = 1
One way is to use an outer query to get the max from the set of integers:
select MAX(your_char) from (select your_char from table where ISNUMERIC(your_char) = 1) sub
or actually this should work too:
select MAX(your_char) from t where ISNUMERIC(your_char) = 1

SQL Server SELECT query ordering by substring

I have a column in a SQL Server table that has the following rows:
MyColumn : C1_xxx1,C2_xxx1,C3_xxx1,C1_xxx2,C1_xxx3,C3_xxx2 etc
It is a text column that contains strings that have the following format: CY_mystring where Y is a number from 1 to 5, followed by the '_' character then mystring that can have any value.
Is there a way to make a select return this column ordered as following:
C1_xxx1
C1_xxx2
C1_xxx3
......
C1_xxxn
C2_xxx1
......
C2_xxxn
C3_xxx1
.......
C3_xxxn
etc
Ordered by the CY_ substring.
thank you
This should do it .. (order first by the first two chars, and then by the last char (assuming that the final n is always one digit long))
SELECT
Column1
FROM
TABLENAME
ORDER BY
LEFT(Column1,2) ASC,
RIGHT(Column1,1) ASC
You say that Y is a number from 1 to 5 it's always one character long. Assuming the format is xY_xxxZ, you can order on Y then Z like:
order by
substring(MyColumn,2,1) -- Second digit
, right(MyColumn,1) -- Last digit
If Z can be longer than one character (i.e. 10 or higher) you can use pathindex to determine the number of digits at the end:
order by
substring(MyColumn,2,1) -- Second digit
, right(MyColumn, patindex('%[^0-9]%', reverse(MyColumn))-1) -- Digits at end

Resources