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 5929321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:24:24+00:00 2026-05-22T14:24:24+00:00

I have database with a Publications table that is many-to-may joined to iself through

  • 0

I have database with a Publications table that is many-to-may joined to iself through a SubPublications table

My stored procedure returns all of the distinct Year-Month combos from a ReleaseDate field of Publications of a specified type that are not related to a specific (by id) publication (hence the 2 params, see below).

QUESTION:
The proc works fine, but I want the return column type as DateTime2 with a dummy date of 1. As it is now, it returns 2 columns of integers. How do I do this?

I know I could do the conversion in my app code, but I’d rather have it delivered as a datetime from the DB.

My SQL ain’t great. I don’t even know if I should use a cast or a convert.

I can’t find an example online of converting back to datetime within a query like that. Can anyone help? Here’s the proc I wrote, as it stands:

ALTER PROCEDURE sp_DistinctPubMonthYears 
    @PubType char(1),
    @PubId int = 0      
AS
BEGIN
SELECT 
    DISTINCT TOP (100) PERCENT 
        DATEPART(month, ReleaseDate) AS month, 
        DATEPART(year, ReleaseDate) AS year         
    FROM(         
        SELECT 
            Publications.ReleaseDate AS ReleaseDate,
            Publications.PublicationId As PubId,
            Publications.PubType AS PubType,
            SubPublications.PublicationId AS ParentId
        FROM 
            Publications LEFT JOIN SubPublications
        ON 
            Publications.PublicationId = SubPublications.PublicationId 
        WHERE
            Publications.PubType = @PubType AND
            Publications.PublicationId <> @PubId AND
            (
                SubPublications.PublicationId <> @PubId OR  
                /*either it's parent is NOT the one we're searching on or */
                SubPublications.PublicationId IS NULL  
                /*or it's not joined to anything at all */
            )               
    ) AS sub
ORDER BY year ASC, month ASC 
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-22T14:24:25+00:00Added an answer on May 22, 2026 at 2:24 pm

    You don’t need TOP and you may as well ORDER BY the expression.

    This DATEADD/DATEDIFF expression will give you start of current month

    SELECT DISTINCT
        CAST(
          DATEADD(month, DATEDIFF(month, 0, ReleaseDate), 0) AS datetime2
        ) AS myCol 
    FROM(         
    ...
    ORDER BY
        DATEADD(month, DATEDIFF(month, 0, ReleaseDate), 0)
    

    Edit: As Faust mentioned, we can order on the alias if you prefer.

    ...
    ORDER BY
        myCol
    

    In this case the result is the same.

    If the CAST was to varchar then you would have different results. This is why I tend to use the expression not the alias but it’s quite trivial. Surely I’d test my changes…, no?

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

Sidebar

Related Questions

that my problem: I have database table like that: id (AI) market_id 1 6
I have a database table that stores document information (DocID, Name, Type, Owner, etc).
I have database with users table where i have many columns include passwords and
In my database I have tables that define types for example Table: Publication Types
i have database table like this +-------+--------------+----------+ | id | ip | date |
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1
Currently I have database with the following associations: One Client to Many Intakes One
I have quite a few situations where I have database structures similar to: TABLE
I have a Database that contains data about articles , structures and manufacturers .
I have a database that I have created using SQL Server Developer 2008. I

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.