I'm inserting into my database here with parameters from the form.
The form has 4 input fields (current_no, current_street, current_city, current_state). I've set it up so that the client can display each of these values on the web page, but what they want is to be able to view these values in a single column.
so basically from...
current_no | current_street | current_city | current_state
to...
| current_no current_street current_city current_state |
I decided I'd add another column to the table that'll store this "full" value and I figured that populating it would probably be easy but I think I'm missing something here:
insert into customers(current_address) values (current_no + ' ' + current_street + ' ' + current_city + ' ' + current_state)
Although this doesn't give me any errors, it only inserts the current_no value into the column.
How can I concatenate all the values (string types) into the column on database side?
Thanks in advance for any help!
in mySQL use CONCAT()
insert into customers(current_address) values
(
(CONCAT(current_no,' ',current_street,' ',current_city,' ',current_state)
)
or CONCAT_WS()
insert into customers(current_address) values
(
(CONCAT_WS(' ', current_no,current_street,current_city,current_state)
)
Try using
INSERT INTO customers (current_address)
SELECT
CONCAT_WS(' ', current_no, current_street, current_city, current_state)
Others have suggested CONCAT_WS and that's right. The + operator the SQL language doesn't concatenate strings, as you have discovered.
But, please think about your plan of adding a new column to your table for this. New columns with redundant data aren't usually a great idea.
If you already have the columns with the data in them, I suggest you use a SELECT query when you read the table to create your concatenated column. This will take advantage of the data you already have without placing redundant data in the table.
SELECT CONCAT_WS(' ', current_no,current_street,current_city,current_state)
AS current_address
Related
I'm trying to join two columns of the same table but, if there are null values (in my case they are in the second column), I want to take anyway the row that interests me and, rather than putting the null value, I would put ''. The columns that I want to join are Surname and Name. In other words, I tried to use :
SELECT CONCAT(CSurname, ' ', CName)
FROM Client;
In this way if I have a valid value for surname and a null value for name I obtain null. I use MySql, thanks.
If you want to avoid the problem with a leading space, then the easiest way is probably CONCAT_WS():
SELECT CONCAT_WS(' ', CSurname, CName)
FROM Client;
Unlike most other functions, CONCAT_WS() ignores NULL values (except for the separator), greatly simplifying this logic -- particularly when you are working with more than two columns.
Without it, the equivalent logic could be expressed as:
SELECT CONCAT(COALESCE(CONCAT(CSurname, ' '), ''), COALESCE(CName, ''))
Try the ifnull function
SELECT CONCAT(CSurname, ' ', IFNULL(CName,'')) FROM Client;
I don't have a local mysql installation to try it out but the IFNULL function should achieve what you need.
eg : field name = User_id
Value=abc later i want to insert xyz without disturbing abc Value= abc,xyz i want to insert efg without disturbing abc then Value= abc,xyz,efg and so on
i want to seperating each value by using ","(comma). can any one help me out
In MySQL you could often refer to the value of a column just by using the column name. And to concatenate strings with a separator there's a nifty function called concat_ws (concat with separator).
In your case the code would look something like
UPDATE YourTable
SET Value = CONCAT_WS(',', Value, 'cde')
WHERE User_id = 123;
Good Luck!
MySQL CONCAT_WS() function is used to join two or more strings with separator. The separator specified in the first argument is added between two strings. The separator itself can be a string. If the separator is NULL the result is NULL.
Click hear for more information
Address1,
Address2,
TownCity,
Region,
Postcode,
Country,
Hi guys and gals,
I got a SQL problem.if you could help me with it that'd be awesome.
The table(softc) on top has couple of columns that id like to merge into a single column called "contactinfo"
Columns that needed to be merged:
Address1,
Address2,
TownCity,
Region,
Postcode,
Country,
I've tried UNION but it filters out rows with NULL values.I have 618 records.when i do a UNION it comes up with 750.(some records might contain all NULLs.but they are necessary to map the single into another table)
appreciate your help.thanks
I'm Working with mySQL
Try this
select ISNULL(Address1,'') + ISNULL(' '+Address2,'') + ISNULL(', '+TownCity,'')
+ ISNULL(', '+Region,'') + ISNULL(', '+Postcode,'') + ISNULL(', '+Country,'') as ContactInfo from Softc
You can combine combine / merge cells with Concat command
Example :
CONCAT([Firstname],',',[lastname]) as Fullname.
expression = concat[fieldname/seperator], [fieldname/seperator], ............... etc
Part of database normalization is to break data into it's most discrete components. You have that now. Don't bollocks it up.
A good long term solution to your problem is to update all your null values to empty strings. Then alter those columns so that they don't allow nulls and have a default value of empty strings. This will allow your concatonation queries to run faster because you won't have to check for null values.
Also, if the requirement to return a contact info string is frequent, you can write a database function that returns it.
select IFNull(Address1,'') + IFNull(Address2,'') + IFNull(TownCity,'') + IFNull(Region,'') + IFNull(Postcode,'') + IFNull(Country,'') as ContactInfo from Softc
That should be what you want...
I want to display two columns of different types or two column data of same type that will be displayed in one column.
The types are date + time, or varchar + varchar etc
I know how to concat strings (add a string to one column) but can't seem to do it for two columns data.
Say I want to display two columns both varchar type, fname + lname = Ajay Punja
Or
Lname + DOB = Punja 01/01/2001
I tried using single and double pipes, plus signs etc but always returns 0.
Is it because I need to convert two different data types into one matching data type? But, both varchar types returns 0.
I think it will help you.
SELECT CONCAT(2, ' test') as result form table_name;
It is also possible to convert a number to a string explicitly using the CAST() function. Conversion occurs implicitly with the CONCAT() function because it expects string arguments. e.g.
SELECT 38.8, CAST(38.8 AS CHAR);
My answer thanks to everyone here :)
SELECT CONCAT(fname, ' ', DOB) as Results FROM Person;
SELECT CONCAT(fname, ' ', lname) as Results FROM Person;
Now I understand how to use CONCAT properly. Before I thought for each bracket it must contain only one attribute (or string) and then CONCAT it with enough bracket of data, but clearly that's wrong.
Wrong example (fail attempt):
SELECT CONCAT(fname, '') + (' ', lname)) as Results FROM Person;
I have 60 columns and I need to get unique values from all the columns using a SQL query. Is it possible in SQL Server?
Note I need to find distinct values from all the columns
please try this code
declare #Sql_Str varchar(8000)='';
select #Sql_Str=#Sql_Str+' select cast (' +name +' as varchar(500)) from
<yourtable> union' from sys.columns where [object_id]=object_id('<yourtable>')
set #Sql_Str=SUBSTRING(#Sql_Str,1,len(#Sql_Str)-6)
exec(#Sql_Str)
Drag the columns heading in the SSMS object explorer to the query window.
Find and replace the ", " with "\n" using Regular Expressions.
Generate a SELECT INTO distinct query for each column using Regular Expressions for each column and assign the output to a temp table built by the Regular Expression.
Make use of the data in your temp tables.