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


varchar to datetime convertion challenge


varchar to datetime convertion challenge

Author
Message
latingntlman
latingntlman
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 521
I have a table w/a varchar(10) column (MoYear) which has the month and year with '/' between (i.e. 01/2013). My task is to insert '01' as the date surrounded by '/' (i.e. 01/01/2013). Finally I need to convert it to datetime (i.e. 2013-01-01 00:00:00.000) but I keep getting result 'Jan 1 2013 12:00AM'.

I've tried different convert options but still wrong result. So here's my workaround:

1 - ALTER MoYear field to varchar(30)
2 - INSERT the day of the month (01) into field by using UPDATE as:
UPDATE Table
Set MoYear = Substring(MoYear,1,2) + '/01/' + Substring(MoYear,4,4)
3 - Update field MoYear as datetime as:
UPDATE Table
SET MoYear = convert(datetime,convert(varchar,MoYear,101))

So, as a workaround I had to add a datetime field (MoYear2 = cast(null as datetime)) to the table. Then I updated this field with the converted value from field MoYear as:

UPDATE Table
Set MoYear2 = convert(datetime,convert(varchar,MoYear,101))

Then I get the desired result 2013-01-01 00:00:00.000

My question is, am I taking too many steps in achieving the desired result, or is this the best option?

Regards,

John
Lynn Pettis
Lynn Pettis
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: 40022 Visits: 38566
You really are doing too much. You can create a computed column using the convert part shown below, just replace the variable @TestDate with your column name :



declare @TestDate varchar(10) = '01/2013';
select @TestDate, convert(datetime, stuff(@TestDate,3,0,'/01'), 101);




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)
latingntlman
latingntlman
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 521
I just tried your suggestion as:

update table
set MoYear = convert(datetime, stuff(MoYear,3,0,'/01'), 101)

When I select the table the values are displaying as Dec 1 2012 12:00AM; Aug 1 2012 12:00AM, etc.
Lynn Pettis
Lynn Pettis
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: 40022 Visits: 38566
latingntlman (4/30/2013)
I just tried your suggestion as:

update table
set MoYear = convert(datetime, stuff(MoYear,3,0,'/01'), 101)

When I select the table the values are displaying as Dec 1 2012 12:00AM; Aug 1 2012 12:00AM, etc.


No, you are trying to update a varchar column when I said you could add a computed column to your table using the convert statement to convert the value in MoYear to a datetime value.

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)
Lynn Pettis
Lynn Pettis
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: 40022 Visits: 38566
If you run the following, you will see that the datetime value is implicitly converted to the character string you showed:



declare @TestDate varchar(10) = '01/2013';
select
@TestDate,
stuff(@testDate,3,0,'/01'),
convert(datetime, stuff(@testDate,3,0,'/01'), 101),
cast(convert(datetime, stuff(@testDate,3,0,'/01'), 101) as varchar);




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)
Lynn Pettis
Lynn Pettis
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: 40022 Visits: 38566
If you want a character version (or at least close) of the value shown as datetime, look at the last convert in the following:



declare @TestDate varchar(10) = '01/2013';
select
@TestDate,
stuff(@testDate,3,0,'/01'),
convert(datetime, stuff(@testDate,3,0,'/01'), 101),
cast(convert(datetime, stuff(@testDate,3,0,'/01'), 101) as varchar),
convert(varchar(30), convert(datetime, stuff(@testDate,3,0,'/01'), 101), 120);




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)
Lynn Pettis
Lynn Pettis
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: 40022 Visits: 38566
By the way, if you are converting MoYear to a datetime value, you really should store it in a datetime column, not back as a character string.

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)
latingntlman
latingntlman
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 521
You're right, although, I was hoping for an alternative w/o creating another column. Unfortunately, the data type for the field is varchar(10) and the values are 'mm/yyyy' on the table, so it looks like creating the add'l column as datetime is the safer option.
Sergiy
Sergiy
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10554 Visits: 11964
Computed column would be a good option for you.
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