Using SQL Server 2005
Table1
ID
Abc0012
BED0045
cAB0027
....
I want to replace all the ID values in the table1 like ABC0012, BED0045, CAB0027.
I want to make all characters as caps
Need Query Help
UPDATE Table1
SET ID = UPPER(ID)
Use the UPPER function
update table1 set id = upper(id)
Use upper:
SELECT upper(ID) FROM YourTable
or:
UPDATE YourTable SET ID=upper(ID)
If you want to change them:
UPDATE
Table1
SET
ID = UPPER(ID)
Could work, this is untested though.
I believe you should be able to do something like this:
UPDATE Table1 SET ID = UPPER(ID)
Here's a complete script that shows how to use the UPPER() function to achieve this:
declare #mytable table (
somevalue varchar (20)
)
insert into #mytable(
somevalue
)
values (
'abc123'
)
insert into #mytable(
somevalue
)
values (
'xYz456'
)
insert into #mytable(
somevalue
)
values (
'gjriwe345'
)
update #mytable
set somevalue = upper(somevalue)
select *
from #mytable
Related
In a simplified scenario I have table T that looks somthing like:
Key Value
1 NULL
1 NULL
1 NULL
2 NULL
2 NULL
3 NULL
3 NULL
I also have a very time-consuming function Foo(Key) which must be considered as a black box (I must use it, I can't change it).
I want to update table T but in a more efficient way than
UPDATE T SET Value = dbo.Foo(Key)
Basically I would execute Foo only one time for each Key.
I tried something like
WITH Tmp1 AS
(
SELECT DISTINCT Key FROM T
)
, Tmp2 AS
(
SELECT Key, Foo(Key) Value FROM Tmp1
)
UPDATE T
SET T.Value = Tmp2.Value
FROM T JOIN Tmp2 ON T.Key = Tmp2.Key
but unexpectedly computing time doesn't change at all, because Sql Server seems to run Foo again on every row.
Any idea to solve this without other temporary tables?
One method is to use a temporary table. You don't have much control over how SQL Server decides to optimize its queries.
If you don't want a temporary table, you could do two updates:
with toupdate as (
select t.*, row_number() over (partition by id order by id) as seqnum
from t
)
update toupdate
set value = db.foo(key)
where seqnum = 1;
Then you can run a similar update again:
with toupdate as (
select t.*, max(value) over (partition by id) as as keyvalue
from t
)
update toupdate
set value = keyvalue
where value is null;
You might try it like this:
CREATE FUNCTION dbo.Foo(#TheKey INT)
RETURNS INT
AS
BEGIN
RETURN (SELECT #TheKey*2);
END
GO
CREATE TABLE #tbl(MyKey INT,MyValue INT);
INSERT INTO #tbl(MyKey) VALUES(1),(1),(1),(2),(2),(3),(3),(3);
SELECT * FROM #tbl;
DECLARE #tbl2 TABLE(MyKey INT,TheFooValue INT);
WITH DistinctKeys AS
(
SELECT DISTINCT MyKey FROM #tbl
)
INSERT INTO #tbl2
SELECT MyKey,dbo.Foo(MyKey) TheFooValue
FROM DistinctKeys;
UPDATE #tbl SET MyValue=TheFooValue
FROM #tbl
INNER JOIN #tbl2 AS tbl2 ON #tbl.MyKey=tbl2.MyKey;
SELECT * FROM #tbl2;
SELECT * FROM #tbl;
GO
DROP TABLE #tbl;
DROP FUNCTION dbo.Foo;
I have a temp table declared
declare #tmptable(
value nvarchar(500) not null
);
I use a function to insert values into that temp table.
I am trying to figure out how to update a table using the values of #tmptable
insert into t1 (
active
,SchoolId
,inserted
)
select
1
,temp.value
,#insertedDate
select temp.value from #tmptable;
When i try to insert in table t1 it doesn't work. I guess there are two Select statements is causing the problem. Please let me know how to fix it. Thanks
Try this one -
INSERT INTO dbo.t1
(
Active
, SchoolId
, Inserted
)
SELECT
1
, t.value
, #insertedDate
FROM #tmptable t;
INSERT INTO t1
(
ACTIVE
,SchoolId
,INSERTED
)
SELECT 1
,temp.value
,#insertedDate
FROM #tmptable temp;
insert into t1 (
active
,SchoolId
,inserted
)
select
1
,temp.value
,#insertedDate
from #tmptable;
this will work...
I am doing something like this in SP,
insert into #FilteredTbl select * from #MasterTbl
but now problem is that i want data of #MasterTbl sorted before adding into #FilteredTbl,
How can I do that ?
Maybe something like this:
INSERT INTO #FilteredTbl
SELECT * FROM #MasterTbl AS tbl
ORDER BY tbl.OrderbyColumn
Try following method to insert sort result into temp table. Use ROW_NUMBER to ORDER BY table result
DECLARE #FilteredTbl AS TABLE
(
RowID INT
[Column_1]
[Column_2]
.
.
)
insert into #FilteredTbl
select ROW_NUMBER()OVER(ORDER BY [ShortColumn]),[Field_1],[Field_2]... from #MasterTbl
You can use order by class in select statement
insert into #FilteredTbl
select * from #MasterTbl
order by <column name>
INSERT INTO #FilteredTbl SELECT * FROM #MasterTbl AS mtb ORDER BY mtb.OrderbyColumn [desc]
Here desc is optional if you want to order by your column in descending order than add ir otherwise no need to add
Please sort the table as you want before inserting into another table.
Like:
Select * From #MasterTbl m Order By m.ColumnName
then insert into your new table
Insert Into #FilterTbl Select * From #MasterTbl m Order By m.ColumnName [DESC]
You can also filter your #MasterTbl using Where Clause
You should add identity your table then It will sorted like that
CREATE TABLE #test (
s int IDENTITY (1, 1),
ID int,
Name varchar(50),
part int
);
INSERT INTO #test (ID,Name,Part)
SELECT ID, Name, Part FROM Table_1 ORDER BY ID DESC
SELECT * FROM #test
I have a table with a collection column. I want to do a subselect which returns several integers and put the result in that collection column, however I can't find a syntax to do it through SQL. I did it by writing an SQL procedure which does the same thing (put results of SELECT in SET variable and return variable), however I'm trying to do the same without functions. Can it be done?
First, I create a temporary table:
CREATE TEMP TABLE table1 (
id INTEGER
, col2 SET(INT NOT NULL)
)
Then I fill it with test data:
INSERT INTO table1 (id) VALUES (1);
INSERT INTO table1 (id) VALUES (2);
And now this works:
UPDATE table1 SET col2 = SET{1,2};
...but I'm trying to do this and it doesn't work:
UPDATE table1 SET col2 = (SELECT id FROM table1) WHERE id = 1;
It returns this error:
[Error Code: -9632, SQL State: IX000] Value does not match the type of column (col2).
Manipulating SET types in pure SQL is a pain.
Your UPDATE is trying to assign an INTEGER to a SET OF INTEGER, and the error says "you can't do that".
You should be able to do:
UPDATE table1
SET col2 = SET { (SELECT id FROM table1 WHERE id = 1) }
WHERE id = 1;
However, I'm not sure what the correct modification is to get more than one value into the set; the inner WHERE is not there idly.
You can achieve this by using MULTISET and ITEM keyword
the following example will work:
CREATE TEMP TABLE table1 (
id INTEGER
,col2 MULTISET(INT NOT NULL)
);
INSERT INTO table1 (id) VALUES (1);
INSERT INTO table1 (id) VALUES (2);
UPDATE table1 SET col2 = MULTISET{1,2};
UPDATE table1
SET col2 = MULTISET(SELECT ITEM id FROM table1)
WHERE id = 1;
Be aware of the differences between SET and MULTISET
select set{1,2,1,3,1} from systables where tabid=1;
returns SET{1,2,3}
select multiset{1,2,1,3,1} from systables where tabid=1;
returns MULTISET{1,2,1,3,1}
SELECT *
FROM dbo.tbh_table
WHERE TopicID IN (
SELECT value
FROM dbo.fn_split('19,',')
)
I have to change above query to execute result like below
SELECT *
FROM dbo.tbh_table
WHERE TopicID LIKE '%19,%'
My topicID values are like this 15,19,12,1
But split will give values are 15 19 12 1. because of which i am not able to execute the query.
any guidance will help
Assuming that fn_split is a table valued function (TVF), returning a TABLE (value INT), use this:
SELECT t.*
FROM dbo.tbh_table t
CROSS APPLY
dbo.fn_split(TopicID) split
WHERE split.value = 19
You could add your IDs to a table variable and then join with the table variable to determine if TopicID was in the table variable.
DECLARE #DesiredIDs TABLE (
DesiredID INT
)
INSERT INTO #DesiredIDs (DesiredID) Values (15)
INSERT INTO #DesiredIDs (DesiredID) Values (19)
INSERT INTO #DesiredIDs (DesiredID) Values (12)
INSERT INTO #DesiredIDs (DesiredID) Values (1)
select * from tbh_table t
left join #DesiredIDs d on d.DesiredID = t.TopicID
where d.DesiredID is not null
If I understand your question correctly, you want something like this:
SELECT *
FROM dbo.tbh_table
WHERE 19 IN (
SELECT value
FROM dbo.fn_split(TopicId,',')
)