Changing datetime to date data type? - sql

ALTER PROCEDURE [Deal].[USP_LoadDealHistoryByDealId]
(#DealId int,
#chk int)
AS
IF (#chk = 1)
BEGIN
SELECT
DDT.FieldType AS field,
DDT.OldValue as old,
DDT.NewValue as new,
AU.FullName AS Name,
DDT.UpdateOn AS UpdatedOn
FROM
[Deal].[DealTransaction] as DDT
INNER JOIN
AD.Users as AU ON AU.UserId = DDT.[UpdateBy]
WHERE
DDT.dealId = #DealId
END
ELSE if(#chk = 2)
BEGIN
SELECT
'No Data' as field ,
0 as old ,
0 as new ,
AU.FullName AS Name,
DD.UpdatedOn AS UpdatedOn
FROM
AD.Users as AU
INNER JOIN
[Deal].[Deals] AS DD ON AU.UserId = DD.UploadedBy
WHERE
DD.dealId = #DealId
END
UpdatedOn column is of datatype datetime and it shows to my HTML table like 10-09-1992/12.00.... But I want to show just date like 10-09-1992, what should I do?
Here Is my HTML table
public string LoadDealHistory(DealHistory aDealhistory)
{
StringBuilder html = new StringBuilder();
try {
using (DealTranctionGateway dealTranctionGateway = new DealTranctionGateway())
{
DataTable dt = new DataTable();
dt = dealTranctionGateway.LoadDealHistory(aDealhistory);
string dealHistory = "";
if (dt.Rows.Count > 0)
{
html.Append("<table class='table table-bordered'>");
html.Append("<thead>");
html.Append("<tr>");
html.Append("<th>S.No</th>");
html.Append("<th>FieldType</th>");
html.Append("<th>OldValue</th>");
html.Append("<th>NewValue</th>");
html.Append("<th>Full Name</th>");
html.Append("<th>UpdateOn</th>");
html.Append("</thead>");
html.Append("</tr>");
for (int i = 0; i < dt.Rows.Count; i = i + 1)
{
int sNo = i + 1;
html.Append("<tr>");
html.Append("<td>" + sNo + "</td>");
html.Append("<td>" + dt.Rows[i]["field"] + "</td>");
html.Append("<td>" + dt.Rows[i]["old"] + "</td>");
html.Append("<td>" + dt.Rows[i]["new"] + "</td>");
html.Append("<td>" + dt.Rows[i]["Name"] + "</td>");
html.Append("<td>" + dt.Rows[i]["UpdatedOn"] + "</td>");
html.Append("</tr>");
}
html.Append("</tbody>");
html.Append("</table>");
}
}
}
catch (Exception ex)
{
}
return html.ToString();
}
}

Updated:
Tried this:
string updateOn = "";
updateOn = ((DateTime)dt.Rows[i]["UpdatedOn"]).ToString("dd-MM-yyyy", CultureInfo.InvariantCulture);
html.Append("<td>" + updateOn + "</td>");
OR
You can use format DateTime in SQL:
More format at here.
In your case, it should be using style 105.
SELECT replace(convert(NVARCHAR, UpdatedOn, 105), ' ', '-')

You can convert it to string like:
dt.Rows[i]["UpdatedOn"].ToString("dd-MM-yyyy")
No need for any changes on SQL Server side, date formatting is a front-end problem.

Select convert(date,getdate())

Related

Apex - Test Batch class Execute method not covered code coverage

Batch apex class not covering execute method
Batch Apex Class : Trying to cover execute method but not covered in image above.
This batch class is on aggregate result. Please suggest me how to cover this batch class
It is working on search result of order if the status is based on filter then in execute method it will aggregate the value of fulfilment id of all orders related to it. based on their order status it executes.
global class UpdateOrderIntegrationStatus_Batch implements
Database.Batchable<AggregateResult>, Database.Stateful{
String status_filter = 'Waiting On Prior Order';
String query = 'select count(Id) cnt, Fulfillment__c from Apttus_Config2__Order__c'
+' where Fulfillment__c <> null and Order_Integration_Status__c = \''+status_filter + '\''
+' group by Fulfillment__c '
+' limit 50000';
public UpdateOrderIntegrationStatus_Batch(){}
public UpdateOrderIntegrationStatus_Batch(string q){
query = q;
}
global Iterable<AggregateResult> start(Database.BatchableContext BC){
//system.debug('>>>> query : ' + query)
return new AggregateResultIterable(query);
}
global void execute(Database.BatchableContext BC, List<sobject> results){
set<Id> fufillmentIds = new set<Id>();
for(Sobject sObj:results){
AggregateResult ar = (AggregateResult)sObj;
fufillmentIds.add((Id) ar.get('Fulfillment__c'));
}
List<Contract> fulfillments = [select id,(select Id, Order_Integration_Status__c from Orders__r order by Name asc) from Contract where id IN:fufillmentIds];
List<Apttus_Config2__Order__c> orderToUpdate = new List<Apttus_Config2__Order__c>();
String priorOrderIntegrationStatus = '';
for(Contract fulfillmentObj: fulfillments){
priorOrderIntegrationStatus = '';
for(Apttus_Config2__Order__c order: fulfillmentObj.Orders__r){
if(order.Order_Integration_Status__c == 'Processed'){
priorOrderIntegrationStatus = order.Order_Integration_Status__c;
}
else if(order.Order_Integration_Status__c == 'Error'){
break;
}
else if(order.Order_Integration_Status__c == status_filter && priorOrderIntegrationStatus == 'Processed' ){
order.Order_Integration_Status__c = 'Ready';
orderToUpdate.add(order);
priorOrderIntegrationStatus = order.Order_Integration_Status__c;
break;
}
else{
priorOrderIntegrationStatus = order.Order_Integration_Status__c; //For other statuses like Ready, Not Ready, Pending etc.
continue;
}
}
}
if(orderToUpdate <> null && orderToUpdate.size() > 0){
Database.update(orderToUpdate, false);
}
}
global void finish(Database.BatchableContext BC){
System.debug('UpdateOrderIntegrationStatus_Batch Finished');
}
}
Test Batch class:
#isTest
public class UpdateOrderIntegrationStatus_Batch_Test {
public static testMethod void testBatch() {
Test.StartTest();
Account acc =APTS_BvdUtility.createAccount();
Contact con = APTS_BvdUtility.createContact(acc.Id);
Apttus_Config2__AccountLocation__c acclocation = APTS_BvdUtility.createAccountLocation('Test Loc', acc.Id, con.Id);
Opportunity opp = APTS_BvdUtility.createOpportunity('Test Opp ', acc.Id, con.Id);
Apttus_Config2__PriceList__c priceList = APTS_BvdUtility.createPriceList('Test PriceBook') ;
Apttus_Proposal__Proposal__c quote = APTS_BvdUtility.createQuote(acc, opp, priceList.Id, 'quoteName');
Apttus_Config2__Order__c newOrder = new Apttus_Config2__Order__c();
newOrder.Apttus_QPConfig__ProposalId__c = quote.Id;
newOrder.Apttus_Config2__Status__c = 'Pending';
newOrder.AC_Billing_Street_1__c = '234';
newOrder.AC_Shipping_Street_1__c = '234';
newOrder.Ultimate_Parent_Account_ID__c = quote.Apttus_Proposal__Account__c;
newOrder.Apttus_Config2__BillToAccountId__c = quote.Apttus_Proposal__Account__c;
newOrder.Apttus_Config2__ShipToAccountId__c = quote.Apttus_Proposal__Account__c;
newOrder.Apttus_Config2__RelatedOpportunityId__c = quote.Apttus_Proposal__Opportunity__c;
newOrder.Apttus_Config2__OrderStartDate__c = quote.Apttus_Proposal__ExpectedStartDate__c;
newOrder.Apttus_Config2__OrderEndDate__c = quote.Apttus_Proposal__ExpectedEndDate__c;
newOrder.Apttus_Config2__SoldToAccountId__c = quote.Apttus_Proposal__Account__c;
newOrder.Apttus_Config2__PriceListId__c = quote.Apttus_QPConfig__PriceListId__c;
newOrder.Apttus_Config2__Type__c = quote.Apttus_QPConfig__ABOType__c;
newOrder.Language__c = 'English';
newOrder.Billing_Cycle__c = 'Annual';
newOrder.Detailed_Invoice__c = true;
newOrder.Bill_To_Contracting_Party__c = 'Test123';
newOrder.Ship_To_Contracting_Party__c = 'Test123';
newOrder.Bill_To_Location__c = acclocation.Id;
newOrder.Ship_To_Location__c = acclocation.id;
newOrder.Bill_To_Ultimate_Parent_Account_ID__c = quote.Apttus_Proposal__Account__c;
newOrder.Ship_To_Ultimate_Parent_Account_ID__c = quote.Apttus_Proposal__Account__c;
newOrder.Apttus_Config2__ActivatedDate__c = System.today();
newOrder.Apttus_Config2__OrderDate__c = System.today();
newOrder.CurrencyIsoCode = quote.CurrencyIsoCode;
newOrder.Apttus_Config2__ActivatedDate__c = System.Date.today();
newOrder.Order_Integration_Status__c='Waiting On Prior Order';
insert newOrder;
String testseqname = 'CONTRACT_NUMBER';
double testseqnumber = 1234;
Sequence_Number__c sn = new Sequence_Number__c(Name = testseqname, Next_Sequence_Number__c = testseqnumber);
insert sn;
List<SObject> contracts = new List<Contract>();
Contract fulfillment = new Contract();
fulfillment.Fulfillment_Total__c = 6000;
fulfillment.AccountId = acc.Id;
fulfillment.Opportunity__c = opp.Id;
fulfillment.Renewal_Opportunity__c = opp.Id;
fulfillment.CurrencyIsoCode = newOrder.CurrencyIsoCode;
fulfillment.CustomerSignedDate = system.today();
fulfillment.Status = 'Client Signed';
fulfillment.End_Date__c = newOrder.Apttus_Config2__OrderEndDate__c;
fulfillment.StartDate = newOrder.Apttus_Config2__OrderStartDate__c;
fulfillment.Latest_Order__c = newOrder.Id;
fulfillment.Billing_Integration_Status__c = 'Ready';
fulfillment.RecordTypeId = System.Label.Sales_Contract_RecordTypeID;
fulfillment.Original_Order_Submit_Date__c = newOrder.Apttus_Config2__ActivatedDate__c.date();
fulfillment.BusinessUnit__c = 'Bureau van Dijk Electronic Publishing Inc.';
fulfillment.Contract_Sequence_Number__c = 993360;
// insert fulfillment;
contracts.add(fulfillment);
insert contracts;
List<Contract> contrList = [select id,(select Id, Order_Integration_Status__c from Orders__r order by Name asc) from Contract where id =:contracts[0].Id];
newOrder.Order_Integration_Status__c='Processed';
update newOrder;
String status_filter = 'Waiting On Prior Order';
String query = 'select count(Id) cnt, Fulfillment__c from Apttus_Config2__Order__c'
+' where Fulfillment__c <> null and Order_Integration_Status__c = \''+status_filter + '\''
+' group by Fulfillment__c '
+' limit 50000';
/* String query = 'select id,(select Id, Order_Integration_Status__c from Orders__r order
by Name asc) from Contract';
String query = 'select count(Id) cnt, Fulfillment__c from Apttus_Config2__Order__c'
+' where Fulfillment__c =\''+contrList[0].Id +'\' and Order_Integration_Status__c =
\''+status_filter + '\''
+' group by Fulfillment__c '
+' limit 50000';
*/
UpdateOrderIntegrationStatus_Batch obj = new UpdateOrderIntegrationStatus_Batch();
UpdateOrderIntegrationStatus_Batch obj1 = new UpdateOrderIntegrationStatus_Batch(query);
// obj1.execute(BC, contracts);
ID batchprocessid = Database.executeBatch(obj);
ID batchprocessid1 = Database.executeBatch(obj1,10);
List<Apttus_Config2__Order__c> orders =[select Id,
Order_Integration_Status__c,Fulfillment__c from Apttus_Config2__Order__c where
Fulfillment__c=:contracts[0].Id];
for(Apttus_Config2__Order__c ord : orders){
system.assertEquals('Processed', ord.Order_Integration_Status__c);
}
Test.StopTest();
}
}

Convert Date Milliseconds to Valid DateTime React Native

I'm trying to convert the Milliseconds date from Server to valid datetime but for some reason the code is not working on react native as expected on my other applications
const ToValidDateTime = (date: string) => {
console.log(date); //HERE Date Returned by server is ( /Date(1617048540000)/ )
date = date.replace(/\D/g, ''); //removing everything just taking digits
var fullDate = new Date(date);
console.log(fullDate); //here is just giving me Date { NaN }
var twoDigitMonth = fullDate.getMonth() + 1 + "";
if (twoDigitMonth.length == 1) twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1) twoDigitDate = "0" + twoDigitDate;
var currentDate =
twoDigitMonth + "/" + twoDigitDate + "/" + fullDate.getFullYear();
return currentDate;
}
any ideas what am I doing wrong here?
Convert String to Number when you extract it from /Date(1617048540000)/
This should work
var fullDate = new Date(Number(date));
var date = "Date(1617048540000)"
console.log(date); //HERE Date Returned by server is ( /Date(1617048540000)/ )
date = date.replace(/\D/g, ''); //removing everything just taking digits
console.log(date)
var fullDate = new Date(Number(date));
console.log(fullDate.toDateString()); //here is just giving me Date { NaN }
var twoDigitMonth = fullDate.getMonth() + 1 + "";
if (twoDigitMonth.length == 1) twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1) twoDigitDate = "0" + twoDigitDate;
var currentDate =
twoDigitMonth + "/" + twoDigitDate + "/" + fullDate.getFullYear();

SQL : recursive looping

Table is as follows:
I'm trying to get the result set to have groups of all person that intersects from the table, hence creating following groups in result set from attached table.
Person1, Person2, Person3, Person7, Person8
Person5, Person6, Person9
So far I have following query, but can't seem to get the results intersected on a table of rows and outputted as 1 column.
DECLARE #r VARCHAR(MAX), #n INT, #i INT
SELECT #i = 1,
#r = 'SELECT BOX, ' + CHAR(13),
#n = (SELECT TOP 1 COUNT( USERS )
FROM EXCHANGE
GROUP BY BOX
ORDER BY COUNT( USERS ) DESC ) ;
WHILE #i <= #n BEGIN
SET #r = #r +
CASE WHEN #i = 1
THEN 'MAX( CASE Seq WHEN ' + CAST( #i AS VARCHAR ) + '
THEN USERS
ELSE SPACE(0) END ) + ' + CHAR(13)
WHEN #i = #n
THEN 'MAX( CASE Seq WHEN ' + CAST( #i AS VARCHAR ) + '
THEN '', '' + USERS
ELSE SPACE(0) END ) ' + CHAR(13)
ELSE 'MAX( CASE Seq WHEN ' + CAST( #i AS VARCHAR ) + '
THEN '', '' + USERS
ELSE SPACE(0) END ) + ' + CHAR(13)
END ;
SET #i = #i + 1 ;
END
SET #r = #r + '
FROM ( SELECT BOX, USERS,
ROW_NUMBER() OVER ( PARTITION BY BOX ORDER BY USERS )
FROM EXCHANGE p ) D ( BOX, USERS, Seq )
GROUP BY BOX;'
EXEC( #r ) ;
This type of graph walking is a pain in SQL Server -- you have cycles. The problem is avoiding cycles. Because SQL Server doesn't have very good data types, you need to store the visited nodes as strings.
You can do all this in a recursive CTE. The idea is to follow all paths from a node without repeating any node. Keep the minimum node visited. Voila! That specifies the path:
with cte as (
select box, users,
convert(varchar(max), concat(',', box, ',', users, ',')) as path,
(case when box < users then box else users end) as min_node
from exchange
union all
select cte.box, e.users,
concat(cte.path, e.users, ','),
(case when min_node < e.users then min_node else e.users end)
from cte join
exchange e
on e.box = cte.users
where path not like '%,' + e.users + ',%'
)
select cte.box, min(cte.users), min(cte.path), min(cte.min_node) as grouping
from cte
group by cte.box;
Here is a db<>fiddle.
This assumes that the edges are symmetric, so if you have (a, b), you also have (b, a).
If this is not the case, it is easy to add a CTE that makes this the case:
select box, users
from exchange
union -- on purpose to remove duplicates
select users, box
from exchange;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RecusriveGroup
{
public class FinalResult
{
public string GroupName { get; set; }
public string BoxName { get; set; }
public string UserName { get; set; }
}
class Program
{
static void Main(string[] args)
{
using(var con = new SqlConnection("Data Source=SQLServer;Initial Catalog=TESTDB;Integrated Security=SSPI"))
{
con.Open();
var cmd = new SqlCommand("select distinct Box from Exchange");
cmd.Connection = con;
var adapter = new SqlDataAdapter(cmd);
DataSet dsResult = new DataSet();
adapter.Fill(dsResult);
var finalResult = new List<FinalResult>();
var groupId = 0;
foreach (DataRow row in dsResult.Tables[0].Rows)
{
if(finalResult.Any(f => f.BoxName.Equals(row["Box"])))
{
continue;
}
groupId++;
RecursiveCall("Group" + groupId, row["Box"].ToString(), "", con, finalResult);
}
foreach(var result in finalResult)
{
var cmd1 = new SqlCommand("INSERT INTO FinalResult(Box, [User], [Group]) VALUES(#Box, #User, #Group)", con);
cmd1.Parameters.AddWithValue("#Box", result.BoxName);
cmd1.Parameters.AddWithValue("#User", result.UserName);
cmd1.Parameters.AddWithValue("#Group", result.GroupName);
cmd1.ExecuteNonQuery();
}
}
Console.ReadLine();
}
private static void RecursiveCall(string groupName, string boxName, string userName, SqlConnection sqlConnection, List<FinalResult> finalResult)
{
DataSet dsResult = new DataSet();
if (!string.IsNullOrEmpty(boxName) && !string.IsNullOrEmpty(userName))
{
var cmd = new SqlCommand("select Box, Users from Exchange WHERE Box = #BoxName OR Users = #UserName");
cmd.Parameters.AddWithValue("#BoxName", boxName);
cmd.Parameters.AddWithValue("#UserName", userName);
cmd.Connection = sqlConnection;
var adapter = new SqlDataAdapter(cmd);
adapter.Fill(dsResult);
}
else if(!string.IsNullOrEmpty(boxName))
{
var cmd = new SqlCommand("select Box, Users from Exchange WHERE Box = #BoxName");
cmd.Parameters.AddWithValue("#BoxName", boxName);
cmd.Connection = sqlConnection;
var adapter = new SqlDataAdapter(cmd);
adapter.Fill(dsResult);
}
else
{
var cmd = new SqlCommand("select Box, Users from Exchange WHERE Users = #UserName");
cmd.Parameters.AddWithValue("#UserName", userName);
cmd.Connection = sqlConnection;
var adapter = new SqlDataAdapter(cmd);
adapter.Fill(dsResult);
}
foreach (DataRow row in dsResult.Tables[0].Rows)
{
if (finalResult.Any(f => f.BoxName.Equals(row["Box"].ToString()) && f.UserName.Equals(row["Users"].ToString())))
{
continue;
}
finalResult.Add(new FinalResult() { GroupName = groupName, BoxName = row["Box"].ToString(), UserName = row["Users"].ToString() });
RecursiveCall(groupName, row["Box"].ToString(), row["Users"].ToString(), sqlConnection, finalResult);
}
}
}
}

Convert SQL code to linq (LIKE, SELECT)

How can I do the following sql code with linq?
SQL;
SELECT BusinessEntityID, FirstName + ' ' + LastName AS "FullName"
FROM Person WHERE FullName LIKE 'a%'
LINQ;
using(var db= new db_Context)
{
var query = db.Person.Select(q=> q.FirstName + " "+ q=>q.FullName)
}
Searching after concatenate the strings is not a good idea. I recommend the search for firstname and lastname separately
var query = db.Person.Where (t => t.FirstName.StartsWith("a") || t.LastName.StartsWith("a") )
.Select(q=> new { q.BusinessEntityID, Fullname = q.FirstName + " " + q.LastName })
Following code will be helpful to you,
var list1 = db.Person.Where (t => t.FirstName.StartsWith("a"))
.Select(q=> new {
BusinessEntityID = q.BusinessEntityID,
Fullname = q.FirstName + " " + q.LastName
});
Or
var list2 = from psn in db.Person
where psn.FirstName.StartsWith("a")
select new {
BusinessEntityID = psn.BusinessEntityID,
Fullname = psn.FirstName + " " + psn.LastName
};

SQL aspnet_profile

any idea how I can get user FirstName and LastName from the aspnet_profile table based on UserID using SQL becasue I would like to use in Telerik Reporting as a user parameter.
Sample row (FirstName is George, LastName is Test):
UserID: 06b24b5c-9aa1-426e-b7e4-0771c5f85e85
PropertyName: MobilePhone:S:0:0:Initials:S:0:1:City:S:1:14:FirstName:S:15:6:PostalCode:S:21:7:‌​WorkPhone:S:28:12:LastName:S:40:5:Address1:S:45:17:Address2:S:62:0:Province:S:62:‌​2:Organization:S:64:4:ClinicId:S:68:1:Country:S:69:6:Fax:S:75:0:MSPNumber:S:75:0:‌​
PropertyValuesString: HEast HustonEASGeorgeT7D 1N8604-111-2222Test5555 Beddtvue AveDCHCNL2Canada
PropertyValuesBinary: <Binary data>
LastUpdateDate: 2010-01-02 22:22:03.947
If you insist on using SQL, I'm sure a large number of SUBSTRINGs and PATINDEXes will get you there but it won't be a clean solution.
Update: user373721 found a great resource and posted a comment about it, but it can be easily missed, so I decided to add it to the answer, too - How to get asp.net profile value from MS SQL database using T-SQL?
The built-in dbo.aspnet_Profile_GetProperties stored procedure returns the PropertyValuesString value that is later parsed in the ParseDataFromDB
function.
private void GetPropertyValuesFromDatabase(string userName, SettingsPropertyValueCollection svc)
{
if (HostingEnvironment.IsHosted && EtwTrace.IsTraceEnabled(4, 8))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_PROFILE_BEGIN, HttpContext.Current.WorkerRequest);
}
HttpContext current = HttpContext.Current;
string[] names = null;
string values = null;
byte[] buffer = null;
if (current != null)
{
if (!current.Request.IsAuthenticated)
{
string anonymousID = current.Request.AnonymousID;
}
else
{
string name = current.User.Identity.Name;
}
}
try
{
SqlConnectionHolder connection = null;
SqlDataReader reader = null;
try
{
connection = SqlConnectionHelper.GetConnection(this._sqlConnectionString, true);
this.CheckSchemaVersion(connection.Connection);
SqlCommand command = new SqlCommand("dbo.aspnet_Profile_GetProperties", connection.Connection) {
CommandTimeout = this.CommandTimeout,
CommandType = CommandType.StoredProcedure
};
command.Parameters.Add(this.CreateInputParam("#ApplicationName", SqlDbType.NVarChar, this.ApplicationName));
command.Parameters.Add(this.CreateInputParam("#UserName", SqlDbType.NVarChar, userName));
command.Parameters.Add(this.CreateInputParam("#CurrentTimeUtc", SqlDbType.DateTime, DateTime.UtcNow));
reader = command.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
names = reader.GetString(0).Split(new char[] { ':' });
values = reader.GetString(1);
int length = (int) reader.GetBytes(2, 0L, null, 0, 0);
buffer = new byte[length];
reader.GetBytes(2, 0L, buffer, 0, length);
}
}
finally
{
if (connection != null)
{
connection.Close();
connection = null;
}
if (reader != null)
{
reader.Close();
}
}
ProfileModule.ParseDataFromDB(names, values, buffer, svc);
if (HostingEnvironment.IsHosted && EtwTrace.IsTraceEnabled(4, 8))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_PROFILE_END, HttpContext.Current.WorkerRequest, userName);
}
}
catch
{
throw;
}
}
internal static void ParseDataFromDB(string[] names, string values, byte[] buf, SettingsPropertyValueCollection properties)
{
if (((names != null) && (values != null)) && ((buf != null) && (properties != null)))
{
try
{
for (int i = 0; i < (names.Length / 4); i++)
{
string str = names[i * 4];
SettingsPropertyValue value2 = properties[str];
if (value2 != null)
{
int startIndex = int.Parse(names[(i * 4) + 2], CultureInfo.InvariantCulture);
int length = int.Parse(names[(i * 4) + 3], CultureInfo.InvariantCulture);
if ((length == -1) && !value2.Property.PropertyType.IsValueType)
{
value2.PropertyValue = null;
value2.IsDirty = false;
value2.Deserialized = true;
}
if (((names[(i * 4) + 1] == "S") && (startIndex >= 0)) && ((length > 0) && (values.Length >= (startIndex + length))))
{
value2.SerializedValue = values.Substring(startIndex, length);
}
if (((names[(i * 4) + 1] == "B") && (startIndex >= 0)) && ((length > 0) && (buf.Length >= (startIndex + length))))
{
byte[] dst = new byte[length];
Buffer.BlockCopy(buf, startIndex, dst, 0, length);
value2.SerializedValue = dst;
}
}
}
}
catch
{
}
}
}
http://www.karpach.com/Get-asp-net-profile-value-MS-SQL-database-using-T-SQL.htm
this helped me tremendously!
following the steps in that link allowed me to fetch any particular data in the PropertyValueString from the aspnet_profile table.
copying and pasting-
First Function:
CREATE FUNCTION dbo.fn_GetElement
(
#ord AS INT,
#str AS VARCHAR(8000),
#delim AS VARCHAR(1) )
RETURNS INT
AS
BEGIN
-- If input is invalid, return null.
IF #str IS NULL
OR LEN(#str) = 0
OR #ord IS NULL
OR #ord < 1
-- #ord > [is the] expression that calculates the number of elements.
OR #ord > LEN(#str) - LEN(REPLACE(#str, #delim, '')) + 1
RETURN NULL
DECLARE #pos AS INT, #curord AS INT
SELECT #pos = 1, #curord = 1
-- Find next element's start position and increment index.
WHILE #curord < #ord
SELECT
#pos = CHARINDEX(#delim, #str, #pos) + 1,
#curord = #curord + 1
RETURN
CAST(SUBSTRING(#str, #pos, CHARINDEX(#delim, #str + #delim, #pos) - #pos) AS INT)
END
Second Function:
CREATE FUNCTION dbo.fn_GetProfileElement
(
#fieldName AS NVARCHAR(100),
#fields AS NVARCHAR(4000),
#values AS NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
-- If input is invalid, return null.
IF #fieldName IS NULL
OR LEN(#fieldName) = 0
OR #fields IS NULL
OR LEN(#fields) = 0
OR #values IS NULL
OR LEN(#values) = 0
RETURN NULL
-- locate FieldName in Fields
DECLARE #fieldNameToken AS NVARCHAR(20)
DECLARE #fieldNameStart AS INTEGER,
#valueStart AS INTEGER,
#valueLength AS INTEGER
-- Only handle string type fields (:S:)
SET #fieldNameStart = CHARINDEX(#fieldName + ':S',#Fields,0)
-- If field is not found, return null
IF #fieldNameStart = 0 RETURN NULL
SET #fieldNameStart = #fieldNameStart + LEN(#fieldName) + 3
-- Get the field token which I've defined as the start of the
-- field offset to the end of the length
SET #fieldNameToken = SUBSTRING(#Fields,#fieldNameStart,LEN(#Fields)-#fieldNameStart)
-- Get the values for the offset and length
SET #valueStart = dbo.fn_getelement(1,#fieldNameToken,':')
SET #valueLength = dbo.fn_getelement(2,#fieldNameToken,':')
-- Check for sane values, 0 length means the profile item was
-- stored, just no data
IF #valueLength = 0 RETURN ''
-- Return the string
RETURN SUBSTRING(#values, #valueStart+1, #valueLength)
END
SQL Query can be modded to your needs
SELECT dbo.fn_GetProfileElement('FirstName',PropertyNames,PropertyValuesString)
, dbo.fn_GetProfileElement('LastName',PropertyNames,PropertyValuesString)
FROM aspnet_Profile
For those who are still looking for a method to parse the aspnet_Profile table using pure SQL. Here is what I use:
First you need a Tally table. If you do not know what this is, read this article by Jeff Moden: http://www.sqlservercentral.com/articles/T-SQL/62867/
For you to generate the tally table use this script:
SELECT TOP 11000 IDENTITY(INT,1,1) AS N INTO dbo.Tally FROM Master.dbo.SysColumns sc1, Master.dbo.SysColumns sc2
--===== Add a Primary Key to maximize performance
ALTER TABLE dbo.Tally ADD CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100
--===== Let the public use it
GRANT SELECT, REFERENCES ON dbo.Tally TO PUBLIC
Now on to parsing the ProfileData:
The process below is the fastest way I found to do this after lots of testing on my specific data. I have tested parsing the complete table in one go, but that runs slower than using the function below and parsing one user at a time with a CROSS APPLY.
So to call the function, use something like:
SELECT bla, bla
FROM aspnet_Users u CROSS APPY dbo.ProfileProperties(u.UserID)
The only thing you need to do is to update 3 things to contain the Profile Properties that you use:
1) the return table
2) the PIVOT statement, and
3) the insert statement copying the data from the PIVOT into the return table
Here is the function, Enjoy!
/** =============================================
** Author: Francois Grobler
** Create date: 2013-04-25
** Description: This function extracts all
** Profile Properties for a given UserId,
** and returns them as a table
** Change History:
** Date: Author: Change:
**
** ============================================= **/
CREATE FUNCTION dbo.ProfileProperties
(
#UserID UNIQUEIDENTIFIER
)
RETURNS #returnTable TABLE(
FirstName nvarchar(200)
, LastName nvarchar(200)
, PassportNumber nvarchar(100)
, PositionCode int
, CellNumber nvarchar(20)
, Telephone nvarchar(30)
, FaxNumber nvarchar(20)
, Email nvarchar(200)
, PersalNumber nvarchar(10)
, SouthAfricanIdentityNumber nchar(13)
, ContractNumber nvarchar(20)
, DepartmentName nvarchar(200)
, SiteName nvarchar(200)
, DepartmentCode int
, SiteCode int
, UserAccessCode int
, ApproverCode int
)
WITH SCHEMABINDING
AS
BEGIN
WITH Properties(PropertyNo, PropertyType, UserId, Value)
AS
(
SELECT (ROW_NUMBER() OVER(ORDER BY UserId) - 1) / 4 PropertyNo
, (ROW_NUMBER() OVER(PARTITION BY p.UserId ORDER BY UserId) - 1) % 4 PropertyType
, p.UserId
, SUBSTRING(':' + CONVERT(nvarchar(4000), p.PropertyNames), n + 1, CHARINDEX(':', ':' + CONVERT(nvarchar(4000), p.PropertyNames), n + 1) - n - 1) Value
FROM dbo.Tally, dbo.aspnet_Profile p
WHERE n < LEN(':' + CONVERT(nvarchar(4000), p.PropertyNames))
and SUBSTRING(':' + CONVERT(nvarchar(4000), p.PropertyNames), n, 1) = ':'
and p.UserId = #UserID
)
, FlatProperties(UserId, Property, ValueType, StartIndex, ValueLength)
AS
(
SELECT UserId
, MAX(CASE WHEN PropertyType = 0 THEN Value ELSE '' END) Property
, MAX(CASE WHEN PropertyType = 1 THEN Value ELSE '' END) ValueType
, MAX(CASE WHEN PropertyType = 2 THEN CONVERT(int, Value) + 1 ELSE 0 END) StartIndex
, MAX(CASE WHEN PropertyType = 3 THEN CONVERT(int, Value) ELSE 0 END) ValueLength
FROM
Properties
GROUP BY UserID, PropertyNo
)
, PropertyValues(UserID, PropertyName, PropertyValue)
AS
(
SELECT p.UserID, fp.Property
, CASE fp.ValueType
WHEN 'S' THEN SUBSTRING(p.PropertyValuesString, fp.StartIndex, fp.ValueLength)
ELSE SUBSTRING(p.PropertyValuesBinary, fp.StartIndex, fp.ValueLength) END Value
FROM dbo.aspnet_Profile p INNER JOIN flatProperties fp ON p.UserId = fp.UserId
WHERE p.UserId = #UserID
)
, PropertyTable
AS
(
SELECT
UserID
, pvt.[FirstName]
, pvt.[LastName]
, pvt.[PassportNumber]
, pvt.[PositionCode]
, pvt.[CellNumber]
, pvt.[Telephone]
, pvt.[FaxNumber]
, pvt.[Email]
, pvt.[PersalNumber]
, pvt.[SouthAfricanIdentityNumber]
, pvt.[ContractNumber]
, pvt.[DepartmentName]
, pvt.[SiteName]
, pvt.[DepartmentCode]
, pvt.[SiteCode]
, pvt.[UserCode] UserAccessCode
, pvt.[ApproverCode]
FROM PropertyValues
PIVOT (
MAX(PropertyValue) FOR PropertyName IN ([FirstName],[LastName],[PassportNumber],[PositionCode],[CellNumber],[Telephone],[FaxNumber],[Email],[PersalNumber],[SouthAfricanIdentityNumber],[ContractNumber],[DepartmentName],[SiteName],[DepartmentCode],[SiteCode],[UserCode],[ApproverCode])
) AS pvt
)
INSERT INTO #returnTable
(
FirstName
, LastName
, PassportNumber
, PositionCode
, CellNumber
, Telephone
, FaxNumber
, Email
, PersalNumber
, SouthAfricanIdentityNumber
, ContractNumber
, DepartmentName
, SiteName
, DepartmentCode
, SiteCode
, UserAccessCode
, ApproverCode
)
SELECT TOP 1
FirstName
, LastName
, PassportNumber
, PositionCode
, CellNumber
, Telephone
, FaxNumber
, Email
, PersalNumber
, SouthAfricanIdentityNumber
, ContractNumber
, DepartmentName
, SiteName
, DepartmentCode
, SiteCode
, UserAccessCode
, ApproverCode
FROM PropertyTable;
RETURN;
END
GO