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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:28:42+00:00 2026-05-26T16:28:42+00:00

I have written a sql that select data between a date and a another.

  • 0

I have written a sql that select data between a date and a another.
Basically the problem is about the date format that i wanted to made some selection

My coloum name is date_tm and sample data like this: 2011-04-04 00:30:19.107

My Sql like this:

DECLARE @Date_From DATETIME;
DECLARE @Date_To DATETIME;

SET @Date_From = CONVERT(VARCHAR(10),DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1,120);
SET @Date_To = CONVERT(VARCHAR(10),DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1,120);

select distinct no, lot
from database.dbo.TblName
Where Convert(varchar(10), Date_Tm, 120) Between '@Date_From' And '@Date_To';

I try to debug it using this

Print(@Date_From);
Print(@Date_To); 

The output is something like this “Nov 6 2011 12:00AM” am i not convert its alreadly?
and then i tested with select statement like this:

select CONVERT(VARCHAR(10),DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1,120);

The output is something like this “2011-11-06” why its work here?

Select result is nothing ==”

Then i do a lot of testing regarding this date format
I try to direct parse in date_from and Date_to

where Convert(varchar(10), Date_Tm, 120) Between '2011-04-04' And '2011-04-04' 

I get what is expected

Anyway,what i wanted is get data by a range of date.I am crushing my head with this convert method ==”

Please help me.

Thanks:

PS:Please dont hesitate to voice out my mistake. we all learn from mistake and sorry for my poor english.

Regards:

LiangCk

  • 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-26T16:28:42+00:00Added an answer on May 26, 2026 at 4:28 pm

    It is because you are declaring your two variables as datetime instead of varchar(10). SQL is automatically converting it back to datetime because your variables are datetime.

    DECLARE @Date_From VARCHAR(10);
    DECLARE @Date_To VARCHAR(10);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an SQL Query that brings the result in below format ResourceName
I have written a SQL query that works just fine, but am having a
Hi I have some software written in VB6.0 that uses an SQL Server, but
Ok, I have an application written in C#. We have data in an SQL
I have a three tier system, SQL Server backend, hand written data access layer,
I have SQL data that looks like this: events id name capacity 1 Cooking
Scenario I have a stored procedure written in T-Sql that I use to insert
I have written a Windows service to collect information from all of our SQL
I have a number of manually written scripts (.sql) for tables, views and stored
Hai Techies, I have some stored procedure which was written in SQL server.Now 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.