Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2011-06-21 : 06:33:18
|
| HiI have two tables Fact SalesCalenderMonth INTData----200805200806200807Dimesion DimTimeTimeID INTData-----200805012008060120080701Now i want to add 01 in the CalenderMonth data to get the join to fill the TimeId in my fact TableSELECT dimTime.TimeID FROM FactSales LEFT JOIN dimTime ON FactSales.CalenderMonth = DimTime.TimeIDI want the FactSales.CalenderMonth + 01 so that i can join with TimeIdTHANKSSHANMUGARAJnshanmugaraj@gmail.com |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-21 : 06:47:00
|
You can add the 01 at the end using this:CAST(CalendarMonth AS VARCHAR(6))+'01' And then if you need that to be of datetime data time, cast it again:CAST(CAST(CalendarMonth AS VARCHAR(6))+'01' as datetime) |
 |
|
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2011-06-21 : 06:50:55
|
quote: Originally posted by sunitabeck You can add the 01 at the end using this:CAST(CalendarMonth AS VARCHAR(6))+'01' And then if you need that to be of datetime data time, cast it again:CAST(CAST(CalendarMonth AS VARCHAR(6))+'01' as datetime)
select CAST( CalendarMonth AS VARCHAR(6))+'01' FROM FactSales gives me this error :(Msg 8115, Level 16, State 5, Line 2Arithmetic overflow error converting numeric to data type varchar.THANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-06-21 : 07:11:16
|
What is the result of the following SELECT?select max(CalenderMonth) from FactSales No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-21 : 07:20:16
|
This probably means the data is not in the form of YYYYMM as you indicated. For example, try this:declare @x as bigintset @X = 200805print cast(@X as varchar(6)) It would work fine. But if you change 200805 to 20080501, you would get the error that you saw. So you need to investigate what kind of data you have. You can do select max(CalendarMonth) to see what the maximum numeric value is. select * from yourTable where isnumeric(CalendarMonth) = 0 will tell you if there are any non-numeric characters in the data.If it is that there are some rows with just year and month, and some with year, month and a date, that can be parsed, but first you need to know what kind of data irregularities you have. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2011-06-21 : 10:43:04
|
select CalenderMonthfrom FactSaleswhere -- Search for invalid YYYYMM in INT column 1 = case when CalenderMonth is null then 1 when convert(varchar(20),CalenderMonth) not like '[1-2][0-9][0-9][0-9][0-1][0-9]' then 1 when isdate(convert(varchar(20),CalenderMonth)+'01') <> 1 then 1 else 0 end CODO ERGO SUM |
 |
|
|
|
|
|
|
|