how to replace values in sql - sql

I WANT LIKE THIS IN THE ATTACHMENT
thanks

For getting output what you need ,first table record you need to de-concatenate, i mean single rows record make multi rows record and then put inner join. then again you need to apply concatenation on result using stuff function in sql server.

Related

Can we apply a for loop on select sql statement?

I have a SQL table with 150+ columns and I want to apply on them an aggregation function when selecting values but I don't to list all column names by hand. Instead I want to use a for loop through the column names of the table.
I want to do something like this:
SELECT
AGREEMENT_NO,
COUNT(DISTINCT column) FOR column LOOP columns -- Instead of 150+ lines of count
WHERE ...
GROUP BY AGREEMENT_NO
Does anyone know if it's possible to do it in SQL and if yes how?
This can be done by dynamic query.
Take every column of a table by in temporary table by Information_Schema.columns.
Take a while loop for temporary table for all row.
Write dynamic query and concat column name for your requirement.
Execute the query. Eg. Exec("Query" / variable).
This will give you exact result.

(Hibernate) searching for strings that match a pattern

I can't find the hql to solve my problem.
I have 2 nice tables.
The first table has a column with strings in the form 'xxx-xxx-xxx'.
The second table has a column with strings in the form 'some_prefix:xxx-xxx-xxx'.
What I want to do is given a subset of rows in the second table, find all the entries in the first table that match in the 'xxx-xxx-xxx' part. And I know for sure there cannot be more that one entry in the fist table for each row in the second.
I'm looking for a hql query that fetches those objects but I could use a sql too.
Cheers.
you can use a combination of locate and substring function on column of the second table to get the string after the : sign.
I haven't tested it but it should be something like:
where table1.column = substring(table2.column, locate(table2.column, ':'))

How to build SQL query for associated values?

I am using PyODBC to fetch some data, since I am not fetching all data in my table, I need to write a query which grabs only rows which have associated columns. For example my initial query is:
SELECT SRNumber FROM SO_SC_1 WHERE SRNumber LIKE '%1-%'
This returns the SRNumber values that I want.
Next I want to return the associated last edited user with this SRNumber. This column is named last_edited_user. What is the proper syntax to incorporate multiple queries into one for this scenario? Basically I would like to use the initial query and grab all associated data for each SRNumber.
You query all needed columns using their comma separated names
SELECT SRNumber, last_edited_user
FROM SO_SC_1
WHERE SRNumber LIKE '%1-%'

Get row values as column names in t-sql

I have a requirement to display row values as column names in a data grid view. I want to get the store names into columns using sql select statement. (Please refer the attached image). I want user to enter some values under each column. So STORE 1, STORE 2, STORE 3 should displays as columns in datagrid view. Does anyone can help me to get this work?
while googling i found this can be done using PIVOT in SQL. But in this table i don't have any aggregate columns. Any help pls?
the result should be somthing like
You may know that your data only contains a single row for each pivoting column, but SQL Server has to construct a plan that could accommodate multiple rows.
So, use PIVOT and just use an aggregate that, if passed a single value, will return that same value. MIN() and MAX() fit that description (as does SUM if you're working with numeric data)
You may use specific function of dynamic pivot and pass your query with item count column.
You can use below link which provided you function and can easily show you expected output.
http://forums.asp.net/t/1772644.aspx/1
Procedure name:
[dbo].[dynamic_pivot]

Delete rows conditionally

I have a dataset with over 100,000 rows, over 100 columns and where some values are NULL. Now I want to remove all the rows which contain NULL values.
Can anybody suggest the sql command for it?
With the little information you've provided:
DELETE FROM table WHERE colA IS NULL OR colB is NULL
Add further conditions for each column that you want to check.
Change OR to AND if you only want to delete rows where all of the columns are NULL.
It's fairly easy to generate the SQL for this using a query on user_tab_columns if you don't want to type it out by hand.
use a scripting language like PHP to retreive all column names and then construct your SQL query.
Using pure SQL could get tricky.