How can I use TRIM function in a SQL query? - sql

I would like to use the trim function but don't really know how it works.
I need to select items in a table and export it on a CSV file.
It's working fine but sometime when I have a specific data (here it's a string), the csv file write it with à CR LF and write at the line after.
I think that a problem with the data in the data base and that there "something" after my data in this specific field.
I have this query:
string request = "SELECT * FROM " + tableName;
And I want to try this but I'm not sure.
string request = "SELECT * TRIM(TRAILING FROM ") + tableName;

if you are trying in SQL SERVER.
LTRIM(RTRIM(COLUMN))

Related

TransferSpreadsheet export leading apostrophe

I use some vba to export the results of a select query. The selected data needs to be exported 1 on 1 to Excel. This morning I faced the following error:
3274 on DoCmd.TransferSpreadsheet, External table is not in the expected format
Inspecting the source data, I've found a row containing a leading Apostrophe. Removing this row from the source, solved the error. However, I need this row to be exported as well. Is there any setting to solve this problem?
This is the VBA:
lSQL = ""
lSQL = lSQL & "select a.* from table As e "
Set lQD = lDB.CreateQueryDef("Test", lSQL)
DoCmd.TransferSpreadsheet A_EXPORT, 10, "Test", FileName, True
Replace table (or lSQL) with a simple select query.
In this query, sanitize the field contents using my CSql function or similar. It will double single-quotes which, I believe, should be the cure.

How to add to the end of text column through sql?

I am trying to create an sql query that adds a string to the end of the cell. Right now I have this.
$sql = "UPDATE table SET $column1= $column1 + $newstring1, $column2 = $column2 + $newstring2, $column3 = $column3 + $newstring3, WHERE username = $user_username";
The values of $newstring1, $newstring2, and $newstring3 are formatted like "155:2,"
The idea is to add delimiters to each entry so I can easily sort them later. Right now I'm getting a syntax error on my query, but I'm new to php. Is the error because when the database is empty, there is no original variable and therefore I need to INSERT INTO instead of UPDATE, or is the comma in the string itself creating the error and do I need to somehow concatenate it?
I'm more used to C# than PHP so I'm not sure the proper way to format that type of query.

Form-driven query in MS Access using comma separated values as input? Is it possible?

I have successfully queried data data in Access based on a value passed from a single text box on a form, but is there any way to pass a comma-delimited list into an IN query, or something to that effect?
The goal would be to search on more than a single-value criteria.
You would need to dynamically create your SQL in code, then feed the resultant query into the row source parameter.
If the comma list is numbers (1,5,723) then you can do:
ssql = "select * from table where field IN(" + scommalist + ")"
If the list is text, you have to parse it and put quotes around each value.

Matching text string on first letter in SQL query

SAMPLE CODE:
Dim sql As String = "SELECT * FROM " + tblName + " WHERE needsTranslation = 'True' AND dataText LIKE " & "'" & alpha & "%" & "'" & " ORDER BY dataText;"
da = New SqlDataAdapter(sql, strConnection)
OP:
I would like to create a SQL query that returns all records when the first letter of a string matches my variable. I am coding this in an ASP.net code behind page in vb.net.
SELECT * FROM " + tblName + " WHERE textData = ' & alpha & "
In this exmample textData is a string of text and alpha is a single letter a through z or A through Z.
I don't need the criteria to be case sensitive, but I do need only the first letter of textData to match alpha.
I have tested the LIKE comparator and it does not return all records that begin with alpha.
What is the best way to do this? Any and all help will be appreciated.
thanks again,
The LIKE operator is what you'd want to use, but you have to use the % wildcard character like so:
SELECT * FROM MyTable WHERE textData LIKE 'a%'
SQL has sub-string operator SUBSTR() or SUBSTRING()
select * from tableName where substr( textData ) in ( 'A', 'B', 'C', ... );
I couldn't add to the comments on one of the other posts, but I'll strongly second the need to use a parameterized query for these reasons (you can include usage of the like operator with the wildcard % like the other answer correctly summarized to answer your question):
It will protect you from making mistakes with single quotes, especially if the user enters a search string that includes them
(they will cause your query to fail).
It protects you from SQL injection exploits. Example, a user were able to input the value of the variable "alpha" in the above
example they could enter something like:
'; DELETE FROM ;
If the user you were using had excessive database rights, they could
wreak all kinds of havoc (or they could potentially get access to
data they shouldn't have access to).

SQL concatenation inside VBA string

I'm using VBA excel 2003,SQL 2005 to make a sql query call and inside my sql statement I'm using '+' operator to concatenate two strings.
dim query as string
query = "Select distinct ', '+emailaddress1 "
query = query & "from contact "
would this work inside vba? My query returns too many records in excel but not in SQL?
Please just focus on this 2 lines of code and not worry about the rest of my sql call, I'm just wondering whether or not this specific string would work?
Your code will return a column where each row would be an email address with a comma in front of it. If that is what you want, then yes, it will work.
If, on contrary, you want a single string where all email addresses would be listed, separated with commas, that'd be
query = "declare #foo varchar(max);"
query = query & "select distinct #foo = isnull(#foo,'') + emailaddress1 + ', ' from contact;"
query = query & "select left(#foo, len(#foo)-2);"