I am trying to write a query to find a string withing a string. In Oracle I used regexp_like but I don't see such function in SQL Server 2016. I have a table that has an address column such as:
ADDRESS
--------
345 E 149 ST NY NY
345 EAST 149 STREET NY NY
444 CHEST AVE NY NY
444 CHEST AVENUE NY NY
I want to write a query that search for 345 [E OR East] follow by 149. in the case of 444 chest, I want a query to search for 444 chest[ ave or avenue].
I have something like the following which doesn't work
select * from table1 where address like '345[EEAST] 149%'
Basically I want to tell the query to get any address that start with 345 E or 345 EAST follow by 149.
Can someone help me write a query for this? I know I can use OR clause with two different address but if I have multiple addresses with different pattern then OR clause will not be efficient method. I'm looking into using some type of regular expression.
Unfortunately the amount of support for LIKE expression is very limited currently in SQL Server. Your requirement can be satisfied by below queries
DECLARE #table table(address VARCHAR(1000))
INSERT INTO #table
values
('345 E 149 ST NY NY')
,('345 EAST 149 STREET NY NY')
,('444 CHEST AVE NY NY')
,('444 CHEST AVENUE NY NY')
SELECT * FROM #table WHERE Address LIKE '345 E%149 ST% NY NY'
SELECT * FROM #table WHERE Address LIKE '444 CHEST AVE% NY NY'
Result Set
+---------------------------+
| address |
+---------------------------+
| 345 E 149 ST NY NY |
| 345 EAST 149 STREET NY NY |
+---------------------------+
+------------------------+
| address |
+------------------------+
| 444 CHEST AVE NY NY |
| 444 CHEST AVENUE NY NY |
+------------------------+
Use the LIKE operator, with separate conditions for each match:
SELECT *
FROM table1
WHERE ADDRESS LIKE '345 E 149 %' OR ADDRESS LIKE '345 EAST 149 %';
You can try this in WHERE Clause.
SELECT *
FROM #T
WHERE (
1 = CASE
WHEN ADDRESS LIKE '%345_E_149%' THEN 1
WHEN ADDRESS LIKE '%345_EAST_149%' THEN 1
WHEN ADDRESS LIKE '%444_CHEST_AVE%' THEN 1
WHEN ADDRESS LIKE '%444_CHEST_AVENUE%' THEN 1
END
)
Related
My question is very similar to How to concatenate values in multiple cells based on a condition? and I've had a lot of success applying Mourits de Beer's answer in the past; however for my current problem, I need to only join values that meet two sets of criteria instead of one.
I have Sheet_1 with ID numbers, PO numbers, and Text values that looks like this:
ID
PO
CLASS
1
223
A
1
334
B
2
334
A
3
556
B
3
445
A
1
445
A
3
334
B
I have a Sheet_2 with unique IDs where I need to join all unique PO numbers into a single cell based on whether they are Class A or B, like this:
| ID | CLASS = A and ID = A2 | CLASS = B and ID = A2 |
| ----- |---------------------- | ---------------------- |
| 1 | 223, 445 | 334 |
| 2 | 334 | N/A |
| 3 | 445 | 556, 334 |
I tried to nest an AND() function inside of the IF function so the formula only works if BOTH conditions are met, but this isn't returning all true values.
=ARRAYFORMULA(TEXTJOIN(", ",TRUE,(UNIQUE(IF(AND(Sheet_1!$ID$2:$ID=$Sheet_2!ID2,'Sheet_1!$CLASS$2:$CLASS="A"),Sheet_1!$PO$2:$PO,"NA")))))
I need the function to work in Excel/Google Sheets and do not have strong VBA skills, but any help is appreciated! (This is also my first question here, so please be kind and let me know if there is anything I can do to explain/post my questions better next time. Thank you!)
How can I reshape the dataset below from long to wide in Stata?
a1 a2
NAME Jane
SEX female
PHONE 234
SCORE 9
NAME John
SEX male
PHONE 444
SCORE 10
NAME Baba
SEX male
PHONE 777
SCORE 5
I've tried using gen i = tag(a1) to generate an id. However, this does not uniquely identify each set of repeating data.
You're correct that you need an identifier, indeed two identifiers. But gen i = tag(a1) is illegal -- presumably you mean egen -- and more to the point is not going to help. egen, tag() depends on identifiers existing already and serves only to create a (0, 1) variable, not what you need here.
This works for me. Please note the use of Stata code to create a data example: there is much more on this at the Stata tag wiki.
clear
input str6 (a1 a2)
NAME Jane
SEX female
PHONE 234
SCORE 9
NAME John
SEX male
PHONE 444
SCORE 10
NAME Baba
SEX male
PHONE 777
SCORE 5
end
drop a1
egen id = seq(), block(4)
egen j = seq(), to(4)
reshape wide a2, i(id) j(j)
rename (a2*) (name sex phone score)
destring, replace
list
+------------------------------------+
| id name sex phone score |
|------------------------------------|
1. | 1 Jane female 234 9 |
2. | 2 John male 444 10 |
3. | 3 Baba male 777 5 |
+------------------------------------+
I am not an SQL expert and have the following challenge ahead of me. I have a table which contains a note field next to the name of the person concerned. The note field is a free text, which can contain the name of a person. I would like to make it anonymous.
An example for better understanding: Table "Reports"
ID | PersonID | Name | Notefield
1 | 978 | Max | Max isn't feeling so good today.
2 | 234 | Julia | Julia's blood sugar has improved.
3 | ...
The result should look like this:
ID | PersonID | Name | Notefield
1 | 978 | Max | M. isn't feeling so good today.
2 | 234 | Julia | J. blood sugar has improved.
3 | ...
So I want to change the note field depending on the name. Can anyone here help?
You can use replace
REPLACE(Notefield, Name, LEFT(Name,1) + '.' )
This can be dangerous as it will replace parts of normal words. E.g. "Maximize". You must search for REPLACE(Notefield, Name + '[space]', LEFT(Name,1) + '.' )
I need to reduce a model DB to 3NF. However there is a column in the data thats very ambiguous.
So the database has the following columns. (Apologies for formatting, I did try)
Employer ID | ContractNo | Hours | emp Name | workNo | workLocation
--
123 | A1 | 10 | J Smith | W36 | New York
124 | A1 | 7 | P Jones | W36 | New York
125 | A2 | 9 | R Lewis | W37 | Los Angeles
123 | A2 | 9 | J Smith | W37 | Los Angeles
Each employee has a unique ID, an employee can work at more than 1 location and each location has a unique workNo. I'm just a bit stuck on where to include the ContractNo. There is no indication in the question of what it actually is for.
So my first step was splitting it up into a table with EmployerID, employee Name and hours. And a second table with WorkNo, WorkLocation. But what do I make of that bloody ContractNo?
I expect the contract is likely a separate entity, capturing the nature of the relationship between contractor and contractee.
Image from QuickDBD, where I work.
I need to get a dimension member returned as a calculated measure.
Given:
Dimensions
Customer {ACME, EMCA, Universal Imports, Universal Exports}
Salesperson {Bob, Fred, Mary, Joe}
Credit Type {Director, Manager}
Measures
Credited Value
Value
Relationships
The Customer is a dimension of the facts that contain Value
The Customer, Salesperson and Credit Type are dimensions of the facts that contain Credited Value
I am trying to do the following:
Create calculated measures that will return the Salesperson with the largest $s credited in a role for a customer. e.g.
| Customer | Director | Manager | Value |
|-------------------|----------|---------|-------|
| ACME | Bob | Fred | 500 |
| EMCA | Bob | Fred | 540 |
| Universal Imports | Mary | Joe | 1000 |
| Universal Exports | Mary | Fred | 33 |
ACME has Bob credited with 490 as Director
ACME has Fred credited with 500 as Manager
ACME has Mary credited with 10 as Director
I would like to use this as a calculated measure that I can use in any case where Customers are the ROW.
If I understand your problem correctly, something along this line should do the trick (of course you'd have to use the proper level, hierarchy and cube names):
with
member [Measures].[DirectorTemp] as topcount([Salesperson].[Salesperson].members,1,([Measures].[Credited Value],[Credit Type].[Director],[Customer].currentmember)).item(0).properties("Caption")
member [Measures].[Director] as iif([Measures].[DirectorTemp] = [Salesperson].UnknownMember.properties("caption"), null, [Measures].[DirectorTemp])
member [Measures].[ManagerTemp] as topcount([Salesperson].[Salesperson].members,1,([Measures].[Credited Value],[Credit Type].[Manager],[Customer].currentmember)).item(0).properties("Caption")
member [Measures].[Manager] as iif([Measures].[ManagerTemp] = [Salesperson].UnknownMember.properties("caption"), null, [Measures].[ManagerTemp])
select
{[Measures].[Director],[Measures].[Manager],[Measures].[Value]} on 0,
{[Customer].members} on 1
from MyCube