How to extract somephone number from 1 string in SQL Server? - sql-server

I have a string like :
XLNTT018 : XXX 13 xxx, 0355500000 Note: xxx, 0367000000 ,xxxxx 0768500000
in one of my columns. I want to extract the number with 10 digits length. For example, in the above case, I want to extract 0355500000 | 0367000000 | 0768500000 in SQL Server.

Related

SQL Server Wildcard Search

I'm using SQL Server 2014 and I'm getting incorrect results back from a LIKE expression. I'm trying to match this type of string A-ABC123-AABB where ABC123 and AABB can be any length strings.
Here is the LIKE expression:
ID.name LIKE 'a-%-'+STR.STR
If the iteration has STR as a string with a prefixed dash and there exists an STR with no P, it will return the value with no dash.
How can I do strict matching so that it shows only the result with the dash, IF STR has a string?
Example:
STR table:
col STR Val
----------------
1 test 10
2 -test 12
ID table:
col name
-----------------------
1 a-blah-test
2 a-blah--test
3 a-tst-test
Results:
name val
--------------------
a-blah-test 10
a-blah--test 10
a-blah--test 12
This might fix your problem here although I'm not sure it will handle all the general cases correctly.
ID.names LIKE 'P-%[^-]-'+ STR.STR

Adding the value in column of SQL Server 2012

I need to add two zero in POS data to make it 14th character numbers because TV UPC has 14th character numbers UPC.
For example
Table 1
POS UPC
------------
123456789012
Table 2
TV UPC
--------------
00123456789012
I have to make it POS UPC 14 character number by adding two zero at the beginning of the number to compare exact match.
Prefix the number with 14 0s and select 14 characters from right which will give you desired output
DECLARE #Number varchar(100)='123456789012'
SELECT RIGHT('00000000000000'+#Number,14)
Output
00123456789012
Replace variable with your column name.
Fiddle here : http://sqlfiddle.com/#!3/9f1d32/1

Convert string to integer and update into other field

I have a table that has the following structure
[zip] = <zip, nvarchar(4),>
[cityZipID] = <cityZipID, int,>
In the zip column there is a string containing 4 digits and this is a number between 1000 an 1239 gut stored as a string.
For some reason I need to calculate an other value out of this so I need to convert the string into an integer and store it into an other column called cityZipID. I want to do this using SQL Server Management Studio because it has to convert about 32000 lines so I cannot easily do it by hand.
I tried the following but get only an error message when trying to execute it
UPDATE [MyTestData].[dbo].[Addresses]
SET [cityZipID] = ((int)[zip])/10 -100
WHERE [city] = 'Wien'
The column of cityZipID is null in the moment and should be filled with numbers for the districts like the plzl for the first district is 1010 the 12th district is 1120 So the calculation would result in 1120 / 10 = 112 -100 = 12 and this would be the wanted result.
Any help would be appreciated.
Jonny
Try this query
UPDATE [MyTestData].[dbo].[Addresses]
SET [cityZipID] = (Convert(Int,[zip])/10) -100
WHERE [city] = 'Wien'

sphinx - Column count doesn't match

I have the following in my sphinx
mysql> desc rec;
+-----------+---------+
| Field | Type |
+-----------+---------+
| id | integer |
| desc | field |
| tid | uint |
| gid | uint |
| no | uint |
+-----------+---------+
And I ran the following successfully in sphinx sql
replace into rec VALUES ('24','test test',1,1, 1 );
But when I run in the C mysql API I get this error
Column count doesn't match value count at row 1
the c code is this
if (mysql_query(con, "replace into rec VALUES ('24','test test',1,1, 1 )") )
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
Please note that the C program is connecting to the sphinx sql with no issues
One problem may be that you are quoting the integer for the id column. I would try taking out the single quotes around the 24. The column named desc is also concerning, since that is a reserved word in MySQL.
A good best practice is to always specify the column names, even if you are inserting into all columns. The reason is that you may want to alter the table later to add a column and you don't necessarily want to go back and change all your code to match the new structure. It also makes your code clearer since you don't have to reference the table structure to know what the values mean and it helps in case a tool like Sphinx is using a different order for the columns than you expect. Try changing your code to this, which specifies the columns and quotes them (mysql uses backticks for quotes) and also removes the quotes around the value for the id column:
if (mysql_query(con, "replace into rec (`id`, `desc`, `tid`, `gid`, `no`) VALUES (24, 'test test', 1, 1, 1)") )

A single MySQL query for 'bouncing' table selects

So, say for the sake of simplicity, I have a master table containing two fields - The first is an attribute and the second is the attributes value. If the second field is set to reference a value in another table it is denoted in parenthesis.
Example:
MASTER_TABLE:
Attr_ID | Attr_Val
--------+-----------
1 | 23(table1) --> 23rd value from `table1`
2 | ...
1 | 42 --> the number 42
1 | 72(table2) --> 72nd value from `table2`
3 | ...
1 | txt --> string "txt"
2 | ...
4 | ...
TABLE 1:
Val_Id | Value
--------+-----------
1 | some_content
2 | ...
. | ...
. | ...
. | ...
23 | some_content
. | ...
Is it possible to perform a single query in SQL (without parsing the results inside the application and requerying the db) that would iterate trough master_table and for the given <attr_id> get only the attributes that reference other tables (e.g. 23(table1), 72(table2), ...), then parse the tables names from the parenthesis (e.g. table1, table2, ...) and perform a query to get the (23rd, 72nd, ...) value (e.g. some_content) from that referenced table?
Here is something I've done, and it parses the Attr_Val for the table name, but I don't know how to assign it to a string and then do a query with that string.
PREPARE pstmt FROM
"SELECT * FROM information_schema.tables
WHERE TABLESCHEMA = '<my_db_name>' AND TABLE_NAME=?";
SET #str_tablename =
(SELECT table.tablename FROM
(SELECT #string:=(SELECT <string_column> FROM <table> WHERE ID=<attr_id>) as String,
#loc1:=length(#string)-locate("(", reverse(#string))+2 AS from,
#loc2:=length(#string)-locate(")", reverse(#string))+1-#loc1 AS to,
substr(#string,#loc1, #loc2) AS tablename
) table
); <--this returns 1 rows which is OK
EXECUTE pstmt USING #str_tablename; <--this then returns 0 rows
Any thoughts?
I love the purity of this approach, if pulled off. But I'm thinking you're creating a maintenance bomb. With a cure like this, who needs to be sick?
No one has ever said of a web site "Man, their data sure is pure!" They compliment what is being done with the data. I don't recommend you keep your hands tied behind your back on this one. I guarantee your competitors aren't.

Resources