Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL 2005 - Pivot changing money value to datetime

Posted on 2009-03-31
9
Medium Priority
?
473 Views
Last Modified: 2012-05-06
Hello Experts,

Not sure if you can help with this limited code but I'll give it a try.

I'm creating some dynamic sql and using a pivot funciton with the code below.

The resulting table looks fine except when I select the MAX(DayRate) in my pivot, it is bringing back a datetime of 1900-08-06 00:00:00:)

Can anyone suggest why this would happen?

Many thanks
Don
SET DATEFORMAT DMY
	DECLARE @ArrivalDate datetime
	DECLARE @displaydate nvarchar(22)
	DECLARE @strSQL nvarchar(max)
	DECLARE @strSQL2 nvarchar(max)
	DECLARE @cols nvarchar(max), @cols2 nvarchar(max), @i int
	DECLARE @colname sysname
 
--	Declare a local temporary table to be pivoted with the dates
	DECLARE @tblDate table (AutoNumber int identity not null primary key, DisplayDate nvarchar(22))
 
--	Declare a global temporary table to grab the room details
	CREATE TABLE #Ratestbl (RoomID int, 
							RoomType nvarchar(50),
							RoomDescription nvarchar(max),
							MaxAdults int,
							MinStay int,
							RackRate money,
							)
	
	SET @ArrivalDate = CAST(@Arrival AS DateTime)
 
--	Insert the arrival date and the 27 days that follow into the table to be pivoted	
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+1, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+2, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+3, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+4, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+5, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+6, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+7, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+8, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+9, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+10, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+11, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+12, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+13, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+14, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+15, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+16, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+17, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+18, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+19, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+20, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+21, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+22, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+23, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+24, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+25, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+26, 103))
	insert into @tblDate values(convert(nvarchar(22), @arrivaldate+27, 103))
 
--	Construct the column list for the IN clause
--	e.g., [29/12/2008],[30/12/2008],[31/12/2008]
	SET @i = 1
	SET @cols = N''
	SET @cols2 = N''
	WHILE @i < 29
	BEGIN
	  SET @cols = @cols + N',['+ (select DisplayDate from @tblDate where AutoNumber = @i) + N']'
	  SET @cols2 = @cols2 + N','''+ (select DisplayDate from @tblDate where AutoNumber = @i) + N''''
	  set @colname = (select DisplayDate from @tblDate where AutoNumber = @i)
	  set @strSQL = 'alter table #Ratestbl add [' + cast(@colname as nvarchar(22)) + '] datetime'
	  exec (@strSQL)
	  SET @i = @i + 1
	END
	SET @cols = SUBSTRING(@cols, 2, LEN(@cols))
	SET @cols2 = SUBSTRING(@cols2, 2, LEN(@cols2))
 
--	Pivot query to build result set (in sql 2005 have to use dynamic sql to make column headings dynamic
	set @strSQL ='	select *
					from (select a.RoomID as RoomID,
								a.RoomType as RoomType,
								cast(a.RoomDescription as nvarchar(max)) as RoomDescription,
								a.RoomMaxAdults as MaxAdults,
								a.MinStay as MinStay,
								a.rate as RackRate,
								convert(nvarchar(22), b.RateDate, 103) as Ratedate,
								b.dayrate as DayRate
						 from dbo.Room a
							left outer join dbo.rates b
								on	a.roomid = b.roomid 
									and b.Inventory > 0
						where  a.RoomId = ' + cast(@RoomId as nvarchar(128)) + '
								and convert(nvarchar(22), b.RateDate, 103) in (' + @cols2 + ')
						 ) as a pivot (max(DayRate) for RateDate in ('+ @cols + ') ) as piv'
						 
	INSERT INTO #Ratestbl
	EXEC (@strSQL)

Open in new window

0
Comment
Question by:WorldBlueAustralia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24035109
Because DayRate = 218 Which translates to 1900-08-06.
0
 
LVL 21

Expert Comment

by:pinaldave
ID: 24035114

use

max(DayRate) AS Money
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24035214
I think you have mixed up a variable name, RateDate for DateRate.  You have
(max(DayRate) for RateDate
in your pivot and RateDate I bet is a Date field and Pivot is using that for the resuts data type.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24035218
I mean RateDate for DayRate
0
 

Author Comment

by:WorldBlueAustralia
ID: 24035228
Thanks Guys,

You've identified the problem, now I need the correct syntax to solve it.

max(DayRate) AS Money

Did not work.


RateDate is a Date
DayRate is a Rate (money)

Thanks
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24035427
How about:
max(DayRate) AS MaxDayRate
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 2000 total points
ID: 24035724
WorldBlueAustralia,
Sorry for the delay, your reply caught me on my commute home from work.  I set up a test to see what was going on and found that you have created the columns in your temp table as DATETIME instead of MONEY
        set @strSQL = 'alter table Ratestbl add [' + cast(@colname as nvarchar(22)) + '] datetime'
I changed datetime to MONEY on this line and it works like a charm
        set @strSQL = 'alter table Ratestbl add [' + cast(@colname as nvarchar(22)) + '] MONEY'
0
 

Author Comment

by:WorldBlueAustralia
ID: 24035841
I was hoping someone clever would see it.

I struggled for hours...

Many thanks Luttrell!
Don
0
 

Author Closing Comment

by:WorldBlueAustralia
ID: 31565127
Thanks Luttrell!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question