September 18, 2009 at 1:26 am
Hi,
I need to change the format of a date from '2009-09-18 12:54:59.860'
to '2009-09-18 00:00:00.000' (i.e. I want to keep the time format but with 00 values)
How is it possible?
Thanks in advance.
September 18, 2009 at 2:10 am
September 18, 2009 at 3:48 am
Dave Ballantyne (9/18/2009)
Try this linkhttp://www.sqlservercentral.com/blogs/lynnpettis/archive/2009/03/25/some-common-date-routines.aspx
Hi Dave,
It works fine. Even I have used the following code
declare @start_date varchar(23)
select @start_date = REPLACE((CONVERT (varchar(10), '2008-10-09 00:00:00.000',112) + ' 00:00:00.000'),'-','/')
print @start_date
But I feel ur the code described in the link is better than this.
Anyway,
I have another question.
If my datetime contains time part, the convert function is giving some error.
Like Fiscal_year (id int, start_date datetime, end_date datetime)
SELECT * FROM Fiscal_Year
WHERE CONVERT (varchar(10),'2009-09-01 00:00:00.000', 112)
BETWEEN CONVERT(varchar(10), start_date,112) AND CONVERT(varchar(10),(end_date - 1),112)
SELECT * FROM Fiscal_Year
WHERE '2009-09-01 00:00:00.000'
BETWEEN start_date AND (end_date - 1)
This two query gives different results. The second one is giving proper result though ideally both should return same result. Could you tell me what is the problem with CONVERT?
September 18, 2009 at 3:57 am
When you use the CONVERT function try to not determine the number of characters during conversations
try like this CONVERT(VARCHAR, DATE_FIELD, format)!
September 18, 2009 at 4:08 am
Dugi (9/18/2009)
When you use the CONVERT function try to not determine the number of characters during conversationstry like this CONVERT(VARCHAR, DATE_FIELD, format)!
Hi Dugi,
There is no difference if we are not mentioning the number of characters
What 2 do?
September 18, 2009 at 4:59 am
Hmm wait a min;
SELECT * FROM Fiscal_Year
WHERE CONVERT (varchar(10),'2009-09-01 00:00:00.000', 112)
BETWEEN CONVERT(varchar(10), start_date,112) AND CONVERT(varchar(10),(end_date - 1),112)
SELECT * FROM Fiscal_Year
WHERE '2009-09-01 00:00:00.000'
BETWEEN start_date AND (end_date - 1)
'2009-09-01 00:00:00.000' - this must be column name no data, couz you are using in WHERE CLAUSE!
September 18, 2009 at 5:12 am
Dugi (9/18/2009)
Hmm wait a min;SELECT * FROM Fiscal_Year
WHERE CONVERT (varchar(10),'2009-09-01 00:00:00.000', 112)
BETWEEN CONVERT(varchar(10), start_date,112) AND CONVERT(varchar(10),(end_date - 1),112)
SELECT * FROM Fiscal_Year
WHERE '2009-09-01 00:00:00.000'
BETWEEN start_date AND (end_date - 1)
'2009-09-01 00:00:00.000' - this must be column name no data, couz you are using in WHERE CLAUSE!
No, actually thats a variable, start_date and end_date are the column names
September 18, 2009 at 5:19 am
Dugi (9/18/2009)
When you use the CONVERT function try to not determine the number of characters during conversationstry like this CONVERT(VARCHAR, DATE_FIELD, format)!
I''m digressing from the OP's date format conversion question, but in general I disagree with this advice. If you execute the following TSQL, you will find that the varchar is implicitly declared with a max length of 30 characters and so truncates the 50-character string stored in the local variable @S.
DECLARE @s-2 char(50)
SELECT @s-2 = '12345678901234567890123456789012345678901234567890'
SELECT CONVERT(varchar, @s-2), LEN(CONVERT(varchar, @s-2))
I think it is better practice to declare explicitly the length of all varchar and char data types to help avoid the possibility of "unexpected" string truncations.
September 18, 2009 at 6:01 am
Hmmm working with dates it works perfect so if we discus for any other converting you are right and I know that thing!
For more info about converting use this link
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx
September 18, 2009 at 10:22 am
i think this is what you want..
select convert(datetime,convert(varchar,getdate(),112))
and you will get this...
-----------------------
2009-09-18 00:00:00.000
(1 row(s) affected)
September 21, 2009 at 11:24 pm
Fausto Echevarria (9/18/2009)
i think this is what you want..select convert(datetime,convert(varchar,getdate(),112))
and you will get this...
-----------------------
2009-09-18 00:00:00.000
(1 row(s) affected)
Hi Fausto,
That is exactly my wantsome. Thanks a ton.
September 21, 2009 at 11:27 pm
Fausto Echevarria (9/18/2009)
i think this is what you want..select convert(datetime,convert(varchar,getdate(),112))
and you will get this...
-----------------------
2009-09-18 00:00:00.000
(1 row(s) affected)
Hi Fausto,
That is exactly my wantsome. Thanks a ton.
September 23, 2009 at 6:42 am
Hi Edward,
Another good way. Thanks.
But i feel
select convert(datetime,convert(varchar,getdate(),112))
is better and easier to understand.
But whtz abt performance?:w00t:
September 23, 2009 at 3:18 pm
i see no problem with performance... but im not quite good at tuning and performance
i saw the execution plan and Client Statistics of these two queries
select MY_DateTime_Field from My_table
select convert(datetime,convert(varchar,MY_DateTime_Field,112)) from My_table
and i couldnt see any weird....
but im not sure if the execution plan and client statistics give me a good way to know if theres a poor performance or not.
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply