SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date function help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 03/18/2014 :  12:38:03  Show Profile  Reply with Quote
Hi all
I have the following
Column Name : [Converted Date]
Data Type : varchar(50)
When I try and do month around the [Converted Date] I get the following error message “Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.”

My Query is
SELECT
month([Created Date])
FROM [FDMS_PartnerReporting].[Staging].[Salesforce_MarketingReporting]

Looking forward to your help

Regards
D

tkizer
Almighty SQL Goddess

USA
37133 Posts

Posted - 03/18/2014 :  12:55:30  Show Profile  Visit tkizer's Homepage  Reply with Quote
Sounds like you've got data in that column that can't be converted to date/time. Let's what these show:

SELECT CAST([Created Date] AS datetime)
FROM [FDMS_PartnerReporting].[Staging].[Salesforce_MarketingReporting]

SELECT [Created Date]
FROM [FDMS_PartnerReporting].[Staging].[Salesforce_MarketingReporting]
WHERE ISDATE([Created Date]) = 0

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/18/2014 :  12:57:17  Show Profile  Reply with Quote
If you can, you really should fix the data type. If you are storing a date value you should be using a datatype like DATE, DATETIME, SMALLDATETIME or DATETIME2.

As for the particular error you are getting.. Since you didn't supply any sample data, I can only guess that you have bad data. But, if the data is fine then you might need to "tell" SQL what format it is in by first CONVERTing the string to an actual date value specifying the format.
Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 03/18/2014 :  13:02:51  Show Profile  Reply with Quote
HI guys

I had a look at the date format and its displayed as follows
17/03/2014
17/03/2014
18/03/2014

eg day,month,year
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37133 Posts

Posted - 03/18/2014 :  13:08:49  Show Profile  Visit tkizer's Homepage  Reply with Quote
quote:
Originally posted by masond

HI guys

I had a look at the date format and its displayed as follows
17/03/2014
17/03/2014
18/03/2014

eg day,month,year



Run my queries.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 03/18/2014 :  13:39:55  Show Profile  Reply with Quote
Hi tkizer

This following statement provided me with all the dates

SELECT [Created Date]
FROM [FDMS_PartnerReporting].[Staging].[Salesforce_MarketingReporting]
WHERE ISDATE([Created Date]) = 0

what i am trying to do is find the weekno, month, year from the created date
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37133 Posts

Posted - 03/18/2014 :  13:42:38  Show Profile  Visit tkizer's Homepage  Reply with Quote
Did the one with CONVERT return an error? Or did it return all the dates too? If it didn't return an error, then use this:

SELECT MONTH(CAST([Created Date] AS datetime))
FROM [FDMS_PartnerReporting].[Staging].[Salesforce_MarketingReporting]

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 03/21/2014 :  06:30:45  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Always use proper datetime datatype to store date and time.

Try this

SELECT
month(convert(datetime,[Created Date],103))
FROM [FDMS_PartnerReporting].[Staging].[Salesforce_MarketingReporting]


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000