Just a week ago my Database Team member asked me what is the best way to only select date part from datetime. When ran following command it also provide the time along with date.
SELECT GETDATE()
ResuleSet:
2007-06-10 7:00:56.107
The required outcome was only 2007/06/10.
I asked him to come up with solution by using date functions. The method he suggested was to use
SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))
I approved his method though, I finally suggested my method using function CONVERT.
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
The reason I use this because it is very convenient as well as provides quick support to convert the date in any format. The table which suggest many format are displayed on MSDN.
Some claims that using CONVERT is slower then using DATE functions but it is extremely negligible. I prefer to use CONVERT.
Reference : Pinal Dave (http://blog.SQLAuthority.com) , BOL




Thnx.
It helped me lots.
tks!
cheers mate!
Thanx dude!!! Good work:)
What a wonderful explanation…..
You must be a top noch programmer or a system solution developer……
Lots of love and thanks
How should I change the query to update the “LastModifiedDate” column in my table to a specific value?
HI.
I want to display the date in U.S. format.
Instead of Getdate() iam specifying directly the date.
I have tried using these syntaxes.. It is not changing to that format.
Kindly help
Select convert(varchar,’10/09/2007′,1) ,
Select convert(varchar,’10/09/2007′,101),
Select convert(varchar,’10/09/2007′,110)
SELECT CONVERT(VARCHAR(10),GETDATE(),104)
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
SELECT CONVERT(VARCHAR(10),GETDATE(),106)
SELECT CONVERT(VARCHAR(10),GETDATE(),107)
thanx ,
it helped me alot
thankyou for this function
The convert and string method has more overhead than you think if you need to use it in large rowsets.
Also, you still need another CAST to turn it back into datetime.
An alternate method one is to cast to float, FLOOR, and cast back to datetime.
Finally: the function usually is non deterministic if you use string to datetime (or vice versa) because the date format is culture and locale sensitive. CAST and CONVERT also mess up determistism.
Using the DATEADD(..DATEDIFF..) allows determinism, keeps all calculations within the datetime datatype, and guarantees complete locale insensitivity.
Determinism is important for indexing computed columns, or if the function is used in WHERE clause
[...] code review of Jr. DBA I found interesting syntax DATEFORMAT. This keywords is very less used as CONVERT and CAST can do much more than this command. It is still interesting to learn about learn about this new [...]
Nice Solution
It is Vey helpful for My duplicate Transcation entry where date is set as Index.
Thanks
Hello,
I am extracting year from date in oracle i. e. to_char(sysdate,’yyyy’) Now i want to change my database from oracle to SQL Server 2005. So the point is how to convert
to_char(sysdate,’yyyy’) from oracle to SQL Server 2005.
Plz help me out.
Thanking you,
Tushar
Hai Tushar,
You try this select datepart(yyyy,getdate())
rgds
Thnax Buddy Nice Examples its working a lot….
i personally feel u should add some more exapmles
for sql queries which screw the programming day to day
any ways thanx
Sarthak…
Gr8 work dude.. ppl face number of problems working with datetime…
Regards
Bharath T
10x…..
I want to convert varchar type date into datetime so i am getting the following error.
Solve this Problem….
“Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.”
Dear Sir/Madam,
Can we use CONVERT while index a date field?
But what if I need the date to be 2007-12-14, with hyphens! SQL Server only offers me 2007-12-14 00:00:00, I don’t need the time at all.
It helped me a lot.
Thanks
nice soln
I want to do date format conversion while retrieving date from database.
is it possible?
thks
Hey Thanks a lot.Its works.
GOOD
I m lloking for a sql server query which will display the report between two dates from the database where datetime field is defined.
Sachit
I m looking for a sql server query which will display the report between two dates from the database where datetime field is defined.
Sachit
Thanks
It works best for me
Actually from last 2 weeks i m searching this query
and finally i m very happy that i will now process my project
thanxs alot!!!
i’m working in Asp.net with backend of SQL Server.. i hav doubt for Compare Two Different Column in Table..Like ( Date_From and Date_To) i need to search for Date_From and Date_To when i entered Date_From TextBox and Date_To Textbox .. it ll compare Two Columns and retrive the Date to fill in Grid.. still i’m not able to Get the Concept .. how to Find this? if anybody pls rep as soon as possible..
thanks in advance
Sir,
if db have more than one field having DateTime datatype than how can one specify the particular field in a sql query ?
Thanks! Helped a lot!
Helped a lot!!
my query was as follows (viral this might help you)
SELECT * FROM MRO_MRSA where
AdmissionDate BETWEEN DATEADD(D, 0, DATEDIFF(D, 0, @dateFrom)) AND DATEADD(D, 0, DATEDIFF(D, 0, @dateTo))
Hii
Ur explanation helped me solve my problm
Thanks a lot
i want 2 disply date part from datatime datatype..
so wrote a query
“select convert(datetime, month_payroll, 102) as month_payroll from monthly_rates_swap”
& i get o/p as = “2008-04-29 13:44:17.937″
but i want o/p = ‘29/04/2008′
can any1 pla tell me tht how to convert datetime into simple date…coz in my company, datetime datatype is used for date…
n i want 2 dipsly only Date in SQL..not date & time…
i searched a lot on net..but i dnt get any ans…
plz help me.
thx in advance..
Hi Dave
Just very curious how to seperate Time from Datetime?what is the best way to store and retrieve time alone in a database like 03.00 am or 3.00 p.m ?
Best regards
Sanu
My problem is not reading the date but inserting it into the database in ASP. eg
mydate=”3/4/2008″
Insertqry=”Insert into mytable ( [birthday]) ) values ( ‘ & CDATE(mydate) & “‘)
results in 4/3/2008 going into the database.
session.LCID=3081 often solves this kind of problem, but not here.
(I can use parameters I believe, but I’m stubborn. There must be a way of doing it how I want !)
If i convert date using above method, it will convert into varchar data type, but further we can’t operate date function on it… what to do?
Hi Vrushali,
I hope you have already resolved your problem.
Anyway this is very simple in fact.e.g
“SELECT CONVERT(CHAR(11),GETDATE(),0)”
will give you a date like ‘May 27 2008′
You can experiment with different arguments for interesting results which suits your requirement.
Cheers
SELECT CONVERT(VARCHAR(10),GETDATE(),109)
Aslamo Alikom
I’ve one qeustion that i wnat to use “like” word instead “=” in the query that retrieve the Date…. thanks
Cheers
i need date in 05 jun 2008 format can u help me to resolve this
Select Convert(char(11),enddate,13)as date,* from exexamdate
I have date in varchar eg ‘20080602-13:47:08.603′
wnat to extract milliseconds only like 603,could you please tell me the way.
Hmm I Have A Problem:
I Have a Value Like this: Date: ‘04/05/2005′
The Problem:
1.
Select * FROM tblTest
where Date LIKE ‘%2005%’
Result: I Have 4 records
2. Select * FROM tblTest
where Date LIKE ‘%05_2005%’
Result: I Have 2 Records (‘04/05/2005′, ‘12/05/2005′)
3. Select * FROM tblTest
where Date LIKE ‘%04_05_2005%’
Or Date LIKE ‘04/05/2005′
Or Date LIKE ‘&04/05/2005&’
Result : 0 recods :S:S
WHere Is The Problem , Can u Help Me ?
Hi Pinal,
Your articles are very useful to all.
I am working in database backup utility.
I neet to select only from & to date data.
But tables has no column of date datatype.
Is SQL Server give the data to which date it is inserted into database.
Thanks In Advance
You rock. Finally a simple way to get all the records for Today. :-D
Your site is now in my favorites…….
hi,
thank u..its help me lot
Hi all,
How do I make this work? Thanks in advance
select count(*) from
where convert(modfd_date, varchar(10),110) =
select convert(mon_dt, varchar(10), 110) from (select case
When datepart(weekday, getdate()) = 2 Then
getdate()
else
dateadd( day, -datepart(weekday, getdate() -2), getdate() )
End as mon_dt) t
If I use convert function I get the error “‘varchar’ is not a recognized built-in function name.”
What is the best way to select data on a date column without using the time part. (other than the option stated above ie., DATEADD(D, 0, DATEDIFF(D, 0, GETDATE())) )
I have an index on the date column and would like to use it. Not sure if SQL Server will use an index if a function is applied to a indexed column.
Thank you for any help.
Excellent, elegant, easy. Many thanks.
Hi
If we give Month and Year, Can we get the Start Date and End Date of the Month for that particular year. is it possible?
Regards
Sachin
Hi,
I am using MSSQL server and I want to retrieve all records from [case] table where date_received is like ‘12-02-2008′ and for this I am using below listed query (1) but its not returning me any record since there are matching records present in table.
1. select (select convert(varchar, date_received, 110) as date from [case]) where date_received like ‘%12-02-2008%’
2. select (select convert(varchar, date_received, 110) as date from [case]) where date_received like ‘%2008%’
Query 1 does not return any record but 2 retuns me results.
Can anyone please help me out?
Regards,
Lavee
Respected Sir,
I am developing a Payroll software in VB6.0 and using SQL as backend.
I have a problem that i cant subtract the time from the datetime datatype
Please help me in subtracting datetime.
Regards,
Shashi Kant.
Select Convert(Varchar(15),GetDate(),108)
Hello Sir,
I need a query to retrive record from the table based on day alone,month alone,year alone.for eg if i run that query in the august i will get the record which and all created in that month.
hi i ha too mare solutions watch it in different formats
SELECT CONVERT(VARCHAR(10),GETDATE(),100)
SELECT CONVERT(VARCHAR(10),GETDATE(),101)
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
HI,
this is mahalakshmi. I am doing leave application using C#.net. In that iam checking existing dates are there or not. I have used below query
SELECT COUNT(LeaveApplicationID) AS Expr1
FROM LeaveApplication
WHERE (CONVERT(CHAR(10), FromDate, 101) BETWEEN ‘09/09/2008′ AND ‘09/13/2008′) AND (CONVERT(CHAR(10), ToDate, 101) BETWEEN ‘09/09/2008′ AND
‘09/13/2008′)
Fromdate is 09/12/2008 and todate is 09/18/2008.
but it’s giving count as 0.
Please solve my problem.
thanks it really worked
hiiiiiii
can anyone help me out with this problem am facing with ie
am not able to use between command in sql server
am getting errors
hi all
i have 6 columns with data in it but after giving this command
(select * from income where dt between ’sep 7 200′ and ’sep 9 2008′)
command is executed successfully but the output is
am getting only the names of the columns without data in it
please help me out
CHANGE THE DATE FORMAT AS ‘YYYY-MM-DD’
hi
i working on one project ie payrol system in that i hv to calculate employee present days automaticaly including late mark and all
plz help me hw i can calculat time diff
i insert time in database using date time picker
Hi,
How can i use this in a multiple row query
how can i get date in the format “dd/MM/yyyy” with time included init.i have done a lot of googling.but cant able to manage.
tried with
SELECT CONVERT(VARCHAR(26), GETDATE(), 130)——> 28 Apr 2006 12:39:32:429AM
SELECT CONVERT(VARCHAR(25), GETDATE(), 131)—–> 28/04/2006 12:39:32:429AM
but they are returning a garbage values
In view we use this contiction,we get record only datawise not time
i had use the following function to get only the date part from datetime data type.
set @data=CONVERT(varchar(8), @feedbackdate, 112)
it did work fine and returned the date part as 20081005
but while again using the procedure to insert the values
i.e.
Insert into tblfeedback (feedbackid,customerid,feedbackdate,feedbacktime,description) values(@maxUserId,@customerid,@data,@feedbacktime,@description)
again it gets changed into the same and displays the time too
please give me a solution for the same thankx
i required only time in select query from datetime field in format like:
2:20:55 AM.
if any body know then tell me.
[...] year ago I wrote post about SQL SERVER – Retrieve – Select Only Date Part From DateTime – Best Practice where I have discussed two different methods of getting datepart from [...]
DECLARE @ret_string varchar (255)
DECLARE @year as varchar(10)
DECLARE @Month as varchar(10)
DECLARE @Day as varchar(10)
set @year = cast(year(getdate()) as varchar)
set @Month = cast(month(getdate()) as varchar)
set @Day = cast(Day(getdate()) as varchar)
EXEC xp_sprintf @ret_string OUTPUT, ‘%s-%s-%s’, @year, @Month, @Day
PRINT @ret_string
Result will be like this
2008-10-26
Answer to the ” Vrushal” @ 34
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
SELECT CONVERT(VARCHAR(26), GETDATE(), 13)
27 Oct 2008 00:17:58:423
SELECT CONVERT(VARCHAR(26), GETDATE(),22)
10/27/08 12:25:10 AM
Select Convert(char(11),getdate(),8)
SELECT right(GetDate(),7)
Sir,Plz send me the query to retrieve only time from datetime column.
hai
ur Website Very UseFul For Me And Us
JAI HIND
How do I count on just the date portion of a datetime field?
tnx
select replace(convert(varchar(11), getdate(), 106), ‘ ‘, ‘/’) as date
result
———
date
12/Dec/2008
Testing for December 31st to get resultset of data for anything greater or equal to the 31st, but keep getting all of 2008 data.
Using:
select * from TABLE where enddate >= CONVERT(VARCHAR(10),’2008-12-31′,111)
Answer for @Athar, @Preeti
Query to retrieve only time from datetime column.
select convert(varchar(10), getdate(),108) –without millisecond
OR
select convert(varchar(10), getdate(),114) –with millisecond
Hi,
I want to retrieve only the date part from datetime from the SQL database. I am using
SELECT CONVERT(VARCHAR(10),GETDATE(),111) as sowDate
But it is giving me the current date :(
Plz help me.
I have a problem with dates that are entered in MS Access but are out-of-range like 01/02/830. Can u please help me with a query that can retrieve such dates for correction before I can import the data to SQL Server 2008.
Hi,
Thanks for your blog..
I have a problem in comparing 2 date fields one has the time field as (2009-01-16 12:32:50.690) and the other (2008-08-07 00:00:00.000).
I want to compare the date part of the 2 fields. I tried coverting them to varchar (101) and using the <= operator but the result set was incorrect.
I am not sure how else I can achieve this functionality of comparing the date part only.
thanx a lot….
@Ankit,
You need to use styles with convert.
Format of convert is
convert ( New Data Type, Column_Name or variable, Style)
select A.* , B.* from table_name A , table_name B
where convert ( varchar(10), A.Date_column_name , 101) = convert ( varchar(10), B.Date_column_name , 101)
Hope this helps,
IM
Thank you sir
This topic helped me a lot but can u tell me what is the relation of sequence of the number with date format
[...] SQL SERVER – Retrieve – Select Only Date Part From DateTime – Best Practice [...]
Hello Sir,
I have one date problem regarding importing XML Date to sql Server 2005.
I am passing DataTable to SP from .NET Application C#..
the columns with DateTime data type having
Default value as :
col.DefaultValue = DateTime.Today.Date;
and
string strXML = ds.GetXml();
and passing this strXML to SP.
My XML have tag like :
2009-02-28T00:00:00+05:30
SP is not accepting the date I pass above to datetime field of SQL server 2005.
can you plz. suggest wt could i do for this..
I tried for
select cast(2009-02-28T00:00:00+05:30′ as xml).value(‘xs:dateTime(.)’,
‘DATETIME’)
but it is giving NULL Value.
Thanks.
Reagerd
Hello Sir..
My above reading DataTime from XML get solve using above mentioned casting.
Due to my some mistake in Query I was getting NULL value.
Well, but now I am fasing new problem for this DateTime only of XML…
I have two tags in my XML datatype string which are
2009-01-01T00:00:00+05:30
2009-12-31T00:00:00+05:30
Actualy these dates are 01/01/2009 and 31/12/2009 respectively
But In OpenXML when I convert these dates it gives me different OUTPUT
(Here with I am showing you in select )
select
cast(‘2009-01-01T00:00:00+05:30′ as xml).value(‘xs:dateTime(.)’,'DATETIME’) as StartDate,
cast(‘2009-12-31T00:00:00+05:30′ as xml).value(‘xs:dateTime(.)’,'DATETIME’) as EndDate
OUTPUT is :
2008-12-31 18:30:00.000 2009-12-30 18:30:00.000
respectively ..
Can you please suggest Why this is..?
Regards
Namasthe Pinal
Could you tell me why my DB return is different?
select getdate()
3/3/2009 3:11:56 PM
I wish it was
3/3/2009 15:11:36
Date representation in the DB and Date formatting are two different things.
You take too simplistic approach.
For example, in a native date field, I can perform date arithmetic, but not in a varchar, or string field.
Hence please dont treat them as one and the same.
Oracle has a native date field – 01-01-2009 01:12:35
You can apply a “trunc” function to chop off all time, i.e.
you will get a native ‘01-01-2009′.
On both the above values, you can do arithmetic, i.e. add hours, days, or any time interval – you will get a new date as a result.
Both the above dates, trunc’ed one, and the full date,
can be formatted into a varchar or string.
Hence date presentation is different from date.
This is such a common trap programmers fall into.
Dear All,
I been trying to find an optimize way to fetch the data on datetime columns, and preferably i would go for dateadd method i.e. DATEADD(D, 0, DATEDIFF(D, 0, GETDATE())), the reason why i would choose this method over convert is because of my size of table which is almost like 2 millions rows and somehow my datetime column is a part of primary key. Also to note is, if my concern is only to get the date part then its better to use above mentioned method than using convert, as convert will change the datatype of column on the fly to string and its an overhead for the engine to do this step. Surely Pinal might have a better solution but i been using the dateadd for quite a while now and its going smooth.
Happy coding.
Regards- Rahman
@Rahman
If the date part gets queried a lot, it may be worthwhile to have a computed COLUMN in the TABLE that applies the FUNCTION for you. Then, INDEX that COLUMN.
hello sir,
column of a table contains data as
jan09
jul08
aug08
………… like this
how can i select only last two digits of that column
select right(column_name,2) from table_name;
is not retrieving any data
pls help me
hi siva
use select right(rtrim(column_name),2) from table_name
thanks a lot :)
hello,
i hav used datetimepicker control in my form.it stores date and time both in sql server database.
is there any way to only store time, not date in sql server database?also same for retrieval??
Thank You.
hello,
i hav used datetimepicker control in my form.it stores date and time both in sql server database.
is there any way to only store time, not date in sql server database?also same for retrieval??
Thank You in advance
@Alpesh,
You can use convert styles, to get different formats for date.
Please see a complete list of format styles here
http://msdn.microsoft.com/en-us/library/ms187928.aspx
I have used only 3 formats (108, 109, 114) just to show example:
– only Time ( 24 hours clock)
select convert ( varchar , getdate() , 108)
Output :
23:56:37
GO
– only Time with Micro seconds ( 24 hours clock)
select convert ( varchar, getdate(), 114)
Output :
23:56:48:810
GO
– only Time with Micro seconds ( 12 hours clock)
select substring ( convert ( varchar, getdate(), 109), 13, len ( convert ( varchar, getdate(), 109)) – 12)
Output:
11:57:19:840PM
~ IM.
@Imran
sorry tosay but I asked how to store only time in sql server database . I know how to extract only time/date from tht control…..Plz…How To STORE???
Hi All,
I have a table with two datetime fields, now I want to display both columns as date only.
tablename: sometable with two fields as
created | lastlogin
—————————————————————
2009-06-02 22:02:05.033 | 2009-06-02 22:03:34.503
2009-06-02 22:02:27.530 | 2009-06-02 22:03:50.047
now I want output as
created | lastlogin
————————————-
2009-06-02 | 2009-06-02
2009-06-02 | 2009-06-02
can anyone explain please, Thanks in advance
Sathish.
I have been through all the replies and got it..
SELECT CONVERT(VARCHAR(11),created,102) as created_date, CONVERT(VARCHAR(11),lastlogin,102) as last_date from sometable
Thanks,
Sathish
@Alpesh
declare the the field whatever you want to insert only time as varchar then
while inserting use this function on that field
convert ( varchar(20) , getdate() , 108)
ex:
here
uname as varchar(20)
lastlogin as datetime
created as datetime
query:
insert into table_name (uname, lastlogin, created) values (convert ( varchar(20) , getdate() , 108), getdate(), getdate())
if yours is different case i’m sorry.
Thanks,
Sathish.
I found the soultion. Thanks
I developed the solution for date only from ‘getdate()’ function. Also this worked for me very effectivelyy. Because I does not required to change the data type in the database or not required to write down different function for each time or not required to execute the same function for so many times.
I use this method.
1) Keep data type of column as ‘datetime’.
2)Keep its default value to ‘getdate()’.
3) Write trigger on that table as
CREATE TRIGGER [TRIGGER NAME] ON [dbo].[TABLE NAME]
FOR INSERT, UPDATE, DELETE
AS
UPDATE [TABLE NAME] SET [COLUMN NAME] = CONVERT(datetime,CONVERT(varchar(20),[COLUMN NAME], 111), 111)
It works very effectively.
Answer to Seema :
To extract milliseconds from current date
SELECT(DATEpART(ms,getdate()))
We can also select date using
SELECT LEFT(GETDATE(),11)
FWIW…
Many people prefer the DATEADD method rather than the CONVERT method because they are trying to compare two dates rather than output a date…
If you use the CONVER method, once you get the string with just the date in it, you have to convert it back to a datetime to do any comparisons with it other than =, and two converts is more expensive than a couple of system function calls in a non-negligible way…
Your team member’s solution was better.
I have a problem of trying to find dates in my table which does not follow the following formats. Given are the valid formats:
1. 10/1/2007
2. Thu Sep 20 06:32:10 GMT-0400 (EDT) 2007
3. 10/1/2007 5:00
Can anybody please help me regarding this?
Thanks in advance.
It was a great help! Thanks!
Thank you so much, but is it possible to display just time?
please help me i want to get data between two dates im writing
select * from Test_Date where t_Date>=convert(datetime,’02/02/2009′,103) and t_Date<=convert(datetime,'02/02/2010',103)
can not get the correct data
please help me i want to get data between two dates im writing ,these query act normaly but not correct data
select * from Test_Date where t_Date>=convert(datetime,’02/02/2009′,103) and t_Date<=convert(datetime,'02/02/2010',103)
can not get the correct data
and the datatype of column t_Date is varchar(50)
Hi Rabin,
select *
from Test_Date
where CAST(t_Date AS DATETIME)>=convert(datetime,’02/02/2009′,103) and CAST(t_Date AS DATETIME)<=convert(datetime,'02/02/2010',103)
let me know if it helps you.
Tejas
I have a datetime column in database in ‘9/29/2009 1:00:10 AM’ this format. Based on this coulmn value i need to get other column value but datetime value would be in ‘9/28/2009′ format.
How I can achive following query
select * from tblFirst where Startdate=’9/29/2009′
but in Database Startdate column has value as ‘9/29/2009 1:00:10 AM’
Could you help me?
Thanks in advance
Hi Mrs Tejas Shah thank for you
ok your code is correct but actuall not get
the specific result because if we put from date
‘01/03/2009′ to date ‘01/01/2010′ we cannot get the
record has date ‘02/03/2009′ why ?
Notes: i’m save date in database as dd/mm/yyyy
date column in database has datatype varchar can you help in this code or another way to get the good result.
Thank again for you
@Rabih,
Check if this could help.
Create Table #Example ( Eid int, EmpName varchar(50), DateJoined varchar(50))
Insert into #Example values ( 1, ‘Bill Gates’ , ‘01/01/2009′)
Insert into #Example values ( 2, ‘Gustavo’ , ‘01/02/2009′)
Insert into #Example values ( 3, ‘Catherine’ , ‘01/03/2009′)
Insert into #Example values ( 4, ‘Kim’ , ‘01/04/2009′)
Insert into #Example values ( 5, ‘Humberto’ , ‘01/06/2009′)
Insert into #Example values ( 6, ‘Pilar’ , ‘01/08/2009′)
Insert into #Example values ( 7, ‘Frances’ , ‘01/10/2009′)
Insert into #Example values ( 8, ‘Margaret’ , ‘01/12/2009′)
Insert into #Example values ( 9, ‘Carla’ , ‘01/01/2010′)
Insert into #Example values ( 10, ‘Jay’ , ‘01/02/2010′)
Insert into #Example values ( 11, ‘Ronald’ , ‘01/03/2010′)
select * from #Example
select *
from #Example
where Convert (Datetime, DateJoined) between ‘01/03/2009′ and ‘01/01/2010′
Drop table #Example
~ IM.
@W.Ah
Check if this could help.
Create Table #Example ( Eid Int, Ename varchar(40), JoinDate datetime )
insert into #Example Values ( 1, ‘Imran’ , ‘9/29/2009 1:00:10 AM’)
Select * from #Example
Select Eid, Ename from #Example
Where convert (datetime, convert ( varchar, JoinDate, 101 )) = ‘9/29/2009′
Drop table #Example
~ IM.
Hi Mr Imran Mohammed
thank for this code but not actuall get the result please i need all records between two dates your code not gell all records.
In my table
date column in my table is varchar and i’m put date in my application like dd/mm/yyyy and i’m writing this code
select * from Test_Date where t_Date>=convert(datetime,’01/03/2009′,103) and t_Date<=convert(datetime,'02/02/2010',103)
not get the record has date '02/03/2009′ try this code and see the result please help
thanks again
Hello Rabih,
r u getting follwoing error :
“The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
while using ”
select * from Test_Date where t_Date>=convert(datetime,’01/03/2009′,103) and t_Date<=convert(datetime,'02/02/2010',103)
statement ??
Adi
select * from #TempTable Where convert(numeric,Convert(varchar,cast((left(right(t_Date,7),2) +’/’ + left(t_Date,2) + ‘/’ + right(t_Date,4)) as datetime),112)) >=convert(numeric,Convert(varchar,convert(datetime,’01/03/2009′,103),112)) and
convert(numeric,Convert(varchar,cast((left(right(t_Date,7),2) +’/’ + left(t_Date,2) + ‘/’ + right(t_Date,4)) as datetime),112)) = and <= condition
Hope it will give u all the results u r expecting.
Do reply.
thanks.
Regards
Adi
Rabih
it seems my select statement is not comming compleately
I did following steps.
Flow is like this :
1) arrange ur string dd/MM/yyyy to MM/dd/yyyy
2) then convert it to Datatime
3) then convert that datetime to numeric format using 112
4) Now convert ur From date and ToDate of conditon to datetime 103
5) then convert those From date and toDate to numeric format
6) and then compare using >= and <= condition
Hir Mrs Adi
I’m very very very thank for you
you give me the good solution good work for you.
Bye
if you want send to me your Email Address.
Bye again
Your posts always helps me lot …thanx
Can Datepart be used as part of update…set query. I am trying to update a datefield by the following statement
Update tablexx
Set Datepart(yyyy, Datefld) = 2011.
But I am getting errors.
Thanks,
Ash