January 12, 2010 at 8:57 am
How do i update a table with prefix number for the existing data ?
example:
ColumnA has "51", I want to update it with "2009.51"
January 12, 2010 at 9:10 am
UPDATE TableA
SET ColumnA = '2009.' + ColumnA
WHERE ColumnA = '51'
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2010 at 9:39 am
this is not working it is just adding to the existing number say if i have 1 the result is 2010 adding 1 to 2009.
Expected result :
1 = 2009.1
2 = 2009.2
.
.
.
51=2009.52
January 12, 2010 at 10:22 am
I actually doubt you've tried the solution Wayne proposed....
He suggested to add a string to a string.
What you describe is to add two numbers.
But when you try to use Waynes statement against an integer column you'd get an error message
Msg 245, Level 16, State 1, Line 9
Conversion failed when converting the varchar value '2009.' to data type int..
But not 2010 like you described...
Would you mind explaining a little more what you're trying to do?
To me it seems like you have a week number and want to add the year as a prefix, separated with a '.'. In this case, shouldn't 2009.1 better be 2009.01?
I have no idea what you want to do with those data but if I'm not completely off with my calendar assumption, then your approach would cause some trouble since '2009.2' would be larger than '2009.19'...
Please provide more details.
January 12, 2010 at 11:28 am
I gave my expected result above.
I have a column which has week number like 1 to 52 but i want to update them with prefix year like 2009.1,2009.2......2009.52
January 12, 2010 at 11:45 am
Tara-1044200 (1/12/2010)
I gave my expected result above.I have a column which has week number like 1 to 52 but i want to update them with prefix year like 2009.1,2009.2......2009.52
What is the data type of the column you attempting to modify? Yes, you gave the expected results, but you didn't provide the table definition.
January 12, 2010 at 11:52 am
data type is real
January 12, 2010 at 11:55 am
Tara-1044200 (1/12/2010)
I gave my expected result above.I have a column which has week number like 1 to 52 but i want to update them with prefix year like 2009.1,2009.2......2009.52
I agree with Lutz's suggestion. Though the week numbers are 1...52 it would be better served to do .01 than .1.
.1
.2
.3
.4
.5
.6
.7
.8
.9
.10 = same as .1
There could arise some data inconsistencies at worst and at best there would be data confusion.
It also appears that the column data type is int. You may want to change it to decimal or varchar.
Knowing the table definition will greatly assist in providing a correct answer.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 12, 2010 at 12:10 pm
update dbo.TheTable set
TheColumn = 2009 + (TheColumn / 100.00)
How about this?
January 12, 2010 at 12:10 pm
Here's what I came up with to cover the options I think we're faced with...
If the base column is of data tpe INT I actually doubt that it would be possible at all to get a result like 2009.1 ... (so I used NUMERIC(6,2) instead...) 😉
DECLARE @t TABLE (col1 NUMERIC(6,2),col2 varchar(7),col3 varchar(7))
INSERT INTO @t (col1,col2)
SELECT 1,'1' UNION ALL
SELECT 2,'2' UNION ALL
SELECT 3,'3' UNION ALL
SELECT 51,'51' UNION ALL
SELECT 52,'52'
SELECT * FROM @t
UPDATE @t
SET
col1 = 2009 + col1 / 100 ,
col2 = '2009.'+col2,
col3 = '2009.'+ cast(cast(col1 AS int) AS varchar(2))
SELECT * FROM @t
January 12, 2010 at 12:50 pm
lmu92 (1/12/2010)
I actually doubt you've tried the solution Wayne proposed....He suggested to add a string to a string.
How do i update a table with prefix number for the existing data ?
example:
ColumnA has "51", I want to update it with "2009.51"
The data was enclosed in quotation marks, so it was assumed that this was a string.
THIS IS WHY it is so important to provide CREATE TABLE statements, and INSERT statements with test data to show what YOUR environment is really like. Read the first link in my signature for how to do that.
Since you have a real data type, I concur with Lynn's suggestion.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2010 at 1:01 pm
WayneS (1/12/2010)
..The data was enclosed in quotation marks, so it was assumed that this was a string.
...
I agree with you and my comment didn't question your solution at all...
Since we're "left in the dark" regarding data type I decided to use the "curtain fire sample method"...
January 12, 2010 at 1:12 pm
great, Lynn's query
update dbo.TheTable set
TheColumn = 2009 + (TheColumn / 100.00)
worked for me.
For the same real data type how would i update 2008.1,2008.2......
with 2009.1,2009.2.........
January 12, 2010 at 1:17 pm
Tara-1044200 (1/12/2010)
great, Lynn's queryupdate dbo.TheTable set
TheColumn = 2009 + (TheColumn / 100.00)
worked for me.
For the same real data type how would i update 2008.1,2008.2......
with 2009.1,2009.2.........
Would you help me to understand it?
Your requirement was to get 2009.1 when passing 1 to the query.
You're stating that you get your desired result.
But [SELECT 2009 + 1/100.0] will result in 2009.01.
What do I miss here??
January 12, 2010 at 1:44 pm
lmu92 (1/12/2010)
WayneS (1/12/2010)
..The data was enclosed in quotation marks, so it was assumed that this was a string.
...
I agree with you and my comment didn't question your solution at all...
Since we're "left in the dark" regarding data type I decided to use the "curtain fire sample method"...
Lutz,
Sorry, I forgot to remove the quote from you... I was directing this to the OP.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 15 posts - 1 through 15 (of 24 total)
You must be logged in to reply to this topic. Login to reply