Share via

SQL Comparison between dates

Sarvesh Pandey 71 Reputation points
Dec 5, 2022, 5:44 AM

Hi All,
On 5th of December 2022, I have created a table with date 5/12/2022.

Now when i am running the below query it is showing no recods. Note - getdate() = 5/12/2022

Getdate() - ![266919-image.png]2

Table detail -
266950-image.png

SQL Query -
select * from BusinessDays where Convert(varchar(2),Datepart(day,getdate())) = convert(varchar(2),'Business Day');

267041-image.png

I have also use CAST function but still getting no ouputs

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,672 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sreeju Nair 12,456 Reputation points
    Dec 5, 2022, 6:09 AM

    in your query, refer to the following part,

    convert(varchar(2), 'Business Day')

    Replace 'Business Day' to the column name Business Day and your query will work.

    select * from BusinessDays where Convert(varchar(2), DatePart(day, getdate())) = convert(varchar(2), [Business Day]);  
    

    Hope this helps


1 additional answer

Sort by: Most helpful
  1. NikoXu-msft 1,916 Reputation points
    Dec 5, 2022, 5:56 AM

    Hi @Sarvesh Pandey

    Change it to this:

    select * from BusinessDays where Convert(varchar(2),Datepart(day,getdate())) = convert(varchar(2),[Business Day]);  
    

    Best regards,
    Niko

    ----------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.