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

  • SEARCH
  • Home
  • 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 8583887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:38:30+00:00 2026-06-11T21:38:30+00:00

I have built a select statement to pick the date the participant was first

  • 0

I have built a select statement to pick the date the participant was first recorded in the database. However I want to create a function out of this select statement to use it. I am fairly new to SQL and have never built a function before. My select statement looks like this:

Select DATEDIFF(day, (select min(startdatetime)
                        from GamePlay
                     ), enddatetime)
  from GamePlay
 where ParticipantID = '200'

My attempted function looks like this:

CREATE FUNCTION daysPlayed (@ParticipantID int)
RETURNS DateTime
AS
BEGIN
    Return DATEDIFF(day, (select min(startdatetime)
                            from GamePlay
                         ), enddatetime)
      from GamePlay
     where ParticipantID = @ParticipantID
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-06-11T21:38:32+00:00Added an answer on June 11, 2026 at 9:38 pm

    DATEDIFF does not return a datetime. It returns an int.

    Your function might look like this:

    CREATE FUNCTION daysPlayed (@ParticipantID int)
    RETURNS INT
    AS
    BEGIN
        DECLARE @result INT
        SELECT @result=DATEDIFF(day, (select min(startdatetime) from GamePlay), enddatetime) from GamePlay where ParticipantID = @ParticipantID
        RETURN @result
    
    END
    

    but i don’t think it will do what you want it to do.
    Also, please note that using functions in selects over a large number of rows is a guarantee performance hit and you might not be able to use some of the indexes you set up in your tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a local DVD Database using Codeigniter, With film names etc in.
I have built a site using php and want to try keep it one
I have an application built upon ASP.NET 3.5 and PostgreSQL 8.3. My database has
I have a inline select statement to calculate the product of the set of
I want to do a SELECT statement where I replace every () with a
I need to build a form where I have 2 comboBoxes . Select country
I have built a site which requires user to login. Now I have decided
I have built so far an application that allows the user to drag and
I have built up an array of objects, created from a class, I wrote
I have built a Custom MaskedTextBox , changing the values of BeepOnError and AsciiOnly

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.