INSERT INTO SELECT - shrink 2 columns in one - sql

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$

Related

Concatenating columns in SQL with delimiter. If first column is null, do not want delimiter to show up after it. How do I get rid of that?

SELECT TOP 1000 places.*
, home_Desc
, CONCAT_WS(' - ', COALESCE(brand, ' '), home_Desc, location_Desc) AS homeSite
Example:
brand
home_Desc
location_Desc
blue
large
woody
NULL
small
forest
So right now I am getting:
1.' blue - large - woody '
2. ' - small - forest '
But what I want for the second set is:
small - forest
CREATE TABLE TEST(brand VARCHAR(100), home_Desc VARCHAR(100), location_Desc VARCHAR(100))
INSERT INTO TEST VALUES ('blue','large','woody'),
(NULL,'small','forest')
SELECT CONCAT_WS(' - ',brand, home_Desc, location_Desc) FROM TEST
As Squirrel say, you must replace COALESCE(brand, '') to brand
Taking advantage of the rule a non-NULL value (the dash character in this instance) concatenated with a NULL value (branch, in this instance) would yield NULL (and assuming that the other two can never be null/blank:
, COALESCE(brand + '-', ' ') + home_Desc + ' - ' + location_Desc AS homeSite

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 find repeating numbers in a column in SQL server . Eg 11111, 33333333, 5555555555,7777777 etc

I need to identify repeated numbers( Eg: 1111, 33333333, 5555555555,777777777 etc.) in a column.
How can I do this in sql server without having to hard code every scenario. The max length is 10 of the column. Any help is appreciated.
This will check if the column has all the same value in it.
SELECT *
FROM tablename
WHERE columnname = REPLICATE(LEFT(columnname,1),LEN(columnname))
As Nicholas Cary notes, if the column is numbers you'd need to cast as varchar first:
SELECT *
FROM tablename
WHERE CAST(columnname AS VARCHAR(10)) = REPLICATE(LEFT(CAST(columnname AS VARCHAR(10)),1),LEN(CAST(columnname AS VARCHAR(10))))
Riffing on #Dave.Gugg's excellent answer, here's another way, using patindex() to look for a character different than the first.
select *
from some_table t
where 0 = patindex( '[^' + left(t.some_column,1) + ']' , t.some_column )
Again, this only works for string types (char,varchar, etc.). Numeric types such as int will need to be converted first.

How to remove text followed by a special characted in MSQL

I have a table named 'country' with a column named 'name'
and the names in this column appears with translation followed by special character '/'.
IRELAND/IRLANDE
GREECE/GRÈCE
DENMARK/DANEMARK
Now i want only the countrynames before this special character'/' so the out put should look like this..
IRELAND
GREECE
DENMARK
please help. Thanks in advance
create table #t(name nvarchar(40))
insert into #t values('IRELAND/IRLANDE')
,('GREECE/GRÈCE')
,('DENMARK/DANEMARK')
select substring(name,0,CHARINDEX('/',name)) from #t
Why LEN(names)-CHARINDEX('/',names)?
try using CHARINDEX('/',names)+1 instead.
As far as I remember, SUBSTRING's third variables should be the length of the desired output string.
A more complex query to help you out:
DECLARE #Text VARCHAR(50) = 'IRELAND/IRLANDE GREECE/GRÈCE DENMARK/DANEMARK'
SELECT
SUBSTRING(SUBSTRING(#Text,0,CHARINDEX(' ',#Text,0)),0,CHARINDEX('/',#Text,0))
+
SUBSTRING(SUBSTRING(#Text,CHARINDEX(' ',#Text,0),CHARINDEX(' ',#Text,0)),0,CHARINDEX('/',#Text,0))
+ ' '
+
REPLACE(REVERSE(SUBSTRING(REVERSE(#Text),CHARINDEX('/',#Text,0) + 1 ,CHARINDEX('/',#Text,0))),'/', ' ')

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.