Distinct column value in multiple column output (then into CSV) - sql

I have the following query:
SELECT first, last, title, email, org
FROM people WHERE email <> ""
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'
Which works. I need to select distinct emails (I don't want multiple entries from the same email).
Would it work like?:
SELECT first, last, title, distinct(email), org
FROM people WHERE email <> ""
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'

Which platform / version of SQL are you using? Typically this would be done with a group by statement. Something like:
SELECT first, last, title, email, org
FROM people
GROUP BY Email
WHERE email <> ""
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'
The above will actually work with some platforms / versions of SQL but the "correct" (standard sql) way to do it would be as follows (of course if the other fields are different for same email you get undefined results):
SELECT max(first), max(last), max(title), email, max(org)
FROM people
GROUP BY Email
WHERE email <> ""
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'

Related

An illegal XML character "001A" was found in an SQL/XML expression or function argument sql QUERY

I am trying to concatenate 1 column data by grouping other columns using XMLAGG() but I am facing XML bad data issue .
SQL error:
An illegal XML character "001A" was found in an SQL/XML expression or function argument
This is my query:
SELECT FIRSTNAME, LASTNAME, EMAIL_ID, VNDR_ID,
CASE WHEN (TEXTPREVIEW > ' ')
THEN
substr(xmlserialize(Xmlagg(Xmltext(Concat(', ',Trim( TEXTPREVIEW)))) as clob), 3)
END AS Notes
from CONTACT_ETL_NOTE_TABLE
where TEXTPREVIEW <> ''
GROUP BY FIRSTNAME, LASTNAME, EMAIL_ID, VNDR_ID,TEXTPREVIEW
How can I avoid this error?
If the data is less than 32K you would be better using LISTAGG
but if all you need is to remove the byte sequence x'001A', then you can use REGEXP_REPLACE for that (depending on your Db2 version).
SELECT FIRSTNAME, LASTNAME, EMAIL_ID, VNDR_ID
, CASE WHEN (TEXTPREVIEW > ' ')
THEN substr(xmlserialize(Xmlagg(Xmltext(Concat(', ',Trim(
REGEXP_REPLACE(TEXTPREVIEW,'\x00\x1A','') )))) as clob), 3) END AS Notes
from
CONTACT_ETL_NOTE_TABLE
where
TEXTPREVIEW <> ''
GROUP BY
FIRSTNAME, LASTNAME, EMAIL_ID, VNDR_ID,TEXTPREVIEW
or as 001A might be UTF-16, and in UTF-8 you actually have a UTF-8: 0x1A then you might just need REGEXP_REPLACE(TEXTPREVIEW,'x1A',''). So maybe check what byte(s) you have that are causing the issue. Note that XML does not allow x00-x1F control characters (although Unicode is fine with them... go figure)

Using string methods in a SELECT query to select up to the second space?

In an MS-Access database I'm working with, one of the tables has a field called "Name". The format of this field will generally be along the lines of "firstname surname integer", but sometimes may just be "firstname surname".
I need to select just the first name and the surname from the name field.
I've looked at using the Left function
SELECT DISTINCT LEFT([Name], x)
However since names are different lengths, this isn't going to work since there is no constant integer to use as the second parameter. Nor can it be used with
SELECT DISTINCT LEFT(InStr([Name], " "), x)
for the above reason, but also because because that would split the field at the first space.
Is there a way using LEFT, TRIM, SPLIT or any other string manipulation that I can create a query to select just the first two parts of the name? I need the space included.
You can try this.
SELECT DISTINCT IIf( ( InStr( InStr([Name],' ') + 1 , [Name], ' ') > 0 ), Left( [Name], InStr(InStr([Name],' ') + 1 , [Name], ' ') ), [Name])
FROM MyTable;

How to show leading/trailing whitespace in a PostgreSQL column?

I can't see leading/trailing whitespace in the following following SQL statement executed with psql:
select name from my_table;
Is there a pragmatic way to see leading/trailing whitespace?
One of option is to use format() function.
With given query case: select format( '"%s"', name ) from my_table;
PoC:
SELECT format( '"%s"', name )
FROM ( VALUES ( ' a ' ), ( ' b ' ) ) v(name);
format
--------
" a "
" b "
(2 rows)
Turn off "aligned mode" in psql: \a
\a
select * from my_table;
id|col1|col2
12|foo|bar
I'd append surrounding quotes:
select '"' || name || '"' from my_table;
If you don't mind substituting all whitespace characters whether or not they are leading/trailing, something like the following will do it:
SELECT REPLACE(REPLACE(REPLACE(REPLACE(txt, ' ', '_'),
E'\t', '\t'),
E'\r', '\r'),
E'\n', '\n') AS txt
FROM test;
This is using an underscore to mark the spaces but of course you are free to choose your own. See SQL fiddle demo.
If you strictly only want to show up the leading/trailing ones it will get more complex - but if this is really desired, something may be possible using regex_replace.
You can try this:
select name from my_table
where name like '% '

Need help finding syntax in SQL developer

i have the following query for inserting values into my Customer table:
INSERT INTO customer(Booking_id,First_name,Last_name,Phone,Address,Town,Postcode,email)
VAlUES
(1,'Elroy','Craddock',01497 3139773','36 Yaffingale Gate','Tadley','RG78 2AB','e.craddock#yautia.co.uk')
after running it writes
Error starting at line 1,551 in command:
INSERT INTO customer (Booking_id, First_name, Last_name, Phone, Address, Town, Post code, email) VALUES( 1551 ,' Leonard ',' Babbs ', 01959 8159688 ,' 46 Zoophagy Green ',' Choppington ',' NE41 5DB ',' l.babbs#sommelier.co.uk ')
Error at Command Line:1,551 Column:86
Error report:
SQL Error: ORA-00917: missing comma
00917. 00000 - "missing comma"
*Cause:
*Action:
i'v been trying to fix this syntax error for almost a day now! Any help/suggestions are appreciated! Thank you
This is your query:
INSERT INTO customer (Booking_id, First_name, Last_name, Phone, Address, Town, Post code, email) VALUES( 1551 ,' Leonard ',' Babbs ', 01959 8159688 ,' 46 Zoophagy Green ',' Choppington ',' NE41 5DB ',' l.babbs#sommelier.co.uk ')
Your problem is here: 01959 8159688. This is an invalid number literal.
Depending on Phone column type, it's got to be: '01959 8159688' (if it is a text column), or 01959.8159688 (if it is a numeric column).
The problem is with 01959 8159688.
Assuming this is a phone number, and you want to keep the space in order to separate the area code from the rest of the number, you should surround it with single quotes: '01959 8159688' - otherwise, it's interpreted as two unrelated numeric literals.
My suggestion is to format your queries like this:
insert into yourtable (
field1
, field2
, etc
)
values (
value1
, value2
, etc
)
It makes the commas more visible. It also makes counting easier since you need the same number of fields and values. Finally, it makes commenting easier if you need to find a problematic part of your query.

Detect \n character saved in SQLite table?

I designed a table with a column whose data contains \n character (as the separator, I used this instead of comma or anything else). It must save the \n characters OK because after loading the table into a DataTable object, I can split the values into arrays of string with the separator '\n' like this:
DataTable dt = LoadTable("myTableName");
DataRow dr = dt.Rows[0]; //suppose this row has the data with \n character.
string[] s = dr["myColumn"].ToString().Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);//This gives result as I expect, e.g an array of 2 or 3 strings depending on what I saved before.
That means '\n' does exist in my table column. But when I tried selecting only rows which contain \n character at myColumn, it gave no rows returned, like this:
--use charindex
SELECT * FROM MyTable WHERE CHARINDEX('\n',MyColumn,0) > 0
--use like
SELECT * FROM MyTable WHERE MyColumn LIKE '%\n%'
I wonder if my queries are wrong?
I've also tested with both '\r\n' and '\r' but the result was the same.
How can I detect if the rows contain '\n' character in my table? This is required to select the rows I want (this is by my design when choosing '\n' as the separator).
Thank you very much in advance!
Since \n is the ASCII linefeed character try this:
SELECT *
FROM MyTable
WHERE MyColumn LIKE '%' || X'0A' || '%'
Sorry this is just a guess; I don't use SQLite myself.
Maybe you should just be looking for carriage returns if you arent storing the "\n" literal in the field. Something like
SELECT *
FROM table
WHERE column LIKE '%
%'
or select * from table where column like '%'+char(13)+'%' or column like '%'+char(10)+'%'
(Not sure if char(13) and 10 work for SQLite
UPDATED: Just found someone's solution here They recommend to replace the carriage returns
So if you want to replace them and strip the returns, you could
update yourtable set yourCol = replace(yourcol, '
', ' ');
The following should do it for you
SELECT *
FROM your_table
WHERE your_column LIKE '%' + CHAR(10) + '%'
If you want to test for carriage return use CHAR(13) instead or combine them.
I've found a solution myself. There is few way (with some dedicated function) to convert ascii code to symbol in SQLite at the moment (CHAR function is not support and using '\n' or '\r' directly doesn't work). But we can convert using CAST function and passing in a Hex string (specified by append X or x before the string) in SQLite like this:
-- use CHARINDEX
SELECT * FROM MyTable WHERE CHARINDEX(CAST(x'0A' AS text),MyColumn,0) > 0
-- use LIKE
SELECT * FROM MyTable WHERE MyColumn LIKE '%' || CAST(x'0A' AS text) || '%'
The Hex string '0A' is equal to 10 in ascii code (\r). I've tried with '0D' (13 or '\n') but it won't work. Maybe the \n character is turned to \r after being saved in to SQLite table.
Hope this helps others! Thanks!