Solved

DotNetNuke Profile Property Export SQL problems

Posted on 2009-04-01
17
788 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

0
Comment
Question by:DDH1
  • 9
  • 6
17 Comments
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
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

0
 

Author Comment

by:DDH1
Comment Utility
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.
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
try replacing nvarchar(3) to varchar(3)
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
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  
 
0
 

Author Comment

by:DDH1
Comment Utility
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.
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
What's the datatype for DOB? (is it nvarchar?) Also can you post some sample DOB values?
0
 

Author Comment

by:DDH1
Comment Utility
The column data type is nvarchar. Samples are
1979/04/31
1952/12/13
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 41

Expert Comment

by:ralmada
Comment Utility
try something like this:

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

0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
sorry
select a.*, cast(datediff(yy, cast(a.DOB as datetime), getdate()) as varchar(4)) as Age
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
or

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

Author Comment

by:DDH1
Comment Utility
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
0
 

Author Comment

by:DDH1
Comment Utility
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'
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
Comment Utility
This will show the age of of the user and each family member without the DOB, for those users with role ID = 2

select a.UserID, 

a.FirstName, 

a.LastName,

cast(datediff(yy, cast(a.DOB as datetime), getdate()) as nvarchar(4)) as Age,

a.Gender, 

a.FM1, 

cast(datediff(yy, cast(a.FM1DOB as datetime), getdate()) as nvarchar(4)) as FM1Age, 

a.FM1Gender, 

a.FM2, 

cast(datediff(yy, cast(a.FM2DOB as datetime), getdate()) as nvarchar(4)) as FM2Age, 

a.FM2Gender, 

a.FM3, 

cast(datediff(yy, cast(a.FM2DOB as datetime), getdate()) as nvarchar(4)) as FM3Age, 

a.FM3Gender, 

a.FM4, 

cast(datediff(yy, cast(a.FM2DOB as datetime), getdate()) as nvarchar(4)) as FM4Age, 

a.FM4Gender, 

a.FM5, 

cast(datediff(yy, cast(a.FM5DOB as datetime), getdate()) as nvarchar(4)) as FM5Age, 

a.FM5Gender, 

a.FM6, 

cast(datediff(yy, cast(a.FM6DOB as datetime), getdate()) as nvarchar(4)) as FM6Age, 

a.FM6Gender 

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

inner join UserRoles b on a.UserID = b.UserID

where b.UserRoleID = 2

Open in new window

0
 

Author Closing Comment

by:DDH1
Comment Utility
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.
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
you are welcome! Glad to help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now