How to wrap my query output with single quotation - sql

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, "'" )

Related

sql concatenation with blank cells

So I am extracting data from one table to another
SELECT *
LTRIM(ADRESSE + ',' + ADRESSE2) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT]
Issue is that if the cells are blank i still get a comma ,
I have tried using & instead of + but nvarchar is incompatible in the '&' operator. Any ideas how I only insert the comma if there is something to concatenate?
You want the equivalent of CONCAT_WS() in other databases. You can do this with STUFF() and some string logic in SQL Server:
SELECT c.*
STUFF( (COALESCE(',' + ADRESSE, '') +
COALESCE(',' + ADRESSE2, '') +
), 1, 1, ''
) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT] c;
This structure is convenient, because you can just add more COALESCE() expressions for more columns.
use case expression
SELECT *, LTRIM(ADRESSE + case when ADRESSE is not null then ',' end + ADRESSE2) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT]
use case when for null checking
SELECT *
LTRIM(ADRESSE + case when ADRESSE2 is not null then
',' else '' end + ADRESSE2) AS ADDRESS12
FROM [Homestore].[dbo].[CLIENT]

how to replace a comma with single quote comma in SQLIN clause

Hi all I have a an where clause like below
select * from table1
where colum1 IN (?parameter)
when I pass the values to the parameter they show up like below
('1,2,3') but to execute the query I need to change the values as ('1','2','3')
is there a way to replace the commas with single quotes comma in IN clause directly?
There is one hack to do what you want, using like:
select *
from table1
where ',' || column1 || ',' like '%,' || (?parameter) || ',%';
This functions, but it will not make use of an index on column1. You should think about other solutions, such as:
Parsing the string into a table variable.
Using in with a fixed number of parameters.
Storing the values in a table.
There may be other Oracle-specific solutions as well.
Use MS SQL, you can convert it into table value and using join for your condition, I am not familiar oracle but you can find same way to do it.
DECLARE #IDs varchar(max) ='1,2,3';
;WITH Cte AS
(
SELECT
CAST('<ID>' + REPLACE( #IDs, ',' , '</ID><ID>') + '</ID>' AS XML) AS IDs
)
SELECT '''' + Split.a.value('.', 'VARCHAR(100)') +'''' AS ID FROM Cte
CROSS APPLY Cte.IDs.nodes('/ID') Split(a)
You can achieve it using with clause. Logic here is to convert each comma separated value into different row.
with temp_tab as (
select replace(regexp_substr(parameter, '[^,]+',1, level),'''','') as str
from dual
connect by level<= length(regexp_replace(parameter, '[^,]+'))+1 )
select * from table1 where column1 in (select str from temp_tab);

How can we replace , with ',' in T-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, ',', '%')

Comma Delimiting Names with Apostrophes

This is a T-SQL question.
I have a select statement where i take a list of cities and comma delimit them, then put them into a temp table. However, one of my cities is Couer D'Alene, which has an apostrophe in its name, and SQL is not liking that - no results are returned. How can I modify this to accept city names with apostrophes? Thanks!
Declare #vCity varchar(1000)
Select #vCity= ',' + #vCity+ ','
create table #Cities
(
cityName varchar (1000)
)
Insert Into #Cities
Select cityName
From cityTable
Where #vCity Like '%,' + cityName + ',%'
Group By cityName
You need to escape the ' with another ':''
Easiest way is the REPLACE function:
Select #vCity= ',' + REPLACE(#vCity,'''', '''''' ) + ','
All the extra ' are escaping several layers deep.
select QUOTENAME( cityName, '''' ) ...

What is the best way to collapse the rows of a SELECT into a string?

In a SQL statement ( or procedure ) I want to collapse the rows of this table into a single comma delimited string.
simpleTable
id value
-- -----
1 "a"
2 "b"
3 "c"
Collapse to:
"a, b, c"
You can concatenate using an embedded 'set' statement in a query:
declare #combined varchar(2000)
select #combined = isnull(#combined + ', ','') + isnull(value,'')
from simpleTable
print #combined
(Note that the first isnull() initialises the string, and the second isnull() is especially important if there's any chance of nulls in the 'value' column, because otherwise a single null could wipe out the whole concatenation)
(edited code and explanation after comments)
Edit (ten years later):
SQL Server 2017 introduced the STRING_AGG() function which provides an official way of concatenating strings from different rows. Like other aggregation functions such as COUNT(), it can be used with GROUP BY.
So for the example above you could do:
select string_agg(value, ', ')
from simpleTable
If you had some other column and you wanted to concatenate for values of that column, you would add a 'group by' clause, e.g:
select someCategory, string_agg(value, ', ') as concatValues
from simpleTable
group by someCategory
Note string_agg will only work with SQL 2017 and above.
This will only work in MSSQL 2005+
select value + ',' from simpletable for xml path ('')
..one way to prevent the extra comma:
select case(row_number() over (order by id))
when 1 then value else ',' + value end
from simpletable
for xml path ('')
DECLARE #EmployeeList varchar(100)
SELECT #EmployeeList = COALESCE(#EmployeeList + ', ', '') +
CAST(Emp_UniqueID AS varchar(5))
FROM SalesCallsEmployees
WHERE SalCal_UniqueID = 1
SELECT #EmployeeList
Results:
1, 2, 4
This is based on #codeulike answer, but will prevent losing the portion of the string that gets concatenated before a null "value" is concatenated on.
declare #combined varchar(2000)
select #combined = isnull(#combined + ', ','') + ISNULL(value,'')
from simpleTable
print #combined