In MySQL, the renamming the the fetched result can be renamed through 'c1 and c2', or 'c1 c2'. In the Documentation, it says 'c1 and c2' is supported , but nothing can be found about 'c1 c2'.
Is 'select c1 c2 from table' supported by TDengine?
select c1,c2 from table
or if you want to use alias
select c1 as xxx, c2 as xxx from table
You can also try to use select c1 xxx, c2 xxx from table, which ignores the as keywords.
Related
As we live in a World of "delivery first", we now found our self with databases that are infected by tons of index hints.
I wrote a query that might help me find out all the WITH (INDEX= in the database:
SELECT
DB_NAME() AS DB_NAME,
name AS Object_Name,
type_desc AS Object_Type,
definition
FROM
sys.sql_modules
INNER JOIN
sys.objects ON sys.sql_modules.object_id = sys.objects.object_id
WHERE
definition LIKE '%(INDEX=%';
but as the definition column is sometimes too long to read I would like to select from that column just, let's say, 30 characters before and 30 characters after the WITH (INDEX=.
How to select just that part of the string?
Basically in that column I just would like to see:
...INNER JOIN tblSession WITH (INDEX=indRealDateTime) ON ...
which is the part of the query I'm interested in. I want to see at a glance how painful is that index hint.
Demo Example,
declare #i varchar(500)='afgdfgdfgdfg dgfdgdfg dfgdfgdfg dfgdfgdfg cvxfsdfsdfdf erwererwer (index fgfdgdf weqweqweqwe dsadsads sfsfsdfd erewwerwer 6786787 35345dfsdfgsdfsdf sdfdsfsdfdf'
DECLARE #margin INT = 15 --try 15000
SELECT SUBSTRING(#i, charindex('index', #i) - #margin, charindex('index', #i) + #margin)
I have two tables that share a common key and a few different columns. The common key is CampaignID. One of the tables has slightly more CampaignID's than the other one and I would like to find out the difference between those two tables. Currently, I am using LEFT OUTER JOIN and CTE to merge these two tables first, then by inspecting NULL columns in the CTE result set derived in the earlier step to count the difference in CampaignID column. For instance,
WITH CTE_Results
AS (SELECT t1.CampaignID AS cd_CampaignID,
t2.CampaignID AS cod_CampaignID,
t1.NAME,
t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID)
-- Now that I have CTE result, I'll use another SELECT to find the difference
SELECT cd_CampaignID, cod_CampaignID
FROM CTE_Results
WHERE cod_CampaignID is NULL
But this seems inefficient to me. Is there a more efficient/faster way to compare the difference in a specific column between two tables in Microsoft SQL Server? Thank you for your answers!
NOTE: I'm new to Microsoft SQL Server and SQL in general.
If one table has more than the other then how bout:
SELECT C1.* FROM CAMPAIGN_WITH_MORE_DATA AS C1
WHERE NOT EXISTS(SELECT * FROM CAMPAIGN_WITH_LESS_DATA AS C2
WHERE C2.CAMPAIGN_ID = C1.CAMPAIGN_ID)
If either may have one campaign or not then UNION this:
SELECT C1.Name AS [Col1],
C1.CAMPAIGN_ID,
'More Campaigns' AS [Source]
FROM CAMPAIGN_WITH_MORE_DATA AS C1
WHERE NOT EXISTS(SELECT * FROM CAMPAIGN_WITH_LESS_DATA AS C2
WHERE C2.CAMPAIGN_ID = C1.CAMPAIGN_ID)
UNION ALL
SELECT C2.Vendor AS [Col1],
C2.CAMPAIGN_ID,
'Less Campaigns' AS [Source]
FROM CAMPAIGN_WITH_LESS_DATA AS C2
WHERE NOT EXISTS(SELECT * FROM CAMPAIGN_WITH_MORE_DATA AS C1
WHERE C1.CAMPAIGN_ID = C2.CAMPAIGN_ID)
Not exists is likely more efficient than a left join. This'll work too:
SELECT CampaignID FROM CampaignDetails
EXCEPT
SELECT CampaignID FROM CampaignOnlineDetails
Just reverse order to check the other way round.
Can anyone tell me how to query a fulltext table in Sql Server, and get only exactly matches? Example:
I have those records in a table named "Items":
Bath
Bathroom
Test
Testing
I need to query for Bath and get only 1 record, "bath", excluding the word Bathroom. The same to the word "Test", wich in my context is different to "Testing".
Have you tried
SELECT column1, column2, ...
FROM [itens] AS FT_TBL INNER JOIN
CONTAINSTABLE([itens], *, 'Bath') AS KEY_TBL
ON FT_TBL.unique_key_column = KEY_TBL.[KEY]
http://msdn.microsoft.com/en-us/library/ms189760.aspx
or
SELECT columnname
FROM [itens]
WHERE CONTAINS(somecolumn, 'Bath')
http://msdn.microsoft.com/en-us/library/ms187787.aspx
Just noticed the updated questions (formatting applied).
If you column holds keywords (single word) then you could just select column = 'keyword'.
SELECT columnname
FROM [itens]
WHERE somecolumn = 'Bath'
are you searching for bath and test in one query or in different?
if in single, then one of the solutions i see is:
select top 1 from table where column like '[., ]bath[., ]%'
if in different, then union a series of such select's
How can I update the value of an xml tag with the value of an xml tag from another related table?
something like this:
UPDATE v2
SET
[xml].modify ('replace value of (//TAG1/text())[1]
with "CAST(v1.[xml].query(''//TAG2'') AS NVARCHAR(MAX))"')
FROM
table2 v2,
table1 v1
WHERE
v2.id = v1.id
I'm pretty late to this question, but if you're looking to "mass update" an XML column in the future, and you are in SQL 2005+, you can use a CTE to accomplish this:
WITH NewXmlData AS
(
SELECT v2.Id AS id
, CAST(v1.[xml].query('//TAG2') AS NVARCHAR(MAX)) AS NewValue
FROM table2 AS v2
INNER JOIN table1 AS v1 ON v2.id = v1.id
)
UPDATE v2
SET [xml].modify ('replace value of (//TAG1/text())[1]
with sql:column("NewXmlData.NewValue")')
FROM table2 AS v2
INNER JOIN NewXmlData AS nxd ON v2.id = nxd.id
I don't think you can do this in a single step - but you can do it in two steps, if you're on SQL Server 2008:
DECLARE #NewValue NVARCHAR(50)
SELECT #NewValue = [xml].value('(//TAG2)[1]', 'NVARCHAR(50)')
FROM dbo.v1
WHERE id = 1
UPDATE dbo.v2
SET [xml].modify('replace value of (//TAG1/text())[1] with sql:variable("#NewValue")')
WHERE id = 1
The ability to specify a sql:variable in your replace value of XQuery is a new feature in SQL Server 2008 - so if you're stuck on 2005, this won't work, unfortunately.
Feature with sql:variable also works on 2005 Server. You just need to put all the construction into braces:
modify('replace value of (//TAG1/text())[1] with {sql:variable("#NewValue")}')
I want to do this in code, not with ALT+F1.
You can also do it this way:
select columnproperty(object_id('mytable'),'mycolumn','IsIdentity')
Returns 1 if it's an identity, 0 if not.
sp_help tablename
In the output look for something like this:
Identity Seed Increment Not For Replication
----------- ------- ------------ ----------------------
userid 15500 1 0
Adjust the WHERE clause to suit:
select
a.name as TableName,
b.name as IdentityColumn
from
sysobjects a inner join syscolumns b on a.id = b.id
where
columnproperty(a.id, b.name, 'isIdentity') = 1
and objectproperty(a.id, 'isTable') = 1
As expansion on #Blogbeard's answer
If you like pure query and not inbuilt functions
select col_name(sys.all_objects.object_id, column_id) as id from sys.identity_columns
join sys.all_objects on sys.identity_columns.object_id = sys.all_objects.object_id
where sys.all_objects.name = 'system_files'
Identity is the value that is used for the very first row loaded into the table.
There is a microsoft article which can provide good knowledge about Identity:
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017
Now, there are couple of ways for identifying which column is an identity column in a table:
We can use sql query: select
columnproperty(object_id('mytable'),'mycolumn','IsIdentity')
sp_help tablename