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
 General SQL Server Forums
 New to SQL Server Programming
 A date within a "string", to be converted

Author  Topic 

kiddoOnSQL
Starting Member

16 Posts

Posted - 2011-05-29 : 22:27:42
Hello

I get a string with a date in it from a text file in this format "ddmmyyyy". This is stored in a nvarchar column type in my SQL table.

I then have to pass this string to an existing function that calculates the week ending date from that date. This is that weekending function:

ALTER FUNCTION [dbo].[GetWeekEndingDate](@CurrentDate datetime)
RETURNS datetime
AS
BEGIN
Declare @NewDate as DateTime
set @NewDate = dateadd( day, ( 15 - (datepart( weekday, @CurrentDate ) + @@DateFirst )) % 7, @CurrentDate )
RETURN (Cast(Year(@Newdate) as varchar(4)) + '/' + Cast(Month(@Newdate) as VarChar(2)) + '/' + Cast(Day(@Newdate) as VarChar(2)))
END


GO

I DONT want to change the week ending function, however the week ending function seems to accept my string parameter only if I pass it as 20110331 or 31 Mar 2011.
How do I convert\format my original nvarchar type date string so that I can pass that to this function?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-05-30 : 02:01:53
You can get it using SUBSTRING().


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jfarrugia
Yak Posting Veteran

55 Posts

Posted - 2011-05-30 : 02:34:27
Hi,

Try using CONVERT with a 103 code, follow:

http://www.itexposed.com/forum/general-sql-queries/convert-date-sql/




Where software development knowledge meets the reader
Go to Top of Page
   

- Advertisement -