How to split a column which contains a combined text and number? - sql

I have a column in a table which consists of data like name50, somename20, other40, some65 like that.
I want to split the text part and number part and add the number part into another table with an empty column, which contains a column already with the text part. Now I have add the number part to the corresponding name part in this table.
For example in the second table I have a column called Textpart with the same text part from the first tables column (which I want to split) with all the names repeated several times randomly. And another caolumn called Numberpart which is empty.
Now I have to fill that numberpart with the corresponding numbers from the first table.
Please help me. thank you.

You can use a combination of substring and patindex.
First extract the numeric part. To get the text part just replace the previously found numeric part with an empty string.
select substring(data, patindex('%[0-9]%', data), len(data)) as numeric_part,
replace(data, substring(data, patindex('%[0-9]%', data), len(data)), '') as text_part
from tablename
To update the other table with the numeric part, use the text_part column to join.
Note that this will only work well if the numbers are towards the end.

Related

Change varchar to bigint

Currently I have a package that loads a table. However, I am wanting to change the productnumber field to a bigint that is currently a varchar. I also want to create a new column for the old productnumbers. so currently it looks like this:
ProductNumber: 1827493849, PN19379247
As you can see the product number has nonnumeric in them which is why I want a column to store the old numbers such as they are currently. but my goal is to create a column that will store these two numbers and more where it takes the leading PN off. Can someone please give examples to how this is done. Currently I have created a new table with ProductNumber as bigint and OldNumer as varchar and the table is empty. I want it to be able to store the account numbers as is under OldNumber and then insert them into the other column taking the leading PN off. Which the PN must be taken off before inserting to the ProductNumber column because it is a bigint. Help is greatly appreciated.
I wouldn't recommend a bigint for this. Instead, use a numeric of the appropriate length.
If you want to convert a string by removing the first two characters, you can use stuff():
select try_convert(numeric(20), stuff(productnumber, 1, 2, ''))
If you don't know where the number begins, then look for it using patindex():
select try_convert(numeric(20), stuff(productnumber, 1, patindex('%[0-9]%', productnumber) - 1, ''))

Replacing comma with concatenating operator in sql server

I have a base table where a field contains value of field names separated with comma.
I am trying to get the field value and query it another table to get the concatenated value.
E.g Base table A has a field Target_field which contains value such as : Addr_1,Addr2,Zip. I am trying to replace the , with + ' ' + so that when i use that field to query from another table, i get the concatenated value.
I could have used concat() function, but i want a space after a each field value.
Could you please help.
I think you may looking for REPLACE function
REPLACE('Addr_1,Addr2,Zip', ',' ,'+'' ''+')

How to spread the values from a column in Hive?

One field of table is made up of many values seperated by comma,
for example, a record of this field is:
598423,4803510,599121,98181856,1666529,106317962,4061964,7828860,598752,728067,599809,8799578,1666528,3253720,601990,601235
I want to spread the values in every record of this field in Hive.
Which function or method I can use to realize this?
Thanks.
I'm not entirely sure what you mean by "spread".
If you want an output table that has a value in every row like:
598423
4803510
599121
Then you could use explode(split(data,',')
Otherwise, if each input row has exactly 16 numbers and you want each of the numbers to reside in a different column, you have two options:
Define the comma as a delimiter for the input table ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
Split a single column into 16 columns using the split UDF: SELECT split(data,',')[0] as col1, split(data,',')[1] as col2, ...

Extract alphanumeric value from varchar column

I have a table which contains a column having alphanumeric values which is stored as a string. I have multiple values in that column having values such as F4737, 00Y778, PP0098, XXYYYZ etc.
I want to extract values starting with a series of F and must have numeric values in that row.
Alphanumeric column is the unique column having unique values but the rest of the columns contain duplicate values in my table.
Futhermore, once these values are extracted I would like to pick up the max value from the duplicate row,for eg:
Suppose I have F4737 and F4700 as a unique Alphanumeric row, then F4737 must be extracted from it.
I have written a query like this but the numeric values are not getting extracted from this query:
select max(Alplanumeric)
from Customers
where Alplanumeric '%[F0-9]%
or
select max(Alplanumeric)
from Customers
where Alplanumeric like '%[0-9]%'
and Alplanumeric like 'F%'**
I run the above query but I am only getting the F series if I remove the numeric part from the above query. How do I extract both, the F starting series as well as the numeric values included in that row?
Going out on a limb, you might be looking for a query like this:
SELECT *, substring(alphanumeric, '^F(\d+)')::int AS nr
FROM customers
WHERE alphanumeric ~ '^F\d+'
ORDER BY nr DESC NULLS LAST
, alphanumeric
LIMIT 1;
The WHERE conditions is a regular expression match, the expression is anchored to the start, so it can use an index. Ideally:
CREATE INDEX customers_alphanumeric_pattern_ops_idx ON customers
(alphanumeric text_pattern_ops);
This returns the one row with the highest (extracted) numeric value in alphanumeric among rows starting with 'F' followed by one ore more digits.
About the index:
PostgreSQL LIKE query performance variations
About pattern matching:
Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
Ideally, you should store the leading text and the following numeric value in separate columns to make this more efficient. You don't necessarily need more tables like has been suggested.

SQL query to extract text from a column and store it to a different column in the same record

I need some help with a SQL query...
I have a SQL table that holds in a column details of a form that has been submitted. I need to get a part of the text that is stored in that column and put it into a different column on the same row. The bit of text that I need to copy is always in the same position in the column.
Any help would be appreciated guys... my mind has gone blank :">
UPDATE mytable
SET other_column = SUBSTRING(column, begin_position, length)
You may just want to use a computed column. This way if the source string changes, your computed column is still correct. If you need to seek to this substring then you might want a persisted computed column if your db supports it.
UPDATE table
SET Column2 = SUBSTRING(Column1, startPos, length)
What if the value you wanted to copy was in a different position in each record, but always followed the same text?