Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:22+00:00 2026-05-26T01:20:22+00:00

I am to create a function to read a table containing some 65 tables

  • 0

I am to create a function to read a table containing some 65 tables and find all columns containing dates, which are stored mostly as int, sometimes as char or datetime. Column names are unknown but I assume ending in ‘%Date’.

I need to find the MAX ie latest date for each client based on his/her SSN. However, the SSN is also called different names, e.g. SSN, SSN_Nr, SSN_No, etc. My code below is hopelessly wrong:

CREATE FUNCTION dbo.LastActivityDate (@SSN varchar(10))
RETURNS datetime
AS
BEGIN
DECLARE @Result datetime

DECLARE @SSN varchar(10)
DECLARE @tableSSN varchar(10)
DECLARE @table varchar(100)
DECLARE @column_name nvarchar(100)
DECLARE @tableDate datetime
DECLARE @LastActivityDate datetime

SET @column_name='%DATE' --only ends in date, so as not to pick up 'update_office',etc

if @SSN IS NULL OR @SSN = '' OR @SSN = ' '  
begin
    BREAK
end 
else 
begin
  WHILE select Table_Name,Field1 from dbo.MERGE_TABLES where [Enabled]='Y' AND Field1=@SSN 
  BEGIN
      SET @table = Table_Name
      SET @tableSSN = Field1
      declare @column_name nvarchar(100)
      set @table = 'e_client'
      set @column_name='%DATE' --only ends in date, so as not to pick up 'update_office',etc
      SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name=@table AND column_name LIKE @column_name ORDER BY ordinal_position 
      IF ISDATE(SELECT .... FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name=@table AND column_name LIKE @column_name)=1
      begin
          CONVERT(datetime,(SELECT .... FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name=@table AND column_name LIKE @column_name))
      end
.
.
.
      IF (Select MAX([@tableDate]) from @table where @tableSSN=@SSN) > @LastActivityDate --column like FinishDate, proposedFinishDate  or startdate if finish is empty

      begin
         @LastActivityDate=@tableDate
      end
  END

end--@SSN
SET @Result=@LastActivityDate
RETURN @Result
END
GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:20:22+00:00Added an answer on May 26, 2026 at 1:20 am
    • You’ve got an undocumented database.
    • You don’t know the column names.
    • You don’t know which columns contain dates.
    • The dates are stored using at least three different data types.
    • You don’t know which columns contain SSANs.
    • Columns are inconsistently named.

    Some problems cry out for automation. I don’t think this is one of them.

    I wouldn’t trust a script to get it right in this database. I think you should look at each table, and determine by eye which columns contain dates and which columns contain SSANs. Even if it takes you 5 minutes a table, that’s still less than a day’s work.

    If you have to do it a second time, then you can automate it, based on your new-found knowledge of the tables and columns.

    If you worked here, you could use a text tool to find all the SSAN columns, because they’d all be based on a CREATE DOMAIN statement, like (PostgreSQL)

    CREATE DOMAIN SSAN AS CHAR(9) CHECK (VALUE ~ '^\\d{9}$');
    CREATE TABLE users (
        user_id INTEGER PRIMARY KEY,
        ssan SSAN NOT NULL UNIQUE,     -- These *are* unique where I work. YMMV.
        ...
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing some numeric columns, and i have to keep them
Here I have some function to read my table <?php public function get_all_record($table, $fields
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
How come the following doesn't work? CREATE FUNCTION Test (@top integer) RETURNS TABLE AS
A few examples to show, just incase: Inline Table Valued CREATE FUNCTION MyNS.GetUnshippedOrders() RETURNS
Source table: Create Table ExamAnswers { StudentID varchar(12), QuestionID int, Answer char(1) } and
I can't seem to read a UDT properly from a stored function with the
How to create a partitioned index on a table which is not partitioned although
I need to use the same Stored Procedures against many tables all with the
I can create function pointers in Fortran 90, with code like real, external ::

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.