SQl Output to CSV WITHOUT single Quotes - sql

I am running a query on Adaptive Server Anywhere v7.
select customerinfo.customerid, Name, Address1, Address2, City, State, ZIP, Country from customerinfo, addressinfo
where customerinfo.customerid = addressinfo.customerid
and MEMBER = (Date(GetDate()-4))
and addressinfo.addresstype = 's';
Output to C:\SamplePacks.CSV
Output is:
123, 'name','address1,'address2'.....
Is there a way to run the query so that the single quotes DO NOT show?

See doc
QUOTE clause The QUOTE clause is for the TEXT output format only.
The quote string is placed around string values. The default is a
single quote ('). If ALL is specified in the QUOTE clause, the quote
string is placed around all values, not just around strings. To
suppress quoting, specify empty single quotes. For example, QUOTE ''.
So it seems that you should do
Output to 'C:\SamplePacks.CSV' QUOTE ''

Related

How do I use single quotes as part of a string in SQL

I have a where clause that uses a string,
Where
pm.Alias = 'Toys'R'Us France'
However part of the string uses single quotation marks, 'R'
How do i wrap up the whole string to pass through into my Where clause
I cannot use:
Where
pm.Alias = 'Toys''R''Us France'
As i need the whole string encased, as i will use this in Excel to pass this as a paramter into my query
in SQL, if you want to have Single Quotes inside a string, then you should specify it as 2 consecutive single quotes for every single quote in your string. So
Where
pm.Alias = 'Toys'R'Us France'
should be written as
Where
pm.Alias = 'Toys''R''Us France'
You might try using extra quotes after and before the existing quotes.
In this case add quote before and after 'R', and the query will be like below.
Where
pm.Alias = 'Toys''R''Us France'
I recently face this issue in my sqlite database, you can resolve using like this.
Where
pm.Alias = "Toys'R'Us France"
use double quote (") instead of single quote (') after equal sign.

White space records are retrieved despite having a check not include it?

Select 'Total schools' As Title, Count(1) As Total_Count
From (Select Distinct School_Name, Indv_Id
From D_Education D
where Regexp_Like(School_Name,'[^A-Za-z0-9, -./]+')
);
this query is returning around 100 distinct school names.
Most of the records returned by this are having special characters like ?, \ or whitespace. My question is why is white space records are retreived despite having a check to exclude whitespace. Dorcas Place - such records are coming.
Any help is appreciated :)
This expression:
where Regexp_Like(School_Name, '[^A-Za-z0-9, -./]+')
Is simply requiring that at least one character not in your list is in the name.
If you want names that have a special character, this may be what you want:
where not Regexp_Like(School_Name, '^[A-Za-z0-9, -./]$')
This is checking that all characters in the name are valid. The ^ and $ are anchors in the regular expression. They require that the entire string match the pattern.
Edit:
To allow single quotes, you would do:
where not Regexp_Like(School_Name, '^[A-Za-z0-9, -./'']$')
You need to double the single quote, because it is the delimiter for a string.

How do I correctly implement quotename in SQL Server?

Background Info:
I have a stored procedure that is populating an SSRS report. The SSRS report is ran and exported as a CSV. It is then opened as textfile and ran through a 3 party vendor application.
The output of the text book should look like this:
lid, status, i_flag,Count, pDate, iDate
62558633,"Text Value","08/16",11,"08/16","08/16"
78013526,"Text Value","",,"08/16""08/16"
My results look like this:
lid, status, i_flag,Count,pDate,iDate
19007442,"'Dir,MgmtII'",'',2,'','02/16'
17343623,'Text','',0,'11/15','02/16'
Now the code that I'm using is:
SELECT
quotename(isnull(i_flag,''''), '''') as i_flag,
isnull(lid, 0) as lid,
quotename(isnull(status,''''), '''') as status,
isnull(Count, 0) as Count,
quotename(isnull(p_Date,''''), '''') as p_Date,
quotename(isnull(i_Date,''''), '''') as i_Date
FROM
#Table
Any ideas on how I can fix this. Been stumped on this for a bit. Thanks.
If I'm understanding your question correctly (which I'm quite possibly not), I think you want:
SELECT
QUOTENAME(ISNULL(i_flag,''), '"') AS i_flag,
ISNULL(lid, 0) AS lid,
QUOTENAME(ISNULL([status],''), '"') AS [status],
ISNULL([Count], 0) AS [Count],
QUOTENAME(ISNULL(p_Date,''), '"') AS p_Date,
QUOTENAME(ISNULL(i_Date,''), '"') AS i_Date
FROM
#Table
It sounds like you have some values in fields which you wish to wrap in double quotes " for the purpose of exporting to CSV, plus in some cases the values in these fields might be NULL.
My suggestion above handles this by first using ISNULL to replace any NULL values with an empty string and then using QUOTENAME to wrap the resultant value in double quotes.
The crucial differences to your posted code are:
When using ISNULL I replace the NULL with an empty string '' instead of a string containing a single quote character '''' (two consecutive single quotes within a string represent an escaped literal single quote character)
When using QUOTENAME to wrap the values in double quotes, I specify a string containing a double quote '"' in the second parameter, instead of a string containing a single quote character ''''.
I hope that helps you - if you're still having problems, perhaps you could provide some sample rows from your #Table temp table and the output you're expecting from the query so people can help you further.
As an aside, it's not good practice to use SQL reserved keywords like status or count as column names or aliases, but if you must, I'd recommend enclosing them in brackets (i.e. [status]) for readability (especially in SSMS or any IDE with SQL syntac highlighting or colour-coding) as I have done above.

Invalid column name when selecting

I have a table called Jobs with the following column names: JobID, Name, and Value. The table is filled like just one entry: JobID: 1, Name: TestJob, Value: 10
I want to do select * from Jobs where Name="TestJob", but this gives me an error saying "Invalid column name 'TestJob'". Why can't I select by the value of the Name column? Doing JobID=1 or Value=10 will give me the proper result.
Use single quotes instead of double quotes. Single quotes are the standard for SQL string and date constants:
select *
from Job
where Name = 'TestJob';
Some databases do accept double quotes for this purpose. It is always safest to use single quotes for string and date constants and double quotes to escape identifier names (if needed).
Change your double quotes " to single quotes '. Double quotes are used to surround object names, probably in the same way [] can be used, so you can have spaces and other normally-invalid object name characters in the object name. Single quotes, on the other hand, are used for string literals.
Use ' instead of ". It will work.

Replace quote in SQL Select

I have a scenario in my vb.net project where users need to select a name from a combobox (or type a new name).
Names of course can have ' as in Tina O'Hara which in the database would be stored as Tina O''Hara.
My combobox is populated from a sql select command. I have tried to use a Replace so that the names display correctly in the dropdown.
SqlStr = "SELECT Replace(ContactName, '''', ''') AS ddlText FROM tbl_Visits Where CustID = " & hd_CustID.value & " ORDER By ContactName"
PopulateCMBX(cmbx_ContactName, SqlStr, "Please Select")
PopulateCMBX... This gets a list of names from the supplied SqlStr and populates the combobox itemlist with a 'Please Select' as the first option.
SqlStr is producing an error as there is not a matching set of ' how do I fix this. Thanks
When referencing single quotes in MS-SQL, you need to escape each single quote (') with two single quotes ('').
If the name is stored in the database with two single quotes, such as (''), then when you do the replace you need to use four single quotes (''''). In addition to these, you need to enclose the string with single quotes.
What this means is that your replace should look like this:
SqlStr = "SELECT Replace(ContactName, '''''', '''') ...
The 2nd param in the replace has 6 single quotes: 4 to represent the two single quotes you are replacing, and 2 to make it a string. The 3rd param has 4 single quotes: 2 to represent a single escaped quote and two to make it a string.
Try with this, maybe not the best solution but it works (check this example):
select Replace(Replace(ContactName, '''', '$&'),'$&$&','''') from tbl_Visits
Note that $& is like a key that should not affect the values ​​in your field, for example, could be a word: &&k&& or %%key%%, etc.
Back to clarify, it is not the best solution, but sometimes i've used.