Solved

DotNetNuke Profile Property Export SQL problems

Posted on 2009-04-01
17
797 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
ID: 24055740
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
ID: 24055907
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
ID: 24060044
try replacing nvarchar(3) to varchar(3)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 41

Expert Comment

by:ralmada
ID: 24060080
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
ID: 24060189
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
ID: 24060415
What's the datatype for DOB? (is it nvarchar?) Also can you post some sample DOB values?
0
 

Author Comment

by:DDH1
ID: 24060527
The column data type is nvarchar. Samples are
1979/04/31
1952/12/13
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24061029
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
ID: 24061059
sorry
select a.*, cast(datediff(yy, cast(a.DOB as datetime), getdate()) as varchar(4)) as Age
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24061101
or

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

Author Comment

by:DDH1
ID: 24061310
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
ID: 24061387
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
ID: 24064941
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
ID: 31565423
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
ID: 24065193
you are welcome! Glad to help!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

809 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