Select data from table with column name and description SQL Server - sql

I have a legacy table with all column named in an old way, the names don't make sense to others, but the table description contains column description, how to can select all data from the table, and combine with the column description?
UPDATED BELOW:
To get the Names and Columns Description
SELECT
COLUMN_NAME AS Name,
COLUMN_TEXT AS Description
FROM
[DB2-LINKED-SERVER].[BD2].QSYS2.SYSCOLUMNS
WHERE
TABLE_NAME = 'ITMHED'
I got:
Name Description
ITMNO Item Number
ITMNM Item Name
.... 800+ rows more
Then I have another query:
SELECT * FROM [DB2-LINKED-SERVER].[BD2].ITMHED
It returned me:
ITMNO ITMNM ...800+ more columns
AB-001 Mountain Bike ....
What I want to get:
Item Number Item Name ...800+ more columns
AB-001 Mountain Bike .....
If I need only 2-3 column, I can manually rename them, but with that many record, I want to make it more readable for users. I need to generate a report from that.

SELECT
COLUMN_NAME AS Name + ' as '+
COLUMN_TEXT AS Description + ','
FROM
[DB2-LINKED-SERVER].[BD2].QSYS2.SYSCOLUMNS
WHERE
TABLE_NAME = 'ITMHED'
Could get the output from that and then insert it into the following:
select (insert the output from above here) from [DB2-LINKED-SERVER].[BD2].ITMHED

Related

SQL table column forming from another columns

I have a create table query, where I'm creating a table from another table's data. But I'm supposed to create new column from the same row's data.
In example below, I need to get the first letter from ID from each row.
For example what I have tried:
SELECT (SELECT SUBSTRING((SELECT ID
FROM tableWhereDataComes), 1, 1)) AS PERSONTYPE,
ID , --This is a string like M101 or F101 etc.
FIRSTNAME,
LASTNAME
FROM tableWhereDataComes
How do I get the required M or F to the first column PERSONTYPE?
Are you just looking for LEFT()?
SELECT LEFT(ID, 1) AS PERSONTYPE, ID, FIRSTNAME, LASTNAME
FROM tableWhereDataComes;
I would note that this is probably not necessary. You can add a computed column to the table:
alter table tableWhereDataComes add persontype as (left(id, 1));
This is then calculated when the table is queried (or if persisted whenever the data changes), so it is always up-to-date.

SQL - join on free text field

I have two tables from two different databases that I want to join together but I don't have a column on which this can be easily done by a join.
In table A I have a table with server names
In table B I have a table where one of the columns has a free text field (description).
I want to be able to create a search that searches for the server name within the description column and then add that description column onto the end of table A.
For example:
Table A Table B
name date
server description
customer
Output
name
server
customer
description (join on searching for server name in description)
If you don't have a full text index on description, then you can use like:
SELECT A.name, A.server, A.customer, B.description
FROM A JOIN
B
ON ' ' + B.description + ' ' LIKE '% ' + A.server ' %';

Create view with except condition

Example i want to create a view name TEST
which i already created a view before that
View name : customer
Inside customer View:
//CUSTOMER
NAME ID ADDRESS AGE SEX TELNO EMAIL
-----------------------------------------------------------------------
CHRIS 1 12321312 21 F 646885 ascs#gmail
JOHN 2 SADASDSA 23 M 5452131 asd#gmail
MAY 3 LKJLKJLKJ 32 F 645643 cxz#gmail
So i want to create a view name TEST that will store all column inside CUSTOMER but EXCEPT TELNO EMAIL.
so i used this query:
CREATE VIEW TEST AS
SELECT * FROM CUSTOMER
EXCEPT
SELECT TELNO,EMAIL FROM CUSTOMER;
But i fail to work, got errors come out. SQL command not properly end and point to EXCEPT, what's wrong?
You have to list all the columns that you want explicitly:
CREATE VIEW TEST AS
SELECT NAME, ID, ADDRESS, AGE, SEX
FROM CUSTOMER;
There is no way to exclude some columns from a * list.
EXCEPT is an operator in SQL Server. The equivalent in Oracle is MINUS. However, this works at the row level, not at the column level.
If you want to get all the columns in the table, except for those two, you can use all_tab_columns:
select column_name
from all_tab_columns
where lower(table_name) = 'customer' and
lower(column_name) not in ('telno', 'email');
You can then paste them into the select clause.

SQL using where contains to return rows based on the content of another table

I need some help:
I have a table called Countries, which has a column named Town and a column named Country.
Then I have table named Stores, which has several columns (it is a very badly set up table) but the ones that are important are the columns named Address1 and Address2.
I want to return all of the rows in Stores where Address1 and Address2 contains the towns in the Countries table.
I have a feeling this is a simple solution but I just can't see it.
It would help if maybe you could use WHERE CONTAINS but in your parameters search in another table's column?
e.g.
SELECT *
FROM Stores
WHERE CONTAINS (Address1, 'Select Towns from Countries')
but obviously that is not possible, is there a simple solution for this?
You're close
SELECT * FROM Stores s
WHERE EXISTS (
SELECT * FROM Countries
WHERE CONTAINS(s.Address1, Town) OR CONTAINS(s.Address2, Town)
)
This would be my first attempt:
select * from stores s
where
exists
(
select 1 from countries c
where s.Address1 + s.Address2 like '%'+c.Town+'%'
)
Edit: Ooops just saw that you want the 'CONTAINS' clause. Then take Paul's solution

Select from one column and insert into another column with some string appended?

I have a table countries with three columns country_name, country_code, country_flag.
The table has 250 rows but the country_flag column is all null at the moment because I have created it now.
The purpose of this column is to contain a path to the flag file (Image) of that country like Images\Flags\UK.gif (each flag file is named as the country code). Now I want to write a query that should pick country code from the country_code column and append the Images\Flags\ string at the start and .gif at the end, something like this
'Images\Flags\' + country_code + '.gif'
and then update the country_flag column with this value.
If this is possible then it will update the table with less effort and I'll get all image paths in the country_flag column.
Try this
Update countries set country_flag = 'Images\Flags\' + country_code + '.gif'