I have a table that has just a code for the individual column name like "A1G", "Z8H" etc.
This table is a really huge table. Therefore it would not help to manually add the alias name in the SELECT Statement like.
The description for each column code is stored in a different table:
Now I would like to SELECT * from the first table but with the right column header name.
This is stored in the second table within the filters Schema = 'ABC' and Table = 'A'.
That would be desired output:
How would you do that in SQL?
How can I change the column name?
You would be better off just creating a view with all the columns aliased to your preferred names. Once that's done you can select from the view and get the data back with the headings you want.
Look into Inner Join
Or Left Join.
Related
i didn't find the right answear here. So I work with a lot of different tables. The Schema of the Tables are GDBADMP.[Table_Name] or USCH1060.[Table_Name].
Now i search a way to list all tables with an exact column Name like PLZ_ID. The column contains different values, somtimes varchar or int
Like: Show all Tables (GDBADMP.[Table_Name]) with the column Name PLZ_ID
My first thoughts are like this:
SELECT *
FROM GDBADMP.*
WHERE PLZ_ID
Kind regards
Look at the syscat.columns catalog view. You may join it to syscat.tables by (tabschema, tabname) to get tables only (excluding views, for example).
Thanks to this comment. I found a solution!
SELECT *
FROM SYSCAT.COLUMNS a
WHERE a.colname = 'PLZ_ID'
I suggest using SYSIBM.SYSCOLUMNS which is a system table that contains all the fields in all tables.
Below a sample query that will return the tables in a form of creator.table:
SELECT TRIM(TBCREATOR)||'.'||TRIM(TBNAME) AS FULLTABLE
FROM SYSIBM.SYSCOLUMNS
WHERE NAME='PLZ_ID'
Is there a way in SQL server to alias column names for a particular table and store the aliases somewhere such that you can access the aliases while querying? I have a table where I cannot change column names and I am trying to figure out if there is a way to alias them to make them more user friendly.
CREATE VIEW vw_RenameTable
AS
Begin
Select GoodName1 = DumbName1
,GoodName2 = DumbName2
From MyTable
End
A view would be the natural answer to the question. But, if you want to access the columns in the same table, you can use computed columns:
alter table mytable add bettercolumn as [Bad Ugly Name];
You can then use the computed column in a select on the same table.
I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory
I have one table named tblalias.which is having two columns cid, description
cid description
1 Employee
2 Join Date
3 Retire Date
Like this three record is present
Now I have another table tblemployee. I want to write a query for tblemployee to get record but alias name for that query I want should come from tblalias
select nama as Employee,
joindate as "Join Date",
retiredate as "Retire Date"
from tblemployee
If I change value is tblalias table to my select query should return new value as alias is it possible if yes how please help me
The only way to do this is with dynamic SQL. First fetch the alias names then build the final SQL and execute it.
There is no way doing this with a single "hardcoded" statement.
If you want spaces in names you should quote them. (spaces in names is generally a bad Idea, but that's another matter)
I'm trying to create a sql query, but there is this error:
Ambiguous column name 'description'.
Its because this column occurs in both tables.
if I remove the description from the query, it works.
I tried to rename the description-field "AS description_pointer", but the error still occurs.
SELECT TOP 1000 [activityid]
,[activitytypecodename]
,[subject]
,[regardingobjectid]
,[contactid]
,[new_crmid]
,[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as I
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact]
ON I.[regardingobjectid] = [crmtestext_MSCRM].[dbo].[FilteredContact].[contactid]
WHERE new_crmid not like '%Null%' AND activitytypecodename like '%E-mail%'
Both tables coming into play in the query have a column named description. You RDBMS cannot guess which column table you actually want.
You need to prefix the column name with the table name (or table alias) to disambiguate it.
Bottom line, it is a good practice to always prefix column names with table names or aliases as soon as several tables come into play in a query. This avoids the issue that you are seeing here and make the queries easier to understand for the poor souls that have no knowledge of the underlying schema.
Here is an updated version of your query with table aliases and column prefixes. Obviously you need to review each column to put the correct alias:
SELECT TOP 1000
i.[activityid]
,i.[activitytypecodename]
,i.[subject]
,c.[regardingobjectid]
,c.[contactid]
,c.[new_crmid]
,c.[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as i
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact] as c
ON i.[regardingobjectid] = c.[contactid]
WHERE i.new_crmid not like '%Null%' AND i.activitytypecodename like '%E-mail%'