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
 General SQL Server Forums
 New to SQL Server Programming
 EASY! Convert to date

Author  Topic 

matta0990
Starting Member

44 Posts

Posted - 2011-09-14 : 11:46:27
I want to convert this parameter to a date

Period_Id >=@Start_Date and Period_Id <=@End_Dates

The parameter is passed through as a string '01 sept 11' i need it to convert it to '20110901'

any help?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-14 : 11:50:04
thats ok. so far as Period_ID is of type datetime and your language settings is english it will work fine

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-09-14 : 13:09:52
If it is really SEPT, then you'll have to do a custom conversion. Even if SQL could implicitly convert that string to a date/datetime, implicit conversions are a bad idea.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-09-14 : 15:22:02
quote:
Originally posted by Lamprey

If it is really SEPT, then you'll have to do a custom conversion. Even if SQL could implicitly convert that string to a date/datetime, implicit conversions are a bad idea.



Bad Practice...there are no bad ideas I was told once...


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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-09-14 : 17:34:42
quote:
Originally posted by X002548

quote:
Originally posted by Lamprey

If it is really SEPT, then you'll have to do a custom conversion. Even if SQL could implicitly convert that string to a date/datetime, implicit conversions are a bad idea.



Bad Practice...there are no bad ideas I was told once...


Brett

Touche!
Go to Top of Page

matta0990
Starting Member

44 Posts

Posted - 2011-09-15 : 03:46:08
No, Period_Id is nvarchar.... I did not create the table and also not allowed to edit it as its part of a massive database. When i create my report it requires the parameter Period_Id, the user selects the relivent parameter from a drop down menu based on a stored proc from the database. The stored proc returns the last 50 Period_Id's and the user selects one which is then passes through the parameter. This should run my report, BUT as the stored Proc returns '01 SEP 11' my report needs to read it as '20110901'...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-15 : 04:03:04
then you've to use CONVERT() function to convert it to datetime

like

SELECT convert(datetime,Period_ID,106)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

matta0990
Starting Member

44 Posts

Posted - 2011-09-15 : 05:04:32
When i try and convert this to datetime i get a error...

Conversion failed when converting date and/or time from character string.?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-15 : 05:19:40
are you having value like

12 Sep 2011

ot
12 Sept 2011

ie. is it always DD MMM YYYY format?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

matta0990
Starting Member

44 Posts

Posted - 2011-09-15 : 05:26:41
Its '12 sep 2011' that’s what the stored proc returns.

So that is what is passed into my query as the parameter as '@start_date' for Period_Id... but the Period_Id needs to read it as '20110912' so i need a way of converting it in my query so that when '12 sep 2011' is selected by the users, the query converts it to '20110912' to pass into the parameter....
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-09-15 : 12:00:08
There are a couple options:
1. The first best option is to fix you database. (seriously)

2. You can convert the "DD MMM YYYY" string to a date, then convert that to a string to compare against the other string:
WHERE Period_Id >= CONVERT(NVARCHAR(8), CONVERT(DATE, @start_date, 106), 112)

3. You can convert the "DD MMM YYYY" sring to a date and convert teh Period_ID to a date and compare. But, this method will not be able to take advantage of any indexs, if any exist:
 WHERE CAST(Period_Id AS DATE) >= CONVERT(DATE, @start_date, 106)
Go to Top of Page
   

- Advertisement -