Need help finding syntax in SQL developer - sql

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.

Related

SQL code to get the first letter of two strings

I am trying to get the first letter of a single name and full name.
For example
Name
Alex Patterson
Alex
Output should be
A P
A
Can someone help me with to achieve this?
For recent SQL Server:
SELECT Name, STRING_AGG (initial, ' ') as full_initial
FROM (
SELECT Name, SUBSTRING(value,1,1) as initial
FROM people
CROSS APPLY STRING_SPLIT(name, ' ')
) t
GROUP BY Name

How to remove space from phone number (SQL)

I have phone numbers in the following format:
03 12345678 and 0412 3456789
I need to remove the space from the numbers so that I can join to another table where number format is 0312345679 and 04123456789. I do not want to update the table.
I have tried to run the following query for the home number format, but keep getting an error:
SELECT
REPLACE(p.Home_Phone_Num, ' ', '') AS Home_Num
FROM table
The error:
Syntax error: expected something between the 'SELECT' keyword and the 'REPLACE' keyword.
Thanks
This looks like a Teradata error message. This database does not have a replace() function - instead, you need oreplace():
select oreplace(p.Home_Phone_Num, ' ', '') as Home_Num from mytable
To remove single characters there's no need for oReplace, use oTranslate instead:
oTranslate (p.Home_Phone_Num, ' ', '') AS Home_Num
This might also replace additional characters
oTranslate (p.Home_Phone_Num, ' -/()', '') AS Home_Num

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)

Unexpected execution in an update query in SQL

I am getting an 'Unexpected' result with an update query in SQL Server 2012.
This is what I am trying to do.
From a column (IDENTIFIER) composed by an ID ','name (e.g. 258967,Sarah Jones), I have to fill other two columns: ID and SELLER_NAME.
The original column has some values with a blank at the end and the rest with out it:
'258967,Sarah Jones'
'98745,Richard James '
This is the update query that I am executing:
UPDATE SELLER
SET
IDENTIFIER = LTRIM(RTRIM(IDENTIFIER)),
ID = Left(IDENTIFIER , charindex(',', IDENTIFIER )-1),
SELLER_NAME = UPPER(RIGHT((IDENTIFIER ),LEN(IDENTIFIER )-CHARINDEX(',',IDENTIFIER )));
But I am having a wrong result at the end
258967,Sarah Jones 258967 SARAH JONES
98745,Richard James 98745 ICHARD JAMES
The same happens with all the names that has the blank at the end. At this point I wonder, if I have specified that I want to eliminate all the blanks at the begining and at the end of the value of IDENTIFIER as a first action, why the system updates the ID and SELLER_NAMES and then does this action?.
Just to specify: The IDENTIFIER column is part of the seller table which is updating from another person that imports the data from an Excel file. I receive this values and I have to normalize the information. I only can read the SELLER table, take this into account before answer
Try this, because you have space in right side of name, so it will just truncate one char from name. So just need to RTRIM(IDENTIFIER) and thats it.
SELLER_NAME = UPPER(RIGHT((RTRIM(IDENTIFIER)),LEN(IDENTIFIER )-CHARINDEX(',',IDENTIFIER)));
The design of your tables violates 1NF and is nothing but painful. Instead of doing all this crazy string manipulation you could leverage PARSENAME here quite easily.
with Something(SomeValue) as
(
select '258967,Sarah Jones' union all
select '98745,Richard James '
)
select *
, ltrim(rtrim(PARSENAME(replace(SomeValue, ',', '.'), 2)))
, ltrim(rtrim(PARSENAME(replace(SomeValue, ',', '.'), 1)))
from Something
Instead of using Right(), use SubString().
Here's an example. I've tried to show each step individually to illustrate
; WITH x (identifier) AS (
SELECT '258967,Sarah Jones'
UNION ALL
SELECT '98745,Richar James '
)
SELECT identifier
, CharIndex(',', identifier) As comma
, SubString(identifier, CharIndex(',', identifier) + 1, 1000) As name_only
, LTrim(RTrim(SubString(identifier, CharIndex(',', identifier) + 1, 1000))) As trimmed_name_only
FROM x
Note that the 1000 used should be the maximum length of the column definition or higher e.g. if your IDENTIFIER column is a varchar(2000) then use 2,000 instead.
try trim the IDENTIFIER first like this
SALLER_NAME = UPPER(RIGHT((RTRIM(IDENTIFIER),LEN(IDENTIFIER )-CHARINDEX(',',IDENTIFIER )));

INSERT INTO SELECT - shrink 2 columns in one

I'm trying to shrink two columns - note and app from dbo.UCAST3$ - into column klient.appendix
This is how I try to shrink 2 columns with small note: [note] + ' appendix: ' + [app]
I tried following:
INSERT INTO dbo.klient
(name, surname, rodcis, nopass, street, zip, city, appendix, tel, fax, titul, akce, rocnik)
SELECT
[nameorig], [surnameorig], [rodcisorig], [nopassorig], [adresa], [ZIP], [place],
[note] + ' appendix: ' + [app], [telhome], [telwork], titul, '000000-00', 2014
FROM dbo.UCAST3$
I get following error:
Error converting data type varchar to numeric.
It seems that it is skipping to next column nopassorig which is numeric.
Can anyone please help me solve this out?
[note] is decimal(30,0)
[app] is nvarchar(255)
Never, never, never store multiple values in one column!
Please don't do it. If you ever want to seperate the values again in SQL this will be a pain and slow.
Just concatete the values in the SQL select statments where you need it.
You need to cast [note] as varchar to successfully concatenate.
INSERT INTO dbo.klient
(name, surname,rodcis,nopass,street,zip,city,appendix,tel,fax,titul,akce,rocnik)
SELECT [nameorig], [surnameorig],[rodcisorig],
[nopassorig],[adresa],[ZIP],[place],
CAST([note] as VARCHAR(30)) + ' appendix: ' + [app],
[telhome],[telwork],titul,'000000-00',2014
FROM dbo.UCAST3$