How to convert nvarchar to date data type?

  • I have to convert nvarchar to date data type. and fetch the week number of that particular month from the column

    Pls find the sample data

    CREATE TABLE #mytable

    (

    current_week nvarchar(30)

    )

    insert into (current_week)

    select '2012-02-24 ~~ 2012-03-01' union all

    select '2012-01-20 ~~ 2012-01-26' union all

    select '2012-01-20 ~~ 2012-01-26 ' union all

    select '2012-01-20 ~~ 2012-01-26 ' union all

    select '2012-01-27 ~~ 2012-02-02' union all

    select '2012-02-03 ~~ 2012-02-09' union all

    select '2012-02-17 ~~ 2012-02-23' union all

    select '2012-02-17 ~~ 2012-02-23' union all

    ;

  • Well, you provided DDL and sample data, but I'm not quite sure what you expect as output. Please provide expected results based on the sample data you provided.

  • Your INSERT query doesn't work. I fixed it like this:

    insert into #mytable(current_week)

    select '2012-02-24 ~~ 2012-03-01' union all

    select '2012-01-20 ~~ 2012-01-26' union all

    select '2012-01-20 ~~ 2012-01-26 ' union all

    select '2012-01-20 ~~ 2012-01-26 ' union all

    select '2012-01-27 ~~ 2012-02-02' union all

    select '2012-02-03 ~~ 2012-02-09' union all

    select '2012-02-17 ~~ 2012-02-23' union all

    select '2012-02-17 ~~ 2012-02-23'

    Then, taking the simplest approach, this query gets the two dates out as dates:

    select

    cast(left(current_Week, 10) as date) as 'fromdate'

    , cast(right(rtrim(current_Week), 10) as date) as 'todate'

    from #mytable

    However, the simplest approach may not be the right approach if your data is more variable. In that case you'd want to parse the rows with charindex or patindex functions and use substring to pull out the date fields before casting them to date or datetime

  • Select cast(DATEPART(week, cast(Left(Ltrim(current_week), 10)as date)) as nvarchar) +'~~'+cast(DATEPART(week, cast(right(Rtrim(current_week), 10)as date))as nvarchar) as [Week Number] from #mytable

    Week Number

    8~~9

    3~~4

    3~~4

    3~~4

    4~~5

    5~~6

    7~~8

    7~~8

    This will show on which week the begin and end dates will fall in that calendar year. If you just need to find the week that that falls in the range of dates you provided then it will be necessary to know on what date your week actually starts. LTRIM and RTRIM will not actually be necessary if your sample did not contain SPACE. Hope this helps.

  • Thanks for ur reply.

    I want current_week column before fifteen days from now.

    That is, getdate() - 15 days = current_week. But this column is not in date data type

    so I used query mentioned by you.. but its throwing error.

    select case when dateadd(DATEPART(day, cast(Left(Ltrim(current_week), 10)as date)),-15,getdate()) then 'previous week' end

    from mytable

  • If you need to get the current_week that is 15 days before today's date then you can use this statement:

    Select current_week from #mytable

    where GETDATE()-15 between cast(Left(Ltrim(current_week), 10)as date) and cast(right(Rtrim(current_week), 10)as date)

    To get a result back from your sample I have used (2012-01-29) 813 days before today's date. The date falls between the begin and end date of your current_week range

    Select current_week from #mytable

    where GETDATE()-813 between cast(Left(Ltrim(current_week), 10)as date) and cast(right(Rtrim(current_week), 10)as date)

    current_week

    2012-01-27 ~~ 2012-02-02

  • Do you need to get the week start and end dates, or the week number in the year? Have you considered the use of a calendar table? Keep in mind that when storing dates as strings, you will likely have to convert back to date to do these types of calculations. It is better to store dates in date format. But I realize you may not have had much choice in the matter.

    ----------------------------------------------------

  • I want use case when statement where if the getdate is less than 15 days then fetch

    'currentweek('the week's flag'.

    eg : currentweek(2013-04-01 ~~ 2013-04-07).

  • Also if getdate() - 8 days then Next week('2013-04-01 ~~ 2013-04-07)'

    All the columns come from single table. But the values need to be fetched like next

    week and currentweek.

    So two conditions :if getdate()-15 days then currentweek( 'the week flag')

    if getdate() - 8 days then nextweek('the week flag')

    eg : currentweek(2013-04-01 ~~ 2013-04-07)

    nextweek(2013-04-08 ~~ 2013 -04 - 15)

  • aar.mba (4/17/2014)


    I have to convert nvarchar to date data type. and fetch the week number of that particular month from the column

    Pls find the sample data

    CREATE TABLE #mytable

    (

    current_week nvarchar(30)

    )

    insert into (current_week)

    select '2012-02-24 ~~ 2012-03-01' union all

    select '2012-01-20 ~~ 2012-01-26' union all

    select '2012-01-20 ~~ 2012-01-26 ' union all

    select '2012-01-20 ~~ 2012-01-26 ' union all

    select '2012-01-27 ~~ 2012-02-02' union all

    select '2012-02-03 ~~ 2012-02-09' union all

    select '2012-02-17 ~~ 2012-02-23' union all

    select '2012-02-17 ~~ 2012-02-23' union all

    ;

    This table bears the hallmarks of an absolute beginner and will forever be a pig to use. Can you create and use your own table instead of this silly rubbish?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Company wont use calendar table and I'm not authorized too.

  • ;WITH c (current_week,fromdate,todate) AS (

    SELECT current_week,CAST(LEFT(current_week,10) as date),CAST(RIGHT(current_week,10) as date)

    FROM #mytable

    )

    SELECT DISTINCT c.current_week,n.current_week AS [next_week]

    FROM c

    JOIN c n ON DATEADD(day,-8,SYSDATETIME()) BETWEEN n.fromdate and n.todate

    WHERE DATEADD(day,-15,SYSDATETIME()) BETWEEN c.fromdate and c.todate

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply