SQL Comparison between dates

Sarvesh Pandey 71 Reputation points
2022-12-05T05:44:37.777+00:00

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

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

Answer accepted by question author
  1. Sreeju Nair 12,756 Reputation points
    2022-12-05T06:09:55.457+00:00

    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
    2022-12-05T05:56:53.203+00:00

    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.