DotNetNuke Profile Property Export SQL problems

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

DDH1Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ralmadaConnect With a Mentor Commented:
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
 
ralmadaCommented:
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
 
DDH1Author 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.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ralmadaCommented:
try replacing nvarchar(3) to varchar(3)
0
 
ralmadaCommented:
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
 
DDH1Author 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.
0
 
ralmadaCommented:
What's the datatype for DOB? (is it nvarchar?) Also can you post some sample DOB values?
0
 
DDH1Author Commented:
The column data type is nvarchar. Samples are
1979/04/31
1952/12/13
0
 
ralmadaCommented:
try something like this:

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

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

select a.*, cast(datediff(yy, convert(datetime, replace(a.DOB, '/', ''), 112), getdate()) as varchar(4)) as Age  
0
 
DDH1Author 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
0
 
DDH1Author 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'
0
 
DDH1Author 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.
0
 
ralmadaCommented:
you are welcome! Glad to help!
0
All Courses

From novice to tech pro — start learning today.