July 2, 2009 at 3:54 pm
I need to pull the date only from a text field that contains the date and time in it. I tried using convert but it hasen't worked, so I tried to break it in sections as a new field but I can't get the year part right. Below is an example of what the value in the text field looks like and then what I'm trying to do.
7/2/2009 4:18:25 PM
7/2/2009 4:14:28 PM
selectCallLogID,
left(Date, charindex('/', Date)-1) + '/' +
substring(Date,charindex('/',Date)+1,charindex('/',Date)-1)
From#tmpDateConv
Gives me result: 12/26 which is the month/date; but I can't the year part down.
Any help is appreciated.
thx,
John
July 2, 2009 at 3:57 pm
What is the exact result you want and is the source date a datetime column?
July 2, 2009 at 4:00 pm
I want it as 7/2/2009.
the source field is a text field and the value is stored as 7/2/2009 11:07:23 AM
July 2, 2009 at 5:03 pm
latingntlman (7/2/2009)
I need to pull the date only from a text field that contains the date and time in it. I tried using convert but it hasen't worked, so I tried to break it in sections as a new field but I can't get the year part right. Below is an example of what the value in the text field looks like and then what I'm trying to do.7/2/2009 4:18:25 PM
7/2/2009 4:14:28 PM
selectCallLogID,
left(Date, charindex('/', Date)-1) + '/' +
substring(Date,charindex('/',Date)+1,charindex('/',Date)-1)
From#tmpDateConv
Gives me result: 12/26 which is the month/date; but I can't the year part down.
Any help is appreciated.
thx,
John
So close...
select CallLogID,
left(Date, CharIndex(' ', Date)-1)
from #tmpDateConv
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
July 2, 2009 at 5:13 pm
[font="Verdana"]There are various options.
If you just want it as a string, you can use this (change the number on the end of the convert to vary the format):
select convert(varchar(10), getdate(), 120); -- ODBC format: yyyy-mm-dd
select convert(varchar(10), getdate(), 101); -- US format: mm/dd/yyyy
If you want a datetime value without the time component, use:
select dateadd(day, datediff(day, 0, getdate()), 0);
You're starting with a string that is in US date format (by the looks), so I'd combine them to get something like the following:
select dateadd(day, datediff(day, 0, convert(datetime, [MyDateTimeField], 101)), 0) as [MyDateField]
from dbo.MyTable;
[/font]
One word of advice: don't rely on implicit conversions between strings and dates. Your code will break if it moves between different default date formats.
July 3, 2009 at 6:38 am
Thanks guys,
Bruce, your suggestions "convert(varchar" won't work because it's a text field, so the field needs to be parsed to remove the spaces and time after the date. Then I can convert it.
Regards,
John
July 3, 2009 at 6:56 am
This should give you the idea
DECLARE @DateInString varchar(20)
SET @DateInString = '7/2/2009 4:18:25 PM'
SELECT CAST(@DateInString AS DATETIME)
SELECT DATEADD(dd,datediff(dd,0,@DateInString),0)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 3, 2009 at 6:57 am
latingntlman (7/3/2009)
Thanks guys,Bruce, your suggestions "convert(varchar" won't work because it's a text field, so the field needs to be parsed to remove the spaces and time after the date. Then I can convert it.
Bruce's last query takes that into account. Did you try it?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 3, 2009 at 8:17 am
Yeah, I tried Bruce's but it gave me a syntax error. Anyway, I went with Wayne's suggestion and it seems pretty simpler.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply