I've tried
'field1'+'field2'+'field3' as combination
and tried:
((((field_1||'_')||field_2_yes)||'_')||field_3_yes) AS combination
tried:
field1 || field2 as combination
i've also tried Concat, coalesce, can't seem to get it to work.
Also I am querying SQL server through excel if that helps.
Expected output is a column named combination with the field1field2field3 data as the rows within combination column. I'm am not trying to create the literal string field1field2field3 but combine the data from these fields into a string and display in new column.
The issue is with the data types as to why these fields are not combining.
Could you please try without aposthropes?:
select combination = (field1 + field2)
from table
In case you need to put a space between the values, if field1 and field2 is varchar, then you can do:
select combination = (field1 + ' ' + field2)
from table
If field1 and field2 is not varchar, then you can cast like below and use in your main query:
cast(field1 as varchar(50))
So do you want only to display 2 fields as a one field?
Field1 = 'hello'
field2 = 'world'
select field1 + field2 as concatfield from table
select concat(field1, field2) as concatfield from table
This will give you output like that:
helloworld
If you want to add space then try this:
select field1 + ' ' + field2 as concatfield from table
select concat(field1, ' ', field2) as concatfield from table
This will give you output like that:
hello world
And then exacly the same if you want to update a field in DB
update table
set concatfield = field1 + ' ' + field2 --or concat(field1, ' ', field2)
--where 1=1
It is pretty basic
declare #Field varchar(10) = 'value'
select #Field + #Field + #Field as comb
select '#Field' + '#Field' + '#Field' as comp
comb
------------------------------
valuevaluevalue
comp
------------------
#Field#Field#Field
Related
I have a database with many tables and that table has a common column. How can I retrieve that table which have same value in that column?
ex:-
I have 25 table, all tables have a column name CCODE now I want to know which tables have same value for this column?
The following statement will create an UNION SELECT what brings back all the data you need in one result set. Best is to set the query output to text and don't forget to set the query option max text to highest (8192). Take the result of this SELECT into a new SQL window and execute it:
WITH AllTablesWithMyColumn AS
(
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME='CCODE'
)
SELECT STUFF(
(
SELECT 'UNION SELECT ''' + TABLE_NAME + ''' AS TableName, CCODE FROM ' + TABLE_NAME + CHAR(13) + CHAR(10)
FROM AllTablesWithMyColumn
FOR XML PATH(''),TYPE
).value('.','varchar(max)'),1,6,'')
If you need any further help, just tell me...
this is how my table looks like :
Now i want to retrieve Field1 for 'AAA'
I tried something like :
select Field1 from table where CommaSeparatedList='AAA'
But it didn't work.
So How can I achieve this ??
select field1
from table
where ',' + replace(commaseparatedlist, ' ', '') + ',' like '%,AAA,%'
I suggest that you try:
SELECT Field1 FROM table WHERE CommaSeparatedList LIKE '%AAA%'
If you just need the column to contain the value you are looking for, you could do something like this:
SELECT Field1
FROM table
WHERE CommaSeparatedList LIKE '%AAA%'
Is it possible to select 2 columns in just one and combine them?
Example:
select something + somethingElse as onlyOneColumn from someTable
(SELECT column1 as column FROM table )
UNION
(SELECT column2 as column FROM table )
Yes, just like you did:
select something + somethingElse as onlyOneColumn from someTable
If you queried the database, you would have gotten the right answer.
What happens is you ask for an expression. A very simple expression is just a column name, a more complicated expression can have formulas etc in it.
Yes,
SELECT CONCAT(field1, field2) AS WHOLENAME FROM TABLE
WHERE ...
will result in data set like:
WHOLENAME
field1field2
None of the other answers worked for me but this did:
SELECT CONCAT(Cust_First, ' ', Cust_Last) AS CustName FROM customer
Yes it's possible, as long as the datatypes are compatible. If they aren't, use a CONVERT() or CAST()
SELECT firstname + ' ' + lastname AS name FROM customers
The + operator should do the trick just fine. Keep something in mind though, if one of the columns is null or does not have any value, it will give you a NULL result. Instead, combine + with the function COALESCE and you'll be set.
Here is an example:
SELECT COALESCE(column1,'') + COALESCE(column2,'') FROM table1.
For this example, if column1 is NULL, then the results of column2 will show up, instead of a simple NULL.
Hope this helps!
To complete the answer of #Pete Carter, I would add an "ALL" on the UNION (if you need to keep the duplicate entries).
(SELECT column1 as column FROM table )
UNION ALL
(SELECT column2 as column FROM table )
DROP TABLE IF EXISTS #9
CREATE TABLE #9
(
USER1 int
,USER2 int
)
INSERT INTO #9
VALUES(1, 2), (1, 3), (1, 4), (2, 3)
------------------------------------------------
(SELECT USER1 AS 'column' from #9)
UNION ALL
(SELECT USER2 AS 'column' from #9)
Would then return : Result
Yes, you can combine columns easily enough such as concatenating character data:
select col1 | col 2 as bothcols from tbl ...
or adding (for example) numeric data:
select col1 + col2 as bothcols from tbl ...
In both those cases, you end up with a single column bothcols, which contains the combined data. You may have to coerce the data type if the columns are not compatible.
if one of the column is number i have experienced the oracle will think '+' as sum operator instead concatenation.
eg:
select (id + name) as one from table 1; (id is numeric)
throws invalid number exception
in such case you can || operator which is concatenation.
select (id || name) as one from table 1;
Your syntax should work, maybe add a space between the colums like
SELECT something + ' ' + somethingElse as onlyOneColumn FROM someTable
I hope this answer helps:
SELECT (CAST(id AS NVARCHAR)+','+name) AS COMBINED_COLUMN FROM TABLENAME;
select column1 || ' ' || column2 as whole_name FROM tablename;
Here || is the concat operator used for concatenating them to single column and ('') inside || used for space between two columns.
SELECT firstname || ' ' || lastname FROM users;
I've picked up some SQL similar to the following:
IF EXISTS(SELECT name FROM tempdb..sysobjects WHERE name Like N'#tmp%'
and id=object_id('tempdb..#tmp'))
DROP TABLE #tmp
into #tmp
select * from permTable
I need to add more data to #tmp before continuing processing:
insert into #tmp
select * from permTable2
But this gives errors because SQL has assumed sizes and types for #tmp columns (e.g. if permTable has a column full of ints but permTable2 has column with same name but with a NULL in one record you get "Cannot insert the value NULL into column 'IsPremium', table 'tempdb.dbo.#tmp").
How do I get #tmp to have the types I want? Is this really bad practise?
Have you considered creating a table var instead? You can declare the columns like such
declare #sometable table(
SomeField [nvarchar](15),
SomeOtherField [decimal](15,2));
This is why select into is a poor idea for your problem. Create the table structure specifically with a create table command and then write two insert statements.
It isn't possible.
If you need to generate the table definition typelist now,
create a view from the select statement, and read the columns and their definition from information_schema... (this work of art won't consider decimal and/or datetime2)
Note: this will give you the lowest possible field-length for varchar/varbinary columns you currently selected. You need to adjust them manually...
SELECT
','
+ COLUMN_NAME
+ ' '
+ DATA_TYPE
+ ' '
+ ISNULL
(
'('
+
CASE
WHEN CHARACTER_MAXIMUM_LENGTH = -1
THEN 'MAX'
ELSE CAST(CHARACTER_MAXIMUM_LENGTH AS varchar(36))
END
+ ')'
, ''
)
+ ' '
+ CASE WHEN IS_NULLABLE = 'NO' THEN 'NOT NULL' ELSE '' END
FROM information_schema.columns
WHERE table_name = '________theF'
ORDER BY ORDINAL_POSITION
And the field-list for the insert-statement:
SELECT
',' + COLUMN_NAME
FROM information_schema.columns
WHERE table_name = '________theF'
ORDER BY ORDINAL_POSITION
I have a table in SQL Server 2005 filled with data. Is there a method by which I can generate update statements including the data in it?
There's a nice free tool here http://www.lss.co.uk/Products/LiveDataScript/
If you don't find any better solution, try this:
SELECT
'UPDATE [Table] (field1, field2, field3) Values ('
+ field1 + ', '
+ field2 + ', '
+ field3 + ')'
FROM [Table]
I know, not nice.
Go here and get the Microsoft SQL Server Management Studio Express SSMSE Download page
After installing you can then connect to your database with the tool and generate several types of "vanilla" scripts.
If you mean data on the same row, just use it in the Update Statement
Update MyTable Set ColumnA = ColumnB + ColumnC
If you want to use data from other rows, you probably have to join it back to it's self
Update a
Set ColumnA = b.ColumnD
From MyTable a
Join MyTable b on a.ColumnB = b.ColumnC
SELECT
'UPDATE [Table] SET field1 = ' + field1 + ' , field2 = ' + field2 + ' , field3 = ' + field3 + ' WHERE <condition> ' FROM <Table>
Use extra single Quotes wherever string data is updated.