I have a column that stores 2 values. Example below:
| Column 1 |
|some title1 =ExtractThis ; Source Title12 = ExtractThis2|
I want to remove 'ExtractThis' into one column and 'ExtractThis2' into another column. I've tried using a substring but it doesn't work as the data in column 1 is variable and therefore it doesn't always carve out my intended values. SQL below:
SELECT substring(d.Column1,13,24) FROM dbo.Table d
This returns 'Extract This' but for other columns it either takes too much or too little. Is there a function or combination of functions that will allow me to split consistently on the character? This is consistent in my column unlike my length count.
select substring(col1,CHARINDEX('=',col1)+1,CHARINDEX (';',col1)-CHARINDEX ('=',col1)-1) Val1,
substring(col1,CHARINDEX('=',col1,CHARINDEX (';',col1))+1,LEN(col1)) Val2
from #data
there is duplicate calculation that can be reduced from 5 to 3 to each line.
but I want to believe this simple optimization done by SQL SERVER.
I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id
I have table test( ID Numeric(11,0), report varchar(255) )
and data looks below
1 ,Age,,,,,,family_status,,,,,,
2 ,,,,,,,,retaliation,hostile environment,,,,
3 ,,,,,,,,,,,,,
4 ,,,,,,,,retaliation,,,,,
5 ,,,,,,,,,hostile environment,,,,
6 ,Age,,,,,,,,,,,,
7 ,,,,national_origin,,,,,,,,,
8 Sex,,,,,,,,,,,,,
9 ,,,,national_origin,,disability,,retaliation,,,,,
10 Sex,,,,,,,,retaliation,,,,,
11 ,,,,,,,,
and i would like to update this table by replacing or using any other data scribing to remove extra commas so that data looks
1 Age,family_status
2 retaliation,hostile environment
3
4 retaliation
5 hostile environment
6 Age
7 national_origin,
8 Sex
9 national_origin,disability,retaliation
10 Sex,retaliation
11
i try to use the below statement but not sure how to loop through so that it will check and remove all the commas
UPDATE table test SET report = replace(report , ',,', ',')
If you are just doing this as a one off task (rather than a scripted process you expect to use repeatedly) you could always just run this query repeatedly until you get 0 rows updated
UPDATE table test SET report = replace(report , ',,', ',')
WHERE report like '%,,%'
If you need to do this over and over, or put it in a program I recommend using your procedural (non SQL code) to do the replace where you have better text manipulation commands.
If you aren't thrilled with that, check out this blog article I wrote on a similar problem of replacing repeating spaces from a string.
I have a problem I am trying to solve using a query instead of VBA.
I have two fields which we'll call "FPC" and "Code". Both fields contain numbers. An FPC value will match a Code value. What I want to make sure is that once an FPC value matches a Code value, the same FPC value does not match up with a DIFFERENT Code Value and vice versa - Once a Code Value is used, I don't want the Code Value to match up with more than one FPC.
It is important to note that there are duplicate values used in both fields.
Here is an example:
FPC CODE
1 12
1 12
1 14
2 16
3 11
3 11
4 17
5 19
6 16
There are two errors here:
1. The FPC "1" is matched up with two different Code Values.
2. The Code "16" is matched up with two different FPC values.
Please let me know your suggestions. I was thinking a query would help, and then running VBA to pull the results (there is tens of thousands of records).
What is the purpose of the query? Just to identify problems? If yes then something like
select FPC, count(distinct(CODE)) from tableName where count(distinct(CODE)) >1 group by FPC
(and the converse query for CODE vs. FPC) should be OK.
I have the following data:
2
3
4
5
6
7
8
9
10
apple
green
hi
I would like to consolidate all of this into one value, like so:
'2','4','5','6','7','8','9','10','apple','green','hi'
So I need to take data from rows, paste them into an Excel file, run this macro and get them all in one cell (with commas and quotes). I will then put them in a WHERE IN clause in an SQL statement for faster searches. If there is an easier way or a tool, please let me know.
Here is the intended use in SQL:
Select *
from Blah..Table
where IDs in
(
'2','4','5','6','7','8','9','10','apple','green','hi'
)
I wrote a user defined function called JoinRange that does that. You can get the code here
http://www.dailydoseofexcel.com/archives/2012/03/31/joinrange-update/
You would use it like
=joinrange(A1:A12,,"','","'","'")
which gives the result
'2','3','4','5','6','7','8','9','10','apple','green','hi'
You can also call it from VBA.