Split an Address - TSQL - tsql

I have a field called Address1 in my table Table1.
Here is an example of data in that field -
8 Brick Lane and 11 Balkerne Drive
I want in a query to spit the street and the number but am struggling with how to achieve this.
Any help would be greatful.
Thanks

SELECT
LEFT(Address1, PATINDEX('%[a-z]%', Address1)- 1) as HouseNumber,
SUBSTRING(Address1, PATINDEX('%[a-z]%', Address1), LEN(Address1)) as Street
FROM Table1
Using PATINDEX to find when HouseName begin, in this way you can separate address.
This the expected result:
HouseNumber Street
8 Brick Lane
11 Balkerne Drive
I hope this help.

I am asssuming you want to split the number from the text, you can use
SELECT Left(Address1,CHARINDEX(' ',Address1,0)-1) as houseNumber,
Right(Address1,Len(Address1)-CHARINDEX(' ',Address1,0)) as houseStreet
From Table1

Related

How to select distinct combinations in T-SQL

I'm using SQL in Devexpress dashboard designer. I want to select distinct combinations of two parameters.
Perhaps Devexpress uses Transact-SQL but at the same time GROUP BY clause never works for me.
At the same time DISTINCT BY somehowe doesn't work as well.
Example:
There are two IDs 11 and 22
And there are two values of Date for 11, as an example: 21.01.2000 and 22.01.2000. And there's one for 22 as an example: 23.05.2008
Problem here is that I can't coose DISTINCT by date because there are many other IDs which have the same dates.
So I expect to have one distinct combination of ID and Date.
Does anyone faced with the same problem, can you advice any solution / code example?
Using select distinct will filter duplicates if you leave unique row properties out of the selected fields.
so:
Mike Smit
Mike Smit
Will be reduced to
Mike Smit
But if you're also asking for a PK like a Id field you get the following because id makes both rows distinct
1 Mike Smit
2 Mike Smit
Does this help?

How do I multiple two columns of data together in kdb/q?

I am trying to teach myself kdb/q programming. I can't seem to figure out how to take a simple table (columns symbol, price, and shares) and multiply price * shares to get volume. I've read Q for Mortals, code.kx.com, etc and am stuck. Could someone please give me a hint or point me in a direction of where I could figure out this simple problem! Thanks
Where t is the table name:
update volume: price*shares from t
Or
t: select symbol, price, shares, volume:price*shares from t
checkout the q-sql for select/update queries.
Here is the update statement you are after:
q)trade:([] symbol:5?`APPL`GOOG; price:5?100.; shares:5?10)
q)update volume:price*shares from trade
symbol price shares volume
-------------------------------
APPL 21.09 6 126.54
APPL 88.22095 8 705.7676
APPL 25.0192 4 100.0768
GOOG 51.68842 1 51.68842
APPL 53.8142 8 430.5136
However, I'll recommend checking Q for mortals, it pretty much covers everything for Kdb+ beginner.

Postgres - Order by 2 fields contemporary

I've got a problem with an order by.
I have this table
|desc|phone|calls|group|priority|
|cccc|12347|700|13247|0|
|aaaa|12345|900|12345|0|
|bbbb|12346|500|12345|1|
I need to order this table for calls respecting the group, so my result should be
|desc|phone|calls|group|priority|
|aaaa|12345|900|12345|0|
|bbbb|12346|500|12345|1|
|cccc|12347|700|13247|0|
because bbbb is with the same group of aaaa.
How can I do this?
Thanks
EDIT:
Hi all,
sorry if my question is unclear, next time I'll be more specific.
Yes, I need to order this table for calls respecting the group, like if bbbb doesn't exists
Since your question is a bit unclear, An order by is enough
select * from sample order by "group"
sqlfiddle-demo

SQL query with ranking order

All,
Need a help with one of the sql queries. I have a query which pulls up records on ranking order.
Select * from
(select count(*) cnt, customer_cd, smallint(Rank() Over(Order by count(8) Desc)) as rnk
from table.customer
Now, the result shows like,
Cnt Customer Cd
110 1- Retail
90 2-Human resources
20 3-Information Technology
11 Not Standard
I want to remove the description from it and will have only the Customer Codes such as 1,2,3,NS etc. Any help how to achieve this.
Thanks.
You could use LOCATE to find the position of the hyphen, assuming you always have a hyphen. Then, you could use SUBSTRING to get the portion of the string before the position found by LOCATE.
select substring(customer_cd,0,locate('-',customer_cd))
from table.customer
should show you what you will get.
You do seem to have some data (e.g. "Non Standard") that has no code at all. Such fields will come out as blank. If you want to replace that with some specific code, you can use a CASE...END expression.
select CASE when locate('-',customer_cd)==0 then ""
else substring(customer_cd,0, locate('-',customer_cd) ) END
from table.customer

Switch column data where a column contains a number?

I have a table that have 3 columns
id, company and adress
i found a bug today that saved the adress in the company-column and company in the adress-column SOMETIMES, i have corrected the bug and now im trying to put the data in the right places
every adress has a number in it so my guess is that the easiest way is to switch adress and company columns if there is a number in the company-column (if there should be a number in the real company name this wont matter that much :p).
How should i write this in TSQL?
I'm not sure this is right thing to do here but as I can't think of any other alternative this should do it.
Update dbo.MyTable
Set Company = Address,
Address = Company
Where Company like '%[0-9]%'
You can try this: i put a simple protection to avoid the swap if the company adress already contains a number
insert into COMPANY (NAME, ADDRESS)
VALUES ('2 bld d''Italie' , 'CA') ,
('Take 2' , 'anselmo street 234') ,
('Microsoft' , '1 Microsoft Way Redmond'),
('lake street 14' , 'Norton'),
('lake street 17' , 'trendMicro');
SELECT * FROM COMPANY
UPDATE COMPANY set NAME = ADDRESS, ADDRESS = NAME
WHERE NAME like '%[0-9]%' and (ADDRESS not like '%[0-9]%')
SELECT * FROM COMPANY
You could notice that the take 2 line won't be swapped