February 8, 2011 at 7:02 pm
Hi T-Sql Gurus,
Here is my table "State"
ID,TYPE,STATE
1,B,CA
2,C,GA
3,D,NY
Here what i want (Same Table "State")
ID,TYPE,STATE,COLUMNXY
1,B,CA,B-CA
2,C,GA,C-GA
3,D,NY,D-NY
Please guide me how i can do this one through t-sql. Thanks in advance.
February 8, 2011 at 7:20 pm
I am assuming u are very baby SQL 🙂 developer (No pun intended) . This requirement is the basic concatenation of 2 columns.
DECLARE @Table TABLE
(
ID INT,
TYPE VARCHAR(1),
STATE VARCHAR(2)
)
INSERT INTO @Table (ID,TYPE,STATE)
SELECT 1,'B','CA' UNION ALL
SELECT 2,'C','GA' UNION ALL
SELECT 3,'D','NY'
SELECT ID,TYPE,STATE , (TYPE +'-'+ STATE) AS COLUMNXY
FROM @Table
Hope this helps 🙂
P.S: That "baby SQL Dev", please don't take it to heart ; Just for fun :w00t:
February 8, 2011 at 8:02 pm
Yes you are right that's y i am here!
I have 60 thousand records in my table, Should i use same solution?
February 8, 2011 at 9:50 pm
Yes you can 🙂
February 9, 2011 at 8:24 pm
First step to add column (COLUMNXY)
second use update statement
UPDATE STATE
SET COLUMNXY = (TYPE,'-',STATE)
February 9, 2011 at 9:05 pm
rocky_498 (2/9/2011)
First step to add column (COLUMNXY)second use update statement
UPDATE STATE
SET COLUMNXY = (TYPE,'-',STATE)
First step to add column (COLUMNXY)
update STATE
SET COLUMNXY = (TYPE + '-' + STATE)
Thanks
February 9, 2011 at 10:06 pm
Hi all,
I would not go down this path as it means you are redundantly storing data and breaching some basic normalisation rules. If the new field is always going to be a concatenation of three elements, field 1, the hyphen and field two I would create a view that users of the system then query to get your results.
Ie. create view viewname as
SELECT ID,TYPE,STATE , (TYPE +'-'+ STATE) AS COLUMNXY
FROM State
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply