How can we replace , with ',' in T-Sql - sql

How can we replace , with ',' in T-Sql
i am passing 'a,b,c' as a parameter and i am trying to replace , with ',' so that i can get the output as 'a','b','c'
i have the workaround where we can put it in a temp table and read from that table but i was curious to know if we can achieve it directly using the replace function in Sql server.
Thanks in advance

You could use
select ''''+replace(column1,',',''',''') +''''
from tablename
However, there may be more efficient ways to do this.

Let's say you were using the parameter
DECLARE #Param NVARCHAR(100)
You can simply perform the following operation:
SET #Param = (SELECT CONCAT('''', REPLACE(#Param ,',',''','''), '''') )
I haven't tested this on SQL yet, but I think this should do the job

Try like this,
SELECT Column1 AS CommaSeparatedColumn
,'''' + replace(Column1, ',', ''',''') + '''' AS CommaWithinSingleQuotesSeparatedColumn
FROM (
VALUES ('a,b,c,d')
) T(Column1)
WHERE Column1 LIKE '%'
OR Column1 LIKE '%' + REPLACE(Column1, ',', '%')

Related

Adding ISNULL into a small dynamic header script

We are using this small recursive script to create a string for the dynamic headers of a table - the variable feeds into a piece of dynamic sql.
DROP TABLE IF EXISTS #PivotStep1;
CREATE TABLE #PivotStep1(PlayMonth VARCHAR(25))
INSERT INTO #PivotStep1
values
('01-oct-2016'),
('01-nov-2016'),
('01-dec-2016'),
('01-jan-2017'),
('01-feb-2017');
DECLARE #ColumnName AS nvarchar(MAX)
SELECT #ColumnName = ISNULL(#ColumnName + ',','')
+ QUOTENAME(PlayMonth)
FROM (
SELECT DISTINCT PlayMonth
FROM #PivotStep1
) AS ps
SELECT #ColumnName;
The above gives the following:
[01-dec-2016],[01-feb-2017],[01-jan-2017],[01-nov-2016],[01-oct-2016]
What we want is to surround each column name with ISNULL and AS... So the desired output is the following:
ISNULL([01-dec-2016],0) AS [01-dec-2016],ISNULL([01-feb-2017],0) AS [01-feb-2017],ISNULL([01-jan-2017],0) AS [01-jan-2017],ISNULL([01-nov-2016],0) AS [01-nov-2016],ISNULL([01-oct-2016],0) AS [01-oct-2016]
We would like to keep using the above recursive approach rather than using STUFF and XML.
Use the quotename twice and do other required concatenation.
Try this:
select #ColumnName = ISNULL(#ColumnName + ',', '')
+ 'ISNULL(' + QUOTENAME(PlayMonth) + ', 0) as ' + QUOTENAME(PlayMonth)
from (
select distinct PlayMonth
from #PivotStep1
) as ps
select #ColumnName;
Demo

How to wrap my query output with single quotation

I often read records from my database and use notepad++ to processing the receipt in this format:
'xxxxxxxxx'
'xxxxxxxxx',
'xxxxxxxxx',
'xxxxxxxxx'
Is there a way I can use SQL query to do this once.
Sample query I ran is:
Select ReceiptNo
from My_table
where TIN = 'KEYVALUE'
This is pretty straightforward concatenation. You need to use 4 quotes here, though: the first and last are your wrapper quotes which contain the string. The inner 2 quotes are your actual quote to use, and an escape quote.
SELECT
'''' + CAST(ReceiptNo as varchar(100)) + ''''
FROM
My_Table
WHERE
TIN = 'KEYVALUE'
You may want to try below:
SELECT
'''' + CAST(ReceiptNo as varchar(100)) + ''','
FROM
My_Table
WHERE
TIN = 'KEYVALUE'
SELECT ''''+ cast(ReceiptNo as varchar(10)) + ''',' as ReceiptNo
FROM My_table
WHERE TIN = 'T'
Here is my sql fiddle
You can use CONCAT function to append single quote. do like
SELECT concat( "'", DATEBASEFIELDNAME, "'" )

SQL Query to List

I have a table variable in a stored procedure. What I want is to find all of the unique values in one column and join them in a comma-separated list. I am already in a stored procedure, so I can do it some way that way; however, I am curious if I can do this with a query. I am on SQL Server 2008. This query gets me the values I want:
SELECT DISTINCT faultType FROM #simFaults;
Is there a way (using CONCAT or something like that) where I can get the list as a single comma-separated value?
This worked for me on a test dataset.
DECLARE #MyCSV Varchar(200) = ''
SELECT #MyCSV = #MyCSV +
CAST(faulttype AS Varchar) + ','
FROM #Simfaults
GROUP BY faultType
SET #MyCSV = LEFT(#MyCSV, LEN(#MyCSV) - 1)
SELECT #MyCSV
The last part is needed to trim the trailing comma.
+1 to JNK - the other common way you will see, which doesn't require a variable is:
SELECT DISTINCT faulttype + ','
FROM #simfaults
FOR XML PATH ('')
Note that if faulttype contains characters like "<" for example, those will be xml encoded. But for simple values this will be OK.
this is how we do this
create table #test (item int)
insert into #test
values(1),(2),(3)
select STUFF((SELECT ', ' + cast(Item as nvarchar)
FROM #test
FOR XML PATH('')), 1, 2, '')
Without the space after the comma it would be;
select STUFF((SELECT ',' + cast(Item as nvarchar)
FROM #test
FOR XML PATH('')), 1,1, '')

How to use Coalesce for string concatenation in a subquery?

I'm trying to string concatenate multiple row values from one table using "Coalesce", separated by comma and list it as a column in a subquery.
Something along the line of
Declare #assignTo nvarchar(4000)
Select
table1.columnA
table1.columnB
(
select #assignTo = Coalesce(#assignTo + ', ', '') + CAST(Name as nvarchar(250))
from
table2
where
...
)
from table1
where
.....
I keep getting "Incorrect syntax near '='."
If I just try to execute the subquery where the Coalesce function is called, its' fine. i.e
Declare #assignTo nvarchar(4000)
select #assignTo = Coalesce(#assignTo + ', ', '') + CAST(Name as nvarchar(250))
from
table2
where
...
Select #assignTo
That's fine. So my question is, how do I include it as a subquery?
Thanks a lot
ps: This is specific to SQL server 2000.
You can't include it as a subquery: you'll have to move it into a UDF.
In SQL Server 2005 you can with the XML PATH technique. But for SQL Server 2000 you have to have a separate scalar UDF with table access and the concatenation
As far as I know,
You can do like this if u prefer to do as SubQuery.
But the above solution is the most convenient one.
Declare #assignTo nvarchar(4000)
Select
table1.columnA
table1.columnB
tmp.[c]
from table1,
(
select #assignTo = Coalesce(#assignTo + ', ', '') + CAST(Name as nvarchar(250)) as [c]
from
table2
where
...
) as tmp
where
Hope it works!

How to pass parameter to stored procedure

I am using SQL Server 2008 Enterprise on Windows Server 2008 Enterprise. In the stored procedure, I need to pass parameter to a select-where-like statement. For example,
#department is store procedure input parameter, its type is varchar(20),
select * from sometable where somecolumn LIKE '%#department%'
but seems my statement above does not work, any ideas how to pass parameter #department to like statement?
thanks in advance,
George
select * /*But don't use * in production!*/
from sometable
where somecolumn
LIKE '%' + #department + '%'
You concatenate the strings:
select * from sometable where somecolumn LIKE '%' + #department + '%'
It's a variable, it goes outside the quotes.
select * from sometable where somecol like '%' + #department + '%'
Or, more preferably, add the %s to the variable and just use it directly
try:
select * from sometable where somecolumn LIKE '%' + #department + '%'
You could do something like this:
select * from sometable where somecolumn LIKE '%' + #department + '%'
select * from sometable
where CHARINDEX(#department,somecolumn)>0
You can also use the CHARINDEX function.