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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Convert date??

Author  Topic 

Goalie35
Yak Posting Veteran

81 Posts

Posted - 2005-10-20 : 14:39:17
I have a varchar date in my stored procedure that comes in as "mm/yyyy" but I need to convert it to a datetime of "mm/dd/yyyy" with "dd" being the 1st of the month before I place it into the database.

So for example, I bring in "10/2005" as a varchar and need to convert it to a datetime of "10/1/2005"

Is there a sql conversion that can do this?

Thanks in advance.

-Goalie35

X002548
Not Just a Number

15586 Posts

Posted - 2005-10-20 : 14:45:29
[code]
DECLARE @d varchar(7)
SELECT @d = '10/2005'
SELECT CONVERT(datetime,SUBSTRING(@d,1,CHARINDEX('/',@d))+'01/'+RIGHT(@d,4))

[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-10-20 : 14:54:46
SET DATEFORMAT DMY
DECLARE @d varchar(7)
SELECT @d = '10/2005'
SELECT CONVERT(datetime, '01/' + @d)

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-21 : 01:30:38
>>I have a varchar date in my stored procedure that comes in as "mm/yyyy"

Hereafter use proper DateTime datatype and pass VALID date to avoid unnecessary conversions

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-10-21 : 09:33:38
quote:
Originally posted by Kristen

SET DATEFORMAT DMY
DECLARE @d varchar(7)
SELECT @d = '10/2005'
SELECT CONVERT(datetime, '01/' + @d)

Kristen



I hate messing with settings.....how can you tell what the current setting for dateformat is? Database propoerties?




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-21 : 10:23:21
>>how can you tell what the current setting for dateformat is? Database propoerties?

DBCC USEROPTIONS


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -