Counting the no. of commas across multiple fields using SQL - sql

I've 3 fields which contain only text. However, i want to add a calculated field which counts the number of commas in each of these 3 fields and displays it separately in the adjacent column. The snippet of SQL i use is shown below. How can i build the calculated field?
SELECT week, client_I, client_II, client_III
FROM quality_control_test;
Please advise!

well, you can "count" the number of a given character in a string, by using this:
length(c) - length(replace(c,',',''))
I'm assume you can figure out how to leverage that for your own query ;)

Related

SQL count query number of lines per row

I am trying to count the amount of urls we have in field in sql I have googled but cannot find anything !
So for example this could be in field "url" row 1 / id 1
url/32432
url/32434
So for example this could be field "url" in row 2 / id 2
url/32432
url/32488
url/32477
So if you were to run the query the count would be 5. There is no comma in between them, only space.
Kind Regards
Scott
This is a very bad layout for data. If you have multiple urls per id, then they should be stored as separate rows in another table.
But, sometimes we are stuck with other people's bad design decisions. You can do something like this:
select (length(replace(urls, 'url', 'urlx')) - length(urls)) as num_urls
Note that the specific functions for length() and replace() might vary, depending on the database.

SSRS - How to get column value by column name

I have one table with rows and each row has a column that contains a field name (say raw1 - 'Number001', raw2-'ShortChar003', etc). In order for me to get that value of these fields I have to use a second table; this table has 1 raw with many columns (number001, Number002, ShortChar003, etc).
How can I extract the value?
Good Question..You can use lookup function
=Lookup(Fields!CityColumn.Value, Fields!CityColumn.Value, Fields!CountColumn.Value, "Dataset1")
Or you might have to use string functions..ex LEFT, Substring, Right same like SQL.If possible pls post some data of both tables, I will explain in detail

How to normalise inconsistent category labels in teradata

Table has text labels for category identification
The spelling has changed over time
I want to normalise the text labels when I count the records in each category
So for example I have The category labels:
'Ready to go' and 'Readytogo'
But I also have another text value abc that I want to replace with Abcd
The rest Inwant to keep in my Groupby and Count
How can I count these in the new group names in Teradata?
At the moment I'm using case statements for the ones I'm okay with, then using OREPLACE to switch one of the values but how do I nest it so that I can OREPLACE 2 or more values with 2 or more new ones? Is OREPLACE the best function to use here?
Thanks
Apologies I have managed to solve it using multiple WHEN, THEN clauses as part of 1 case statement
This changes the text value ax to bn along with Fx to BM for example, as required
The OREPLACE function was not recognised in the release of Teradata that I am using
I will specify question better next time
Thanks

Tagging a consecutive pattern using VBA /SQL

I'd like to create a report that finds consecutive patterns of records.
Here's a sampling of some of the patterns:
1NLUO0 1NM4OM 1NM5AF 1NM5AM 1NM5B1
1NLUO1 1NM4ON 1NM5AG 1NM5AN 1NM5B2
1NLUO2 1NM4OO 1NM5AO 1NM5B3
1NLUO3 1NM5AP
1NLUO4 1NM5AQ
1NLUO5 1NM5AR
1NLUO6 1NM5AS
1NLUO7 1NM5AT
1NLUO8 1NM5AU
1NLUO9 1NM5AW
1NM5AX
1NM5AY
1NM5AZ
If there are 2 or more, we would like to group and report them. If it only finds 1, we don't care.
I was thinking about grouping on Left([Pattern],4), doing a count of the groupings filtering for records >1.
Is there an easier way?
Assuming that your showing multiple rows for just a single column... Couldn't you just trim and look for a blank spaces? Any blank space would indicate that you had two or move items. Something like bleow...
CHARINDEX(' ',RTRIM(LTRIM(field))) > 0

How to retrieve a part of a value in a column

Correction - I only need to Pick the WORK value every result set in the column will contain comma seperated values like below..
"SICK 0.08, WORK 0.08" or "SICK 0.08,WORK 0.08"
I only need to pick WORK 0.08 from this.
I am quite new to SQL
I am using the following script to get some results;
select Work.Work_summary, Work.emp_id
from Work
all work fine. but the first column has values like the following :
WORK 08.57, SICK 08.56 (Some columns)
SICK 07.80, WORK 06.80 , OT 02.00 (Some columns)
How can i only retrieve the column with only the WORK% value, if there is no WORK value the results shall be empty.
select Work_summary, emp_id
from Work
where Work_summary like '%WORK%'
This will return the rows in the Work table where Work_summary column contains the word WORK. See the documentation for more details.
Contains is faster than like.
SELECT Work_summary, emp_id FROM Work WHERE CONTAINS(Work_summary, 'WORK');
then use: this will give only the result where work summary contains work content.
select Work.Work_summary, Work.emp_id
from Work where contains(work.Work_summary ,'work');
select replace(Work_summary,",","") as work_summary
from Work
where upper(Work_summary) like '%WORK%'