Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to convert nvarchar to date data type? Expand / Collapse
Author
Message
Posted Thursday, April 17, 2014 5:56 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:13 AM
Points: 26, Visits: 41
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
;
Post #1562618
Posted Thursday, April 17, 2014 6:21 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:57 PM
Points: 23,009, Visits: 31,508
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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1562623
Posted Thursday, April 17, 2014 6:34 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 268, Visits: 600
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
Post #1562628
Posted Thursday, April 17, 2014 8:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 10:14 AM
Points: 38, Visits: 41
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.
Post #1562687
Posted Sunday, April 20, 2014 11:15 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:13 AM
Points: 26, Visits: 41
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
Post #1563387
Posted Monday, April 21, 2014 7:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 10:14 AM
Points: 38, Visits: 41
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

Post #1563482
Posted Tuesday, April 22, 2014 9:58 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 1:03 PM
Points: 312, Visits: 817
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.
Post #1563936
Posted Tuesday, April 22, 2014 11:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:13 AM
Points: 26, Visits: 41
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).
Post #1564092
Posted Tuesday, April 22, 2014 11:32 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:13 AM
Points: 26, Visits: 41
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)
Post #1564095
Posted Wednesday, April 23, 2014 2:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:56 AM
Points: 7,120, Visits: 13,491
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1564141
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse