0 pts.
 T-sql date question
SQL
Does anyone know how I can convert 2005-09-14 09:49:13.873 to 2005-09-14 00:00:00.000 in a query?

Software/Hardware used:
ASKED: September 14, 2005  3:03 PM
UPDATED: September 13, 2008  10:50 PM

Answer Wiki:
Try function: CONVERT ( data_type [ ( length) ] , expression [ , style ] ) see: http://www.databasejournal.com/features/mssql/article.php/2197931
Last Wiki Answer Submitted:  September 15, 2005  3:40 am  by  Servee   0 pts.
All Answer Wiki Contributors:  Servee   0 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

try this.
sql server 2000

/* Creating a Test Table */
Create Table MyDateTest99
(
DateColumn smalldatetime
)
go

/* Inserting the test value into the table */
/*insert into MyDateTest99 values (’2005-09-15 00:00:00′) */
insert into MyDateTest99 select convert (varchar(10),’2005-09-15 01:03:22′,120)

go

/* Selecting the result */
select DateColumn from MyDateTest99
go

/* Performing Cleanup */
drop table MyDateTest99
go

 0 pts.

 

At first I went for the same solution as first mentioned by servee, but for some reason, my SQL Server did not return the needed.

I doubt, that the solution by chicotellez would work, unless your database runs certain languages, that by default removes the hh.mm from the smalldatetime.
- hh.mm is normally part of the smalldatetime!
Besides – creating an entire new table just to solve this problem seems a bit like shooting birds with canons!

Since the solution by servee didn’t work for me, I worked out this solution in stead… Not an optimal solution at all – I admit to that – but it works:

declare @date as datetime, @strDate as varchar(10)
set @date = getdate()
set @strDate = cast(day(@date) as varchar) +’-'+ cast(month(@date) as varchar) +’-'+ cast(year(@date) as varchar)
select cast(@strDate as datetime)

Hope someone has a better solution! (?)

Good luck
Jacob

 0 pts.

 

Use the convert function.

select convert(varchar(10), '2005-09-14 09:49:13.873', 101)
 64,520 pts.