BigQuery JavaScript UDF to call Array with values from SQL query - google-bigquery

I am trying to write my First Bigquery UDF function (converting Oracle Function) and thanks to SO for the help so far I am able to write partial code as shown below,
My task is when we pass the parameters like admit_date, input1, input2 are passed and discharge date should be calculated based on few calculations (not weekends or Holidays).
Apologies for any mistakes or any trouble
Here is my code :
DECLARE list ARRAY <STRING>;
DECLARE v_holiday STRING DEFAULT 'No';
DECLARE v_due_report INT64;
DECLARE v_due_report_cnt INT64;
DECLARE v_days INT64;
DECLARE p_date DATE;
DECLARE p_input1 STRING;
DECLARE p_input2 STRING;
DECLARE v_bus_day STRING;
DECLARE v_cnt INT64 DEFAULT 1;
DECLARE v_max_days INT64 DEFAULT 1;
#SET v_holiday = (SELECT CASE WHEN EXISTS (SELECT 1 FROM `examples.date_calendar` WHERE HOLIDAY = DATE(p_date)) THEN 'Yes' ELSE 'No' END);
select ARRAY(SELECT holiday from `examples.date_calendar`) AS list;
SET v_due_report_cnt = (SELECT COUNT(*) FROM `examples.report_assessments` WHERE p_date BETWEEN START_DATE AND END_DATE
AND p_input1 = ICD_9_CLASS AND p_input2 = LAST_MEDICATION);
CREATE TEMP FUNCTION patient_report_date(p_admit_date DATE, p_input1 STRING,p_input2 STRING)
RETURNS STRING
LANGUAGE js AS """
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dayName = days[p_admit_date.getDay()];
if(p_admit_date < Date.parse("2016-02-01") {
v_bus_day = p_admit_date.setDate(date.getDate() + 2);
return v_bus_day;
}
# SET v_HOLIDAY = (SELECT CASE WHEN EXISTS (SELECT 1 FROM `examples.date_calendar` WHERE HOLIDAY = TRUNC(p_admit_date)) THEN 'Yes' ELSE 'No' END)
if(!(dayName) in ('Saturday', 'Sunday')) && (v_holiday == 'No'){
v_days = 2;
}
else {
v_days = 3;
p_date_trunc_time = new Date(p_date.getFullYear(), p_date.getMonth(), p_date.getDate());
v_bus_day = new Date(p_date_trunc_time.setSeconds(p_date_trunc_time.getSeconds() + 1));
}
if(p_admit_date < Date.parse("2020-07-01") {
v_due_report = 0
}
#SET v_due_report_cnt = (SELECT COUNT(*) FROM `examples.report_assessments` WHERE p_admit_date BETWEEN START_DATE AND END_DATE
#AND p_input1 = ICD_9_CLASS AND p_input2 = LAST_MEDICATION);
else if (v_due_report_cnt > 0) {
v_due_report = 4
}
v_days = v_days + v_due_report;
while max_days <= v_days {
if (! days[(p_admit_date + day).getDay()] in ('Saturday','Sunday') && (list.indexOf(p_admit_date)==-1) {
v_cnt++
max_days++
}
day++
}
v_bus_day = p_admit_date + v_vcnt;
return v_bus_day
""";
WITH data_date AS (
SELECT DATE("2021-05-31") AS p_DATE, "ICD8081" AS p_input1, "CDE2" AS p_input2 UNION ALL
SELECT DATE("2021-05-30"),"ICD8211","DER2"
)
SELECT p_DATE,
p_input1,
p_input2,
patient_report_date(p_DATE,p_input1,p_input2) discharge_date
FROM data_date;
When I executed the function it throwed me error "SyntaxError: Unexpected Token"
any suggestions or feedback on this code would be helpful.
Appreciate your help
Thanks for reading my post.
Updated Code: Finally, I am able to get my code working, want to add in SO so it might be useful to someone.
DECLARE holiday_list ARRAY <STRING>;
DECLARE holiday_weekend ARRAY <STRING>;
DECLARE report_assessments ARRAY <STRUCT<ICD_9_CLASS STRING,LAST_MEDICATION STRING>>;
DECLARE p_admit_date DATE;
DECLARE p_input1 STRING;
DECLARE p_input2 STRING;
SET holiday_list = (SELECT ARRAY(SELECT CAST(holiday AS STRING) FROM `examples.date_calendar`));
SET v_due_report = (SELECT ARRAY(SELECT STRUCT(CAST(ICD_9_CLASS AS STRING) AS ICD_9_CLASS, CAST(LAST_MEDICATION AS STRING) AS LAST_MEDICATION) FROM `examples.report_assessments`));
CREATE TEMP FUNCTION patient_report_date(p_admit_date DATE, p_input1 STRING,p_input2 STRING)
RETURNS STRING
LANGUAGE js AS """
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var holiday_weekend = ['Saturday','Sunday'];
var v_signature=0;
var v_days=0;
var v_day =1;
var v_cnt=0;
var v_max_days=1;
if(p_admit_date < new Date("2016-02-01")) {
p_admit_date.setDate(p_admit_date.getDate() + 2);
}
else
{
if((holiday_weekend.indexOf(days[p_admit_date.getDay()]) === -1) && (holiday_list.indexOf(p_admit_date) === -1)) {
v_days = 2;
}
else {
   v_days = 3;
}
for(var i = 0; i < report_assessments.length; i++) {
if ((report_assessments[i]['ICD_9_CLASS'] === p_input1) && (signature_assessments[i]['LAST_MEDICATION'] === p_input2)) {
v_signature++;
}
}
if (p_admit_date < Date("2020-07-01") && (v_signature === 0)) {
v_days = v_days;
}
else if (v_signature > 0) {
v_days = v_days + 4;
}
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
while max_days <= v_days {
if ((holiday_list.indexOf(addDays(p_admit_date,v_day).toISOString().slice(0, 10)) === -1) && (holiday_weekend.indexOf(days[addDays(p_admit_date,v_day).getUTCDay()]) === -1)) {
v_max_days++;
}
v_day++;
v_cnt++;
}
p_admit_date.setDate(p_admit_date.getDate() + v_cnt);
}
return p_admit_date;
""";
WITH data_date AS (
SELECT DATE("2021-05-31") AS p_DATE, "ICD8081" AS p_input1, "CDE2" AS p_input2,holiday_list, report_assessments UNION ALL
SELECT DATE("2021-05-30"),"ICD8211","DER2",holiday_list, report_assessments
)
SELECT p_DATE,
p_input1,
p_input2,
patient_report_date(p_DATE,p_input1,p_input2,holiday_list, report_assessments) discharge_date
FROM `examples.tabledata`; ```

Yes, javascript UDFs are also persistent. Just remove TEMP and optinally specify project name and dataset name:
CREATE FUNCTION project_name.dataset_name.patient_report_date(...)

Related

Age Calculator Difference in Kotlin [duplicate]

This question already has answers here:
How to create method for age calculation method in android
(7 answers)
Closed 4 months ago.
I have DatePicker Dialog, When I select date at that time I want to calculate age it's working but when I select date of current year at that time it showing the -1 age instead of 0 then how can solve this? Please help me to solve it.
My code is below:
public int getAge(int year, int month, int day) {
GregorianCalendar cal = new GregorianCalendar();
int y, m, d, noofyears;
y = cal.get(Calendar.YEAR);// current year ,
m = cal.get(Calendar.MONTH);// current month
d = cal.get(Calendar.DAY_OF_MONTH);// current day
cal.set(year, month, day);// here ur date
noofyears = (int) (y - cal.get(Calendar.YEAR));
LOGD("Age......", String.valueOf(noofyears));
if ((m < cal.get(Calendar.MONTH)) || ((m == cal.get(Calendar.MONTH)) && (d < cal.get(Calendar.DAY_OF_MONTH)))) {
--noofyears;
}
LOGD("Age......", String.valueOf(noofyears));
if (noofyears != 0) {
ageCount = noofyears;
} else {
ageCount = 0;
}
if (noofyears < 0)
throw new IllegalArgumentException("age < 0");
return noofyears;
}
java.time
For the sake of completeness and being up-to-date concerning packages, here is the way using java.time (Java 8+).
Java
public int getAge(int year, int month, int dayOfMonth) {
return Period.between(
LocalDate.of(year, month, dayOfMonth),
LocalDate.now()
).getYears();
}
Kotlin
fun getAge(year: Int, month: Int, dayOfMonth: Int): Int {
return Period.between(
LocalDate.of(year, month, dayOfMonth),
LocalDate.now()
).years
}
Both snippets need the following imports from java.time:
import java.time.LocalDate;
import java.time.Period
It's not recommended to use java.util.Date and java.util.Calendar anymore except from situations where you have to involve considerably large amounts of legacy code.
See also Oracle Tutorial.
For projects supporting Java 6 or 7, this functionality is available via the ThreeTenBP,
while there is special version, the ThreeTenABP for API levels below 26 in Android.
UPDATE
There's API Desugaring now in Android, which makes (a subset of) java.time directly available (no backport library needed anymore) to API levels below 26 (not really down to version 1, but will do for most of the API levels that should be supported nowadays).
private int getAge(String dobString){
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
date = sdf.parse(dobString);
} catch (ParseException e) {
e.printStackTrace();
}
if(date == null) return 0;
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dob.setTime(date);
int year = dob.get(Calendar.YEAR);
int month = dob.get(Calendar.MONTH);
int day = dob.get(Calendar.DAY_OF_MONTH);
dob.set(year, month+1, day);
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
age--;
}
return age;
}
Here is a Kotlin extension of the Date class returning the age corresponding to a Date object
val Date.age: Int
get() {
val calendar = Calendar.getInstance()
calendar.time = Date(time - Date().time)
return 1970 - (calendar.get(Calendar.YEAR) + 1)
}
It is compatible for all Android versions. If you wonder what '1970' is, that's the Unix Epoch. The timestamp is 0 on January 1, 1970.
private boolean getAge(int year, int month, int day) {
try {
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dob.set(year, month, day);
int monthToday = today.get(Calendar.MONTH) + 1;
int monthDOB = dob.get(Calendar.MONTH)+1;
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (age > 18) {
return true;
} else if (age == 18) {
if (monthDOB > monthToday) {
return true;
} else if (monthDOB == monthToday) {
int todayDate = today.get(Calendar.DAY_OF_MONTH);
int dobDate = dob.get(Calendar.DAY_OF_MONTH);
if (dobDate <= todayDate) { // should be less then
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static int getPerfectAgeInYears(int year, int month, int date) {
Calendar dobCalendar = Calendar.getInstance();
dobCalendar.set(Calendar.YEAR, year);
dobCalendar.set(Calendar.MONTH, month);
dobCalendar.set(Calendar.DATE, date);
int ageInteger = 0;
Calendar today = Calendar.getInstance();
ageInteger = today.get(Calendar.YEAR) - dobCalendar.get(Calendar.YEAR);
if (today.get(Calendar.MONTH) == dobCalendar.get(Calendar.MONTH)) {
if (today.get(Calendar.DAY_OF_MONTH) < dobCalendar.get(Calendar.DAY_OF_MONTH)) {
ageInteger = ageInteger - 1;
}
} else if (today.get(Calendar.MONTH) < dobCalendar.get(Calendar.MONTH)) {
ageInteger = ageInteger - 1;
}
return ageInteger;
}
Consider Today's Date - 30th August 2020
If Birthdate - 29th July 1993, the output - 27
If Birthdate - 29th August 1993, the output - 27
If Birthdate - 30th August 1993, the output - 27
If Birthdate - 31st August 1993, the output - 26
If Birthdate - 31st September 1993, the output - 26
Now for kotlin Language:
import java.util.Calendar
fun main(args: Array<String>) {
print(getAge(yyyy,mm,dd))
}
fun getAge(year: Int, month: Int, day: Int): String {
val dob = Calendar.getInstance()
val today = Calendar.getInstance()
dob.set(year, month, day)
var age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR)
if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
age--
}
val ageInt = age + 1
return ageInt.toString()
}
private void calculateAge() {
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay();
age.calculateMonths();
age.calTotalWeeks();
age.calTotalHours();
age.calTotalMins();
age.calTotalSecs();
age.calTotalMilsecs();
// Toast.makeText(getContext(), "click the resulted button"+age.getResult() , Toast.LENGTH_SHORT).show();
result.setText("AGE (DD/MM/YY) :" + age.getResult());
}
after that create one class
public class AgeCalculation {
private int startYear;
private int startMonth;
private int startDay;
private int endYear;
private int endMonth;
private int endDay;
private int resYear;
private int resMonth;
private int resDay;
private Calendar start;
private Calendar end;
public String getCurrentDate()
{
end=Calendar.getInstance();
endYear=end.get(Calendar.YEAR);
endMonth=end.get(Calendar.MONTH);
endMonth++;
endDay=end.get(Calendar.DAY_OF_MONTH);
return endDay+":"+endMonth+":"+endYear;
}
public void setDateOfBirth(int sYear, int sMonth, int sDay)
{
startYear=sYear;
startMonth=sMonth;
startDay=sDay;
}
public void calcualteYear()
{
resYear=endYear-startYear/(365);
}
public void calcualteMonth()
{
if(endMonth>=startMonth)
{
resMonth= endMonth-startMonth;
}
else
{
resMonth=endMonth-startMonth;
resMonth=12+resMonth;
resYear--;
}
}
public void calcualteDay()
{
if(endDay>=startDay)
{
resDay= endDay-startDay;
}
else
{
resDay=endDay-startDay;
resDay=30+resDay;
if(resMonth==0)
{
resMonth=11;
resYear--;
}
else
{
resMonth--;
}
}
}
public String getResult()
{
return resDay+":"+resMonth+":"+resYear;
}
public String getAge(int year, int month, int day) {
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dob.set(year, month-1, day);
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
age--;
}
Integer ageInt = new Integer(age);
String ageS = ageInt.toString();
return ageS;
}
static int calculateAge(int birthdayDay, int birthdayMonth, int birthdayYear)
{
DateTime date = DateTime(birthdayYear, birthdayMonth, birthdayDay).toLocal();
DateTime now = DateTime.now().toLocal();
return now.difference(date).inDays ~/ 365.2425;
}
public int getAge(int year, int month, int day) {
final Calendar birthDay = Calendar.getInstance();
birthDay.set(year, month, day);
final Calendar current = Calendar.getInstance();
if (current.getTimeInMillis() < birthDay.getTimeInMillis())
throw new IllegalArgumentException("age < 0");
int age = current.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
if (birthDay.get(Calendar.MONTH) > current.get(Calendar.MONTH) ||
(birthDay.get(Calendar.MONTH) == current.get(Calendar.MONTH) &&
birthDay.get(Calendar.DATE) > current.get(Calendar.DATE)))
age--;
return age;
}
This is how I implement in my source code, I tested. Hope that it is useful :
public static int getAge(String dateTime, String currentFormat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(currentFormat);
try {
Date date = dateFormat.parse(dateTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
Date currentDate = new Date();
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(currentDate);
int currentYear = currentCalendar.get(Calendar.YEAR);
int currentMonth = currentCalendar.get(Calendar.MONTH);
int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
int deltaYear = currentYear - year;
int deltaMonth = currentMonth - month;
int deltaDay = currentDay - day;
if (deltaYear > 0) {
if (deltaMonth < 0) {
deltaYear --;
} else if (deltaDay < 0){
deltaYear --;
}
return deltaYear;
}
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return 0;
}
String getAgeInOther(int year, int month, int day) {
Calendar today = Calendar.getInstance();
Calendar birth = Calendar.getInstance();
birth.set(year, month, day);
Calendar temp = Calendar.getInstance();
temp.set(year, month, day);
int totalDays = 0;
int intMonth=0,intDays=0;
for (int iYear = birth.get(Calendar.YEAR); iYear <= today.get(Calendar.YEAR); iYear++) {
if (iYear == today.get(Calendar.YEAR) && iYear == birth.get(Calendar.YEAR)) {
for (int iMonth = birth.get(Calendar.MONTH); iMonth <= today.get(Calendar.MONTH); iMonth++) {
temp.set(iYear, iMonth, 1);
if ((iMonth == today.get(Calendar.MONTH)) && (iMonth == birth.get(Calendar.MONTH))) {
totalDays += today.get(Calendar.DAY_OF_MONTH) - birth.get(Calendar.DAY_OF_MONTH);
} else if ((iMonth != today.get(Calendar.MONTH)) && (iMonth != birth.get(Calendar.MONTH))) {
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
intMonth++;
}else if ((iMonth == birth.get(Calendar.MONTH))) {
totalDays +=( birth.getActualMaximum(Calendar.DAY_OF_MONTH)- birth.get(Calendar.DAY_OF_MONTH));
} else if ((iMonth == today.get(Calendar.MONTH))){
totalDays += today.get(Calendar.DAY_OF_MONTH);
if (birth.get(Calendar.DAY_OF_MONTH)<today.get(Calendar.DAY_OF_MONTH))
{
intMonth++;
intDays=today.get(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH);
}else {
temp.set(today.get(Calendar.YEAR),today.get(Calendar.MONTH)-1,1);
intDays=temp.getActualMaximum(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH)+today.get(Calendar.DAY_OF_MONTH);
}
}
}
} else if ((iYear != today.get(Calendar.YEAR)) && (iYear != birth.get(Calendar.YEAR))) {
for (int iMonth = 0; iMonth < 12; iMonth++) {
temp.set(iYear, iMonth, 1);
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
intMonth++;
}
} else if (((iYear) == birth.get(Calendar.YEAR))) {
for (int iMonth = birth.get(Calendar.MONTH); iMonth < 12; iMonth++) {
temp.set(iYear, iMonth, 1);
if ((iMonth == birth.get(Calendar.MONTH))) {
totalDays += (birth.getActualMaximum(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH));
} else {
intMonth++;
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
} else if (iYear == today.get(Calendar.YEAR)) {
for (int iMonth = 0; iMonth <= today.get(Calendar.MONTH); iMonth++) {
temp.set(iYear, iMonth, 1);
if ((iMonth == today.get(Calendar.MONTH))) {
totalDays += today.get(Calendar.DAY_OF_MONTH);
if (birth.get(Calendar.DAY_OF_MONTH)<today.get(Calendar.DAY_OF_MONTH))
{
intMonth++;
intDays=today.get(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH);
}else {
temp.set(today.get(Calendar.YEAR),today.get(Calendar.MONTH)-1,1);
intDays=temp.getActualMaximum(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH)+today.get(Calendar.DAY_OF_MONTH);
}
} else {
intMonth++;
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
}
}
int ageYear=intMonth/12;
int ageMonth=intMonth%12;
int ageDays=intDays;
//TODO if you want age in YEAR:MONTH:DAY REMOVE COMMENTS
//TODO return ageYear+":"+ageMonth+":"+ageDays;
return ""+totalDays;//todo TOTAL AGE IN DAYS
}
public static String calculateAge(String strDate) {
int years = 0;
int months = 0;
int days = 0;
try {
long timeInMillis = Long.parseLong(strDate);
Date birthDate = new Date(timeInMillis);
//create calendar object for birth day
Calendar birthDay = Calendar.getInstance();
birthDay.setTimeInMillis(birthDate.getTime());
//create calendar object for current day
long currentTime = System.currentTimeMillis();
Calendar now = Calendar.getInstance();
now.setTimeInMillis(currentTime);
//Get difference between years
years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
int currMonth = now.get(Calendar.MONTH) + 1;
int birthMonth = birthDay.get(Calendar.MONTH) + 1;
//Get difference between months
months = currMonth - birthMonth;
//if month difference is in negative then reduce years by one and calculate the number of months.
if (months < 0) {
years--;
months = 12 - birthMonth + currMonth;
if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
months--;
} else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
years--;
months = 11;
}
//Calculate the days
if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))
days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);
else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
int today = now.get(Calendar.DAY_OF_MONTH);
now.add(Calendar.MONTH, -1);
days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;
} else {
days = 0;
if (months == 12) {
years++;
months = 0;
}
}
//adarsh
if (currMonth > birthMonth) {
if (birthDay.get(Calendar.DATE) > now.get(Calendar.DATE)) {
months = months - 1;
}
}//---------------------------------
} catch (Exception e) {
e.printStackTrace();
}
//Create new Age object
return years + " Y " + months + " M " + days + " days";
}
Here is my solution in Kotlin:
import java.time.LocalDateTime
fun getAge(birthYear: Int, birthMonth: Int, birthDay: Int): Int {
var age: Int = LocalDateTime.now().year - birthYear
if (birthMonth > LocalDateTime.now().monthValue || birthMonth == LocalDateTime.now().monthValue && birthDay > LocalDateTime.now().dayOfMonth) { age-- }
if (age < 0) { age = 0 }
return age
}
int age =0;
age = yearLatest - yearBirth;
if (monthAge > currentMonth) {
if (age != 0) {
age = age - 1;
}
} else if(monthAge == currentMonth){
if (dayAge > currentDay) {
if (age != 0) {
age = age - 1;
}
}
}
return age;
If we want to directly check if age is below or above X age then we can use LocalDate type, work for all the Android API levels.
LocalDate.now().minusYears(18).isBefore(value) //value is your localDate
This is the shortest I could get it to.
static int calculateAge(Calendar birthDay){
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
if (birthDay.get(Calendar.DAY_OF_YEAR) < today.get(Calendar.DAY_OF_YEAR)) {
age--;
}
return age;
}
New Language Dart using DateTime
static int getPerfectAgeInYears(DateTime dob,DateTime today) {
dob = DateTime(dob.year,dob.month,dob.day);
int ageInteger = 0;
today = DateTime(today.year,today.month,today.day);
ageInteger = today.year-dob.year;
if (today.month == dob.month) {
if (today.day < dob.day) {
ageInteger = ageInteger - 1;
}
} else if (today.month < dob.month) {
ageInteger = ageInteger - 1;
}
return ageInteger;}
Call as print(getPerfectAgeInYears(DateTime(2000,6,4),DateTime.now()));
Consider Today's Date - 30th August 2020
If Birthdate - 29th July 1993, the output - 27
If Birthdate - 29th August 1993, the output - 27
If Birthdate - 30th August 1993, the output - 27
If Birthdate - 31st August 1993, the output - 26
If Birthdate - 31st September 1993, the output - 26

REGEXP_REPLACE pattern has to be const? Comparing strings in BigQuery

I'm trying to measure similarity between strings using Dice's Coefficient (aka Pair Similarity) in BigQuery. For a second I thought that I can do that using just standard functions.
Suppose I need to compare "gana" and "gano". Then I would "cook" these two strings upfront into 'ga|an|na' and 'ga|an|no' (lists of 2-grams) and do this:
REGEXP_REPLACE('ga|an|na', 'ga|an|no', '')
Then based on change in length I can calculate my coeff.
But once applied to the table I get:
REGEXP_REPLACE second argument must be const and non-null
Is there any workaround for that? With simple REPLACE() second argument can be a field.
Maybe there is a better way to do it? I know, I can do UDF instead. But I wanted to avoid them here. We are running big tasks and UDFs are generally slower (at least in my experience) and are subject to different concurrency limit.
You can have JavaScript code inside for BigQuery SQL queries.
To measure similarity you could use Levenshtein's distance with a query like this (from https://stackoverflow.com/a/33443564/132438):
SELECT *
FROM js(
(
SELECT title,target FROM
(SELECT 'hola' title, 'hello' target), (SELECT 'this is beautiful' title, 'that is fantastic' target)
),
title, target,
// Output schema.
"[{name: 'title', type:'string'},
{name: 'target', type:'string'},
{name: 'distance', type:'integer'}]",
// The function
"function(r, emit) {
var _extend = function(dst) {
var sources = Array.prototype.slice.call(arguments, 1);
for (var i=0; i<sources.length; ++i) {
var src = sources[i];
for (var p in src) {
if (src.hasOwnProperty(p)) dst[p] = src[p];
}
}
return dst;
};
var Levenshtein = {
/**
* Calculate levenshtein distance of the two strings.
*
* #param str1 String the first string.
* #param str2 String the second string.
* #return Integer the levenshtein distance (0 and above).
*/
get: function(str1, str2) {
// base cases
if (str1 === str2) return 0;
if (str1.length === 0) return str2.length;
if (str2.length === 0) return str1.length;
// two rows
var prevRow = new Array(str2.length + 1),
curCol, nextCol, i, j, tmp;
// initialise previous row
for (i=0; i<prevRow.length; ++i) {
prevRow[i] = i;
}
// calculate current row distance from previous row
for (i=0; i<str1.length; ++i) {
nextCol = i + 1;
for (j=0; j<str2.length; ++j) {
curCol = nextCol;
// substution
nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
// insertion
tmp = curCol + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// deletion
tmp = prevRow[j + 1] + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// copy current col value into previous (in preparation for next iteration)
prevRow[j] = curCol;
}
// copy last col value into previous (in preparation for next iteration)
prevRow[j] = nextCol;
}
return nextCol;
}
};
var the_title;
try {
the_title = decodeURI(r.title).toLowerCase();
} catch (ex) {
the_title = r.title.toLowerCase();
}
emit({title: the_title, target: r.target,
distance: Levenshtein.get(the_title, r.target)});
}")
Below is tailored for similarity
Was used in How to perform trigram operations in Google BigQuery? and based on https://storage.googleapis.com/thomaspark-sandbox/udf-examples/pataky.js by #thomaspark
SELECT text1, text2, similarity FROM
JS(
// input table
(
SELECT * FROM
(SELECT 'mikhail' AS text1, 'mikhail' AS text2),
(SELECT 'mikhail' AS text1, 'mike' AS text2),
(SELECT 'mikhail' AS text1, 'michael' AS text2),
(SELECT 'mikhail' AS text1, 'javier' AS text2),
(SELECT 'mikhail' AS text1, 'thomas' AS text2)
) ,
// input columns
text1, text2,
// output schema
"[{name: 'text1', type:'string'},
{name: 'text2', type:'string'},
{name: 'similarity', type:'float'}]
",
// function
"function(r, emit) {
var _extend = function(dst) {
var sources = Array.prototype.slice.call(arguments, 1);
for (var i=0; i<sources.length; ++i) {
var src = sources[i];
for (var p in src) {
if (src.hasOwnProperty(p)) dst[p] = src[p];
}
}
return dst;
};
var Levenshtein = {
/**
* Calculate levenshtein distance of the two strings.
*
* #param str1 String the first string.
* #param str2 String the second string.
* #return Integer the levenshtein distance (0 and above).
*/
get: function(str1, str2) {
// base cases
if (str1 === str2) return 0;
if (str1.length === 0) return str2.length;
if (str2.length === 0) return str1.length;
// two rows
var prevRow = new Array(str2.length + 1),
curCol, nextCol, i, j, tmp;
// initialise previous row
for (i=0; i<prevRow.length; ++i) {
prevRow[i] = i;
}
// calculate current row distance from previous row
for (i=0; i<str1.length; ++i) {
nextCol = i + 1;
for (j=0; j<str2.length; ++j) {
curCol = nextCol;
// substution
nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
// insertion
tmp = curCol + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// deletion
tmp = prevRow[j + 1] + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// copy current col value into previous (in preparation for next iteration)
prevRow[j] = curCol;
}
// copy last col value into previous (in preparation for next iteration)
prevRow[j] = nextCol;
}
return nextCol;
}
};
var the_text1;
try {
the_text1 = decodeURI(r.text1).toLowerCase();
} catch (ex) {
the_text1 = r.text1.toLowerCase();
}
try {
the_text2 = decodeURI(r.text2).toLowerCase();
} catch (ex) {
the_text2 = r.text2.toLowerCase();
}
emit({text1: the_text1, text2: the_text2,
similarity: 1 - Levenshtein.get(the_text1, the_text2) / the_text1.length});
}"
)
ORDER BY similarity DESC
REGEXP_REPLACE second argument must be const and non-null
Is there any
workaround for that?
Below is just an idea/direction to address above question applied to logic you described:
I would "cook" these two strings upfront into 'ga|an|na' and
'ga|an|no' (lists of 2-grams) and do this: REGEXP_REPLACE('ga|an|na',
'ga|an|no', ''). Then based on change in length I can calculate my
coeff.
The "workaround" is:
SELECT a.w AS w1, b.w AS w2, SUM(a.x = b.x) / COUNT(1) AS c
FROM (
SELECT w, SPLIT(p, '|') AS x, ROW_NUMBER() OVER(PARTITION BY w) AS pos
FROM
(SELECT 'gana' AS w, 'ga|an|na' AS p)
) AS a
JOIN (
SELECT w, SPLIT(p, '|') AS x, ROW_NUMBER() OVER(PARTITION BY w) AS pos
FROM
(SELECT 'gano' AS w, 'ga|an|no' AS p),
(SELECT 'gamo' AS w, 'ga|am|mo' AS p),
(SELECT 'kana' AS w, 'ka|an|na' AS p)
) AS b
ON a.pos = b.pos
GROUP BY w1, w2
Maybe there is a better way to do it?
Below is the simple example of how Pair Similarity can be approached here (including building bigrams sets and calculation of coefficient:
SELECT
a.word AS word1, b.word AS word2,
2 * SUM(a.bigram = b.bigram) /
(EXACT_COUNT_DISTINCT(a.bigram) + EXACT_COUNT_DISTINCT(b.bigram) ) AS c
FROM (
SELECT word, char + next_char AS bigram
FROM (
SELECT word, char, LEAD(char, 1) OVER(PARTITION BY word ORDER BY pos) AS next_char
FROM (
SELECT word, SPLIT(word, '') AS char, ROW_NUMBER() OVER(PARTITION BY word) AS pos
FROM
(SELECT 'gana' AS word)
)
)
WHERE next_char IS NOT NULL
GROUP BY 1, 2
) a
CROSS JOIN (
SELECT word, char + next_char AS bigram
FROM (
SELECT word, char, LEAD(char, 1) OVER(PARTITION BY word ORDER BY pos) AS next_char
FROM (
SELECT word, SPLIT(word, '') AS char, ROW_NUMBER() OVER(PARTITION BY word) AS pos
FROM
(SELECT 'gano' AS word)
)
)
WHERE next_char IS NOT NULL
GROUP BY 1, 2
) b
GROUP BY 1, 2

Most efficient way to deal with ORA-01795:maximum number of expressions in a list is 1000 in hibernate

I have to perform a select on which I have more than 1000 elements via hibernate, and then I received the error "ORA-01795:maximum number of expressions in a list is 1000" when I'm using the Oracle brand.
SELECT * FROM matable WHERE column IN (?,?,...) (>1000 items)
I found many solutions :
Split the list with OR
where A in (a,b,c,d,e,f)
becomes
where (A in (a,b,c) OR a in (d,e,f)) ...
Create a table with UNION ALL
SELECT * FROM maintable
JOIN (
SELECT v1 a FROM DUAL UNION ALL
SELECT v2 a FROM DUAL UNION ALL
SELECT v3 a FROM DUAL UNION ALL
...
SELECT v2000 a FROM DUAL) tmp
on tmp.a = maintable.id
Using tuples to get rid of the limit
where (column,0) in ((1,0),(2,0),(3,0),(4,0), ... ,(1500,0))
Using a temporary table..
where A in SELECT item FROM my_temporary_table
References here and there and also there.
My question is the following : what is the best practice to deal with this issue? By best practice I mean the most performant, but not only for Oracle; if I use hibernate, I don't want to create and manage a different code for each brand of database (I'm concerned by Oracle, MS SQL and PostGre only).
My first reaction would have been to use a temporary table, but I don't know what has the most impact.
Use a temporary table and make the values primary keys on the table. This should allow very efficient optimizations for comparison. The most like is simply an index lookup, although if the table is very small, Oracle might choose some other method such as a table scan.
This method should be faster than 1,000 or conditions, in almost any database. Sometimes in is optimized in a similar way (using a binary tree to store the values). In such databases, the performance would be similar.
I fixed this issue with some changes in hibernate-core jar.
I made a helper class to split an expression in more joins like: ... t.column IN (: list_1) OR t.column IN (: list_2) ... , Then I changed AbstractQueryImpl.expandParameterList method from hibernate to call my method if the collection exceeds the limit.
My hibernate-core version is 3.6.10.Final, but it work fine and for 4.x versions - I tested it.
My code is tested for next cases:
where t.id in (:idList)
where (t.id in (:idList))
where ((t.id) in (:idList))
where 1=1 and t.id in (:idList)
where 1=1 and (t.id in (:idList))
where 1=1 and(t.id) in (:idList)
where 1=1 and((t.id) in (:idList))
where 1=1 and(t.id in (:idList))
where t.id not in (:idList)
where (t.id not in (:idList))
where ((t.id) not in (:idList))
AbstractQueryImpl.expandParameterList :
private String expandParameterList(String query, String name, TypedValue typedList, Map namedParamsCopy) {
Collection vals = (Collection) typedList.getValue();
Type type = typedList.getType();
boolean isJpaPositionalParam = parameterMetadata.getNamedParameterDescriptor( name ).isJpaStyle();
String paramPrefix = isJpaPositionalParam ? "?" : ParserHelper.HQL_VARIABLE_PREFIX;
String placeholder =
new StringBuffer( paramPrefix.length() + name.length() )
.append( paramPrefix ).append( name )
.toString();
if ( query == null ) {
return query;
}
int loc = query.indexOf( placeholder );
if ( loc < 0 ) {
return query;
}
String beforePlaceholder = query.substring( 0, loc );
String afterPlaceholder = query.substring( loc + placeholder.length() );
// check if placeholder is already immediately enclosed in parentheses
// (ignoring whitespace)
boolean isEnclosedInParens =
StringHelper.getLastNonWhitespaceCharacter( beforePlaceholder ) == '(' &&
StringHelper.getFirstNonWhitespaceCharacter( afterPlaceholder ) == ')';
if ( vals.size() == 1 && isEnclosedInParens ) {
// short-circuit for performance when only 1 value and the
// placeholder is already enclosed in parentheses...
namedParamsCopy.put( name, new TypedValue( type, vals.iterator().next(), session.getEntityMode() ) );
return query;
}
// *** changes by Vasile Bors for HHH-1123 ***
// case vals.size() > 1000
if ((vals.size() >= InExpressionExpander.MAX_ALLOWED_PER_INEXPR) && isEnclosedInParens) {
InExpressionExpander inExpressionExpander = new InExpressionExpander(beforePlaceholder, afterPlaceholder);
if(inExpressionExpander.isValidInOrNotInExpression()){
List<String> list = new ArrayList<String>( vals.size() );
Iterator iter = vals.iterator();
int i = 0;
String alias;
while ( iter.hasNext() ) {
alias = ( isJpaPositionalParam ? 'x' + name : name ) + i++ + '_';
namedParamsCopy.put( alias, new TypedValue( type, iter.next(), session.getEntityMode() ) );
list.add(ParserHelper.HQL_VARIABLE_PREFIX + alias );
}
String expandedExpression = inExpressionExpander.expandExpression(list);
if(expandedExpression != null){
return expandedExpression;
}
}
}
// *** end changes by Vasile Bors for HHH-1123 ***
StringBuffer list = new StringBuffer(16);
Iterator iter = vals.iterator();
int i = 0;
while (iter.hasNext()) {
String alias = (isJpaPositionalParam ? 'x' + name : name) + i++ + '_';
namedParamsCopy.put(alias, new TypedValue(type, iter.next(), session.getEntityMode()));
list.append(ParserHelper.HQL_VARIABLE_PREFIX).append(alias);
if (iter.hasNext()) {
list.append(", ");
}
}
return StringHelper.replace(
beforePlaceholder,
afterPlaceholder,
placeholder.toString(),
list.toString(),
true,
true
);
}
My helper class InExpressionExpander:
package org.hibernate.util;
import org.hibernate.QueryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
/**
* Utility class for expand Hql and Sql IN expressions with a parameter with more than IN expression limit size (HHH-1123).
* <br/>
* It work for expression with formats:
* <pre>
*
* where t.id in (:idList)
* where (t.id in (:idList))
* where ((t.id) in (:idList))
* where 1=1 and t.id in (:idList)
* where 1=1 and (t.id in (:idList))
* where 1=1 and(t.id) in (:idList)
* where 1=1 and((t.id) in (:idList))
* where 1=1 and(t.id in (:idList))
*
* where t.id not in (:idList)
* where (t.id not in (:idList))
* where ((t.id) not in (:idList))
* </pre>
* <p/>
* Example:
* <pre>
* select t.id from tableOrEntity t where t.id IN (:idList)
* </pre
*
* #author Vasile Bors
* #since 13/12/2015.
*/
public class InExpressionExpander {
private static final Logger log = LoggerFactory.getLogger(InExpressionExpander.class);
public static final int MAX_ALLOWED_PER_INEXPR = 1000;
private static final int MAX_PARAMS_PER_INEXPR = 500;
private Stack<String> stackExpr = new Stack<String>();
private StringBuilder toWalkQuery;
private final String beforePlaceholder;
private final String afterPlaceholder;
private boolean wasChecked = false;
private boolean isEnclosedInParens = false;
private boolean isInExpr = false;
private boolean isNotInExpr = false;
public InExpressionExpander(String beforePlaceholder, String afterPlaceholder) {
this.toWalkQuery = new StringBuilder(beforePlaceholder);
this.beforePlaceholder = beforePlaceholder;
this.afterPlaceholder = afterPlaceholder;
}
public boolean isValidInOrNotInExpression() {
if (!wasChecked) {
String lastExpr = extractLastExpression();
if ("(".equals(lastExpr)) {
isEnclosedInParens = true;
lastExpr = extractLastExpression();
}
isInExpr = "in".equalsIgnoreCase(lastExpr);
}
wasChecked = true;
return isInExpr;
}
public String expandExpression(List paramList) {
if (isValidInOrNotInExpression()) {
final String lastExpr = extractLastExpression(false);
if ("not".equalsIgnoreCase(lastExpr)) {
isNotInExpr = true;
extractLastExpression(); //extract "not" and consume it
}
extractColumnForInExpression();
StringBuilder exprPrefixBuilder = new StringBuilder();
for (int i = stackExpr.size() - 1; i > -1; i--) {
exprPrefixBuilder.append(stackExpr.get(i)).append(' ');
}
if (!isEnclosedInParens) {
exprPrefixBuilder.append('(');
}
String expandedExpression = expandInExpression(exprPrefixBuilder, paramList);
String beforeExpression = getBeforeExpression();
String afterExpression = getAfterExpression();
String expandedQuery = new StringBuilder(beforeExpression).append(expandedExpression)
.append(afterExpression)
.toString();
if (log.isDebugEnabled()) {
log.debug(
"Query was changed to prevent exception for maximum number of expression in a list. Expanded IN expression query:\n {}",
expandedExpression);
log.debug("Expanded query:\n {}", expandedQuery);
}
return expandedQuery;
}
log.error("Illegal call of InExpressionExpander.expandExpression() without IN expression.");
return null;
}
private String expandInExpression(StringBuilder exprPrefixBuilder, List values) {
String joinExpr = isNotInExpr ? ") and " : ") or ";
StringBuilder expr = new StringBuilder(16);
Iterator iter = values.iterator();
int i = 0;
boolean firstExpr = true;
while (iter.hasNext()) {
if (firstExpr || i % MAX_PARAMS_PER_INEXPR == 0) {
//close previous expression and start new expression
if (!firstExpr) {
expr.append(joinExpr);
} else {
firstExpr = false;
}
expr.append(exprPrefixBuilder);
} else {
expr.append(", ");
}
expr.append(iter.next());
i++;
}
expr.append(')');// close for last in expression
return expr.toString();
}
/**
* Method extract last expression parsed by space from toWalkQuery and remove it from toWalkQuery;<br/>
* If expression has brackets it will return al content between brackets and it will add additional space to adjust splitting by space.
*
* #return last expression from toWalkQuery
*/
private String extractLastExpression() {
return extractLastExpression(true);
}
/**
* Method extract last expression parsed by space from toWalkQuery, remove it from toWalkQuery if is consume = true;<br/>
* If expression has brackets it will return al content between brackets and it will add additional space to adjust splitting by space.
*
* #param consum if true the method will extract and remove last expression from toWalkQuery
* #return last expression from toWalkQuery
*/
private String extractLastExpression(final boolean consum) {
int lastIndex = this.toWalkQuery.length() - 1;
String lastExpr;
int exprSeparatorIndex = this.toWalkQuery.lastIndexOf(" ");
if (lastIndex == exprSeparatorIndex) { //remove last space from the end
this.toWalkQuery.delete(exprSeparatorIndex, this.toWalkQuery.length());
return extractLastExpression(consum);
} else {
lastExpr = this.toWalkQuery.substring(exprSeparatorIndex + 1, this.toWalkQuery.length());
if (lastExpr.length() > 1) {
if (lastExpr.endsWith(")")) {
//if parens are closed at the end we need to find where it is open
int opensParens = 0;
int closedParens = 0;
int startExprIndex = -1;
char c;
for (int i = lastExpr.length() - 1; i > -1; i--) {
c = lastExpr.charAt(i);
if (c == ')') {
closedParens++;
} else if (c == '(') {
opensParens++;
}
if (closedParens == opensParens) {
startExprIndex = i;
break;
}
}
if (startExprIndex > -1) {
lastExpr = lastExpr.substring(startExprIndex, lastExpr.length());
exprSeparatorIndex = exprSeparatorIndex + startExprIndex
+ 1; // +1 because separator is not space and don't must be deleted
}
} else if (lastExpr.contains("(")) {
int parentsIndex = exprSeparatorIndex + lastExpr.indexOf('(') + 1;
this.toWalkQuery.replace(parentsIndex, parentsIndex + 1, " ( ");
return extractLastExpression(consum);
}
}
if (consum) {
this.toWalkQuery.delete(exprSeparatorIndex, this.toWalkQuery.length());
}
}
if (consum) {
stackExpr.push(lastExpr);
}
return lastExpr;
}
private String extractColumnForInExpression() {
String column = extractLastExpression();
String beforeColumn = extractLastExpression(false);
long pointIndx = beforeColumn.lastIndexOf('.');
if (pointIndx > -1) {
if (pointIndx == (beforeColumn.length() - 1)) {
throw new QueryException(
"Invalid column format: " + beforeColumn + ' ' + column
+ " . Remove space from column!");
}
}
return column;
}
private String getBeforeExpression() {
return this.toWalkQuery + " (";
}
private String getAfterExpression() {
if (StringHelper.getFirstNonWhitespaceCharacter(afterPlaceholder) == ')') {
return afterPlaceholder;
}
return afterPlaceholder + ") ";
}
}
I am happy to receive any suggestions for improving this solution.

Non recursive BST (binary search tree)

Hello I don't know what is wrong with my BST insert method.
Any suggestions it has too be non recursive it adds to the right always I want to know why it adds to the ends to the BST when I print it shows that the nodes were added at the right only.
void InsertBST(LZWCmp cmp, TreeNode **root, int code)
{
TreeNode tmp = *root;
TreeNode current = NULL;
Code temp;
Code temp1;
int size;
int comp;
int direction = -1;
if(*root == NULL)
root = CreateNode(code)
while(tmp != NULL) {
temp = GetCode(cmp->cst, tmp->cNum);
temp1 = GetCode(cmp->cst, code);
size = temp.size;
if(temp1.size < temp.size)
size = temp1.size;
comp = memcmp(temp1.data, temp.data, size);
if(temp1.size < temp.size && comp == 0)
comp = -1;
else if(temp1.size < temp.size && comp == 0)
comp = 1;
if(comp < 0) {
current = tmp;
direction = FALSE;
tmp = tmp->left;
} else (
current = tmp;
direction = TRUE;
tmp = tmp->right;
}
}
if(direction == FALSE)
current->left = CreateNode(code);
else
current->right = CreateNode(code);
}
There you go:
void InsertBST(LZWCmp cmp, TreeNode **root, int code)
{
Code temp;
Code temp1;
int size;
int comp;
while ( *root ) {
temp = GetCode(cmp->cst, (*root)->cNum);
temp1 = GetCode(cmp->cst, code);
size = (temp1.size < temp.size) ? temp1.size : temp.size;
comp = memcmp(temp1.data, temp.data, size);
if (comp ==0 ) comp = (temp1.size < temp.size ) ? -1 : 1;
root = (comp < 0) ? &(*root)->left
: &(*root)->right;
}
*root = CreateNode(code);
}
NOTE: this code does not check for duplicates. It always inserts a node.
NOTE2: I am not sure about the sign in: if (comp ==0 ) comp = (temp1.size < temp.size ) ? -1 : 1; (in the original the sign was the same in both cases)

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