|
Anyone know why when i run this query the Yr Column in coming back 203111 instead of 2011
(comments are locked)
|
|
Multiplying a year by 100 to then add the month number on it is not the way to work with date. SQL Server can be a bit awkward when things get complex but in general the date functions let you achieve what you want with pretty simple code. @ThomasRushton has given you some good examples of getting the value you want. For me, if you want a year and month combination that is YYYYMM then you wont be doing anything with it that involves calculations (If you were - such as adding some months onto a YYYYMM combination then let us know as this is not the best way to go.) then I would change the Year and Month value datatypes to CHAR and then the + becomes a concatenating function rather than a summing function. So... @Fatherjack - couldn't it be better expressed just using the CONVERT command and the date style option? The date style 112 is the ISO standard YYYYMMDD, converting the date to a varchar(6) will return the first 6 characters of that date style, job done.
Aug 10 '11 at 11:09 PM
WilliamD
@WilliamD - yeah probably. It was late!
Aug 11 '11 at 12:10 AM
Fatherjack ♦♦
(comments are locked)
|
|
If we rewrite the query: This returns: What you're doing is multiplying the year by 100 and then adding it to itself. Or, indeed, multiplying it by 101. Whichever. This is almost certainly not what you were aiming to achieve. If you just want the year, then use either
(comments are locked)
|
|
When I run your query and substitute (finishdate) with getdate() I get correct results. Since you are pulling from a table T1 for a distinct value from column finishdate, have you validated that you do not in fact have an erroneous year value of 2031 in your data set? No, I don;t have erroneous date. It works when I change the YY to M I would get a result 201101 for Jan 2011. But when I change it to YY it returns 203111
Aug 10 '11 at 11:14 AM
sqlLearner 1
Ah, I spaced on that. My apologies. I was looking at the fdate portion of your query. So your year you are taking the year of finishdate multiplying by 100 and adding it to the year of finishdate. So 201100 plus 2011 = 203111. What exactly is the outcome you are wanting? A value of 2011, 201100, 201111, or 2011002011?
Aug 10 '11 at 11:22 AM
Tim
(comments are locked)
|

