Question

My vendor sent over a piece of SQL code that I'm having a hell of a time getting to function with my existing query.

I have a webpage that is querying a database table for multiple values here's the code I have in the ASP page that queries SQL:

SELECT [Account],
[AccountName],
[ComputerName],
[EMail],
[Telephone],
[Status],
cast(cast([StartDate] as float) as datetime) as 'Start-Date',
cast(cast([LastBackupDate] as float) as datetime) as 'LastBackup-Date',
[TotalFileSize],
[TotalFileCount] 
FROM ExpandedView WHERE loginid = @0

This query works great, but the "Account" field specified above apparently returns only part of the actual account number. Apparently my vendor uses a computation to create the final account number and they've sent it to me:

use <databasenamehere>
select left(
convert(nvarchar,E.Account)
,5)+'-'+convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))
%10 )
+right(convert(nvarchar,E.Account),4) 
as 'Account', AccountName from ExpandView E
Inner Join Customer C
on E.Account=C.Account

Now I realize that that this query is selecting the first 5 numbers of the account number, appending the hyphen and then performing a mathematical procedure to the last 4 numbers, however I cannot figure out how to read this. Could I simply append these two statements together with an "AND" clause?

I really would like to understand what the "E.Account" refers to and why they use a "ExpandView E" and "Customer C" in the query. I'm also kind of lost how those relate to the actual tables.

Any assistance would be appreciated. Or if you prefer, post a link and I'll read up as best I can.

Thanks!

Was it helpful?

Solution 2

Here's the query I discovered that seems to work:

select left
(convert(nvarchar,E.Account)
,5)+'-'+ convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))%10 )
+right(convert(nvarchar,E.Account),4)
as 'Account',
 E.AccountName,
 E.ComputerName,
 E.regloginid,
 E.EMail,
 E.Telephone,
 E.Community,
 E.Status,
 CAST(cast(e.startdate as float) as datetime) as 'Start-Date', 
 CAST(cast(e.lastbackupdate as float) as datetime) as 'LastBackup-Date',
 E.TotalFileSize,
 E.TotalFileCount
 from ExpandedCustomerView E Where E.Status = 'A' and E.regloginid = @0 

It seems that the query they are using renames the database table to preface it with E.xxxxx I'm not certain how this works. I'm not terribly familiar with MSSQL to understand why they are calling a rename of the parameters in the select. However this query works without issue and provides the data I need.

Perhaps if someone can answer why they would use a parmeter around the "FROM EXPANDEDVIEW E" portion? That seems to be the point at which the renaming of the columns occurs.

Anyways, I'd be interested to know, but this definitely works for me, so I'm going to close this out. Thank you everyone for your answers/comments and help.

OTHER TIPS

This should work:

SELECT left(
convert(nvarchar,E.Account)
,5)+'-'+convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))
%10 )
+right(convert(nvarchar,E.Account),4) 
as 'Account',
[AccountName],
[ComputerName],
[EMail],
[Telephone],
[Status],
cast(cast([StartDate] as float) as datetime) as 'Start-Date',
cast(cast([LastBackupDate] as float) as datetime) as 'LastBackup-Date',
[TotalFileSize],
[TotalFileCount] 
FROM ExpandedView E WHERE loginid = @0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top