Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Time Truncation

Author  Topic 

renu
Starting Member

47 Posts

Posted - 2008-08-19 : 05:08:57
Hi all,
I have a table with column c1 varchar

I have both text data and datetime data in that column.

Now how can i truncate time from date without affecting other fiels

for eg

c1
--------
a
d
'01/01/1990 23:09:56'
r
t


from the above table
i need the output like
c1
---
a
d
'01/01/1990'
r
t

how can i acheive this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-19 : 05:17:10
[code]SELECT CASE WHEN ISDATE(c1)=1
AND LEN(c1) >10 THEN LEFT(c1,CHARINDEX(' ',c1)-1)
ELSE c1 END
FROm YourTable[/code]
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-08-19 : 05:29:32
try this

declare @t table ( c1 varchar(100))

insert into @t
select 'a' union all
select 'd' union all
select '01/01/1990 23:09:56' union all
select 'r' union all
select 't'

update @t
set c1 = convert(varchar(10), dateadd(day, datediff(day, 0, c1), 0), 101)
where isdate(c1) = 1

selecT * from @t
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-19 : 06:01:44
quote:
Originally posted by PeterNeo

try this

declare @t table ( c1 varchar(100))

insert into @t
select 'a' union all
select 'd' union all
select '01/01/1990 23:09:56' union all
select 'r' union all
select 't'

update @t
set c1 = convert(varchar(10), dateadd(day, datediff(day, 0, c1), 0), 101)
where isdate(c1) = 1

selecT * from @t



ISDATE() is not reliable
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/09/24/handle-isdate-with-care.aspx

See what happens if the one of the values is 2000?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-19 : 06:04:37
Also see whay visakh used LEN function too

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-19 : 06:13:15
quote:
Originally posted by madhivanan

Also see whay visakh used LEN function too

Madhivanan

Failing to plan is Planning to fail


Courtesy Madhi ofcourse for that
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-19 : 08:26:15
quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

Also see whay visakh used LEN function too

Madhivanan

Failing to plan is Planning to fail


Courtesy Madhi ofcourse for that




Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -