SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to convert nvarchar to date data type?


How to convert nvarchar to date data type?

Author
Message
aar.mba
aar.mba
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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
;
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91678 Visits: 38953
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.

Cool
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)
gbritton1
gbritton1
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1359 Visits: 879
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
@sqletl
@sqletl
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 131
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.
aar.mba
aar.mba
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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
@sqletl
@sqletl
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 131
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
MMartin1
MMartin1
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6993 Visits: 2033
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.

----------------------------------------------------
How to post forum questions to get the best help
aar.mba
aar.mba
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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).
aar.mba
aar.mba
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 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)
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40034 Visits: 20000
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search