We help IT Professionals succeed at work.

DotNetNuke Profile Property Export SQL problems

850 Views
Last Modified: 2012-05-06
I am attempting to export certain profile property fields in dotnetnuke and could use some insight. I have asked this question before on another post, but I included several sub questions to it and didn't get much of a response. Never the less what I want to do is create several calculated columns dependant on the date of birth entries I have in the DNN profile table. I will attach all code for review. Right now I use a PIVOT to get the information I need directly from the userprofile table but there are shortcomings to this approach that I need to work out. I can display the date of birth for the primary member and up to six family members in my statement however there is no way for me to add additional columns for an age calculation (getdate()). Problem is caused by the way DNN stores it's profile properties into a nvarchar column for all datatypes including the dates I am using. So, I though if I could move that data via trigger attached to the userprofile table to another table (whilst still Pivoting the data rows) I could assign those columns the correct 'date' data types and then cast to get the date. I am not a hard core SQL guy so if this sounds idiot...it probably is. Anyway, any help in this department would be very greatly appreciated. The script below does NOT have the age columns and if I create a new table for a trigger to update from the userprofile table, I would have those columns calculate the persons age from the date of birth against todays tade.
Tables I am working with
--begin table-- 
USE [XXX]
GO
/****** Object:  Table [dbo].[UserProfile]    Script Date: 04/01/2009 10:12:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserProfile](
	[ProfileID] [int] IDENTITY(1,1) NOT NULL,
	[UserID] [int] NOT NULL,
	[PropertyDefinitionID] [int] NOT NULL,
	[PropertyValue] [nvarchar](3750) NULL,
	[PropertyText] [ntext] NULL,
	[Visibility] [int] NOT NULL CONSTRAINT [DF__UserP__Visib__1352D76D]  DEFAULT ((0)),
	[LastUpdatedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_UserProfile] PRIMARY KEY NONCLUSTERED 
(
	[ProfileID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
 
GO
ALTER TABLE [dbo].[UserProfile]  WITH NOCHECK ADD  CONSTRAINT [FK_UserProfile_ProfilePropertyDefinition] FOREIGN KEY([PropertyDefinitionID])
REFERENCES [dbo].[ProfilePropertyDefinition] ([PropertyDefinitionID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[UserProfile] CHECK CONSTRAINT [FK_UserProfile_ProfilePropertyDefinition]
GO
ALTER TABLE [dbo].[UserProfile]  WITH NOCHECK ADD  CONSTRAINT [FK_UserProfile_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([UserID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[UserProfile] CHECK CONSTRAINT [FK_UserProfile_Users]
--end table--
 
--begin table--
USE [XXX]
GO
/****** Object:  Table [dbo].[ProfilePropertyDefinition]    Script Date: 04/01/2009 10:13:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProfilePropertyDefinition](
	[PropertyDefinitionID] [int] IDENTITY(1,1) NOT NULL,
	[PortalID] [int] NULL,
	[ModuleDefID] [int] NULL,
	[Deleted] [bit] NOT NULL,
	[DataType] [int] NOT NULL,
	[DefaultValue] [ntext] NULL,
	[PropertyCategory] [nvarchar](50) NOT NULL,
	[PropertyName] [nvarchar](50) NOT NULL,
	[Length] [int] NOT NULL CONSTRAINT [DF_ProfilePropertyDefinition_Length]  DEFAULT ((0)),
	[Required] [bit] NOT NULL,
	[ValidationExpression] [nvarchar](2000) NULL,
	[ViewOrder] [int] NOT NULL,
	[Visible] [bit] NOT NULL,
 CONSTRAINT [PK_ProfilePropertyDefinition] PRIMARY KEY CLUSTERED 
(
	[PropertyDefinitionID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
 
GO
ALTER TABLE [dbo].[ProfilePropertyDefinition]  WITH CHECK ADD  CONSTRAINT [FK_ProfilePropertyDefinition_Portals] FOREIGN KEY([PortalID])
REFERENCES [dbo].[Portals] ([PortalID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[ProfilePropertyDefinition] CHECK CONSTRAINT [FK_ProfilePropertyDefinition_Portals]
--end table--
 
--begin export script--
Select	UserID, 
[2] as FirstName, 
[4] as LastName,
[39] as DOB,
[40] as Gender, 
[43] as FM1, 
[44] as FM1DOB, 
[45] as FM1Gender, 
[47] as FM2, 
[48] as FM2DOB, 
[49] as FM2Gender, 
[50] as FM3, 
[51] as FM3DOB, 
[52] as FM3Gender, 
[53] as FM4, 
[54] as FM4DOB, 
[55] as FM4Gender, 
[56] as FM5, 
[57] as FM5DOB, 
[58] as FM5Gender, 
[59] as FM6, 
[60] as FM6DOB, 
[61] as FM6Gender 
FROM 
(Select UserID, PropertyDefinitionID, PropertyValue from UserProfile) o     
PIVOT      (     MAX(PropertyValue)     FOR [PropertyDefinitionID] IN ([2], [4], [39], [40], [43], [44], [45], [47], [48], [49], [50], [51], [52], [53], [54], [55], [56], [57], [58], [59], [60], [61])) p
--end export script--

Open in new window

Comment
Watch Question

Commented:
What if you do something like this? Note that I've converted Age to nvarchar, but I don't think it's required.
select a.*, cast(datediff(y, convert(datetime, a.DOB, 101), getdate()) as nvarchar(3)) as Age  
from (
Select	UserID, 
[2] as FirstName, 
[4] as LastName,
[39] as DOB,
[40] as Gender, 
[43] as FM1, 
[44] as FM1DOB, 
[45] as FM1Gender, 
[47] as FM2, 
[48] as FM2DOB, 
[49] as FM2Gender, 
[50] as FM3, 
[51] as FM3DOB, 
[52] as FM3Gender, 
[53] as FM4, 
[54] as FM4DOB, 
[55] as FM4Gender, 
[56] as FM5, 
[57] as FM5DOB, 
[58] as FM5Gender, 
[59] as FM6, 
[60] as FM6DOB, 
[61] as FM6Gender 
FROM 
(Select UserID, PropertyDefinitionID, PropertyValue from UserProfile) o     
PIVOT      (     MAX(PropertyValue)     FOR [PropertyDefinitionID] IN ([2], [4], [39], [40], [43], [44], [45], [47], [48], [49], [50], [51], [52], [53], [54], [55], [56], [57], [58], [59], [60], [61])) p
) a

Open in new window

Author

Commented:
It's close! The query ran then immediatly kicked back this error
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type nvarchar.

Commented:
try replacing nvarchar(3) to varchar(3)

Commented:
Sorry, first line should be like this (missed one "y" also)
select a.*, cast(datediff(yy, convert(datetime, a.DOB, 101), getdate()) as varchar(4)) as Age  
 

Author

Commented:
Hi Ralmada,
I really appreciate this as I know you actually read my question and understand it.
You are so close I can taste it. The last query results in this error, but it tries to post the results.
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.

Commented:
What's the datatype for DOB? (is it nvarchar?) Also can you post some sample DOB values?

Author

Commented:
The column data type is nvarchar. Samples are
1979/04/31
1952/12/13

Commented:
try something like this:

select a.*, cast(datediff(yy, case(a.DOB as datetime), getdate()) as varchar(4)) as Age  

Commented:
sorry
select a.*, cast(datediff(yy, cast(a.DOB as datetime), getdate()) as varchar(4)) as Age

Commented:
or

select a.*, cast(datediff(yy, convert(datetime, replace(a.DOB, '/', ''), 112), getdate()) as varchar(4)) as Age  

Author

Commented:
Hi Ralmada,
This one works!! but it still needs just a bit of adjustment. If I return the age for each family member I can drop the DOB fields from display in the pivot. I just changed your select statement to nvarchar.
select a.*, cast(datediff(yy, cast(a.DOB as datetime), getdate()) as nvarchar(4)) as Age
from (
Select      UserID,
[2] as FirstName,
[4] as LastName,
[39] as DOB,
[40] as Gender,
[43] as FM1,
[44] as FM1DOB,
[45] as FM1Gender,
[47] as FM2,
[48] as FM2DOB,
[49] as FM2Gender,
[50] as FM3,
[51] as FM3DOB,
[52] as FM3Gender,
[53] as FM4,
[54] as FM4DOB,
[55] as FM4Gender,
[56] as FM5,
[57] as FM5DOB,
[58] as FM5Gender,
[59] as FM6,
[60] as FM6DOB,
[61] as FM6Gender
FROM
(Select UserID, PropertyDefinitionID, PropertyValue from UserProfile) o    
PIVOT      (     MAX(PropertyValue)     FOR [PropertyDefinitionID] IN ([2], [4], [39], [40], [43], [44], [45], [47], [48], [49], [50], [51], [52], [53], [54], [55], [56], [57], [58], [59], [60], [61])) p
) a

Author

Commented:
also, can I use a cross join on userId to the userroles table to limit the results returned to a specific userrole? For example here is the userroles table
--begin table--
USE [XXX]
GO
/****** Object:  Table [dbo].[UserRoles]    Script Date: 04/03/2009 08:57:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserRoles](
      [UserRoleID] [int] IDENTITY(1,1) NOT NULL,
      [UserID] [int] NOT NULL,
      [RoleID] [int] NOT NULL,
      [ExpiryDate] [datetime] NULL,
      [IsTrialUsed] [bit] NULL,
      [EffectiveDate] [datetime] NULL,
 CONSTRAINT [PK_UserRoles] PRIMARY KEY CLUSTERED
(
      [UserRoleID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[UserRoles]  WITH NOCHECK ADD  CONSTRAINT [FK_UserRoles_Roles] FOREIGN KEY([RoleID])
REFERENCES [dbo].[Roles] ([RoleID])
ON DELETE CASCADE
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[UserRoles] CHECK CONSTRAINT [FK_UserRoles_Roles]
GO
ALTER TABLE [dbo].[UserRoles]  WITH NOCHECK ADD  CONSTRAINT [FK_UserRoles_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([UserID])
ON DELETE CASCADE
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[UserRoles] CHECK CONSTRAINT [FK_UserRoles_Users]
--end table--

I only want to return users belonging to a user role id of '2'
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Ralmada you are a GENIUS!!! This woked perfectly and I am extremely grateful. I just hope every time I need SQL help on here, you are around as I know I found a REAL expert. THank you so much.

Commented:
you are welcome! Glad to help!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.