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


Query error


Query error

Author
Message
smharabbani40
smharabbani40
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 23
Hi i am getting the below error when i run this query.

Msg 241, Level 16, State 1, Line 2
Conversion failed when converting datetime from character string.

Declare @EndDate Datetime
select @EndDate = convert(varchar(10), getdate(), 101) +CAST(DATEPART(hh,getdate()) AS VARCHAR) +':'+ CAST(DATEPART(mi,getdate()) as VARCHAR) +':'+ CAST(DATEPART(ss,getdate()) AS VARCHAR)+CAST(DATEPART(ss,getdate()) AS VARCHAR)
print @EndDate

please let me know the correct query
shafibinyunus
shafibinyunus
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 46
Try this..

Declare @EndDate varchar(25)
select @EndDate =CONVERT(varchar,(convert(varchar(10), getdate(), 101) + ' ' + CAST(DATEPART(hh,getdate()) AS VARCHAR) +':'+ CAST(DATEPART(mi,getdate()) as VARCHAR) +':'+ CAST(DATEPART(ss,getdate()) AS VARCHAR)+CAST(DATEPART(ss,getdate()) AS VARCHAR)),109 )
print @EndDate
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61239 Visits: 17954
What exactly are you trying to do here? It looks like you are just trying to format the current date? Have you looked at CONVERT?

http://technet.microsoft.com/en-us/library/ms187928.aspx

Generally speaking it is better if you can leave the formatting to the front end instead of doing it in sql.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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: 40606 Visits: 20000
shafibinyunus (8/28/2013)
Try this..

Declare @EndDate varchar(25)
select @EndDate =CONVERT(varchar,(convert(varchar(10), getdate(), 101) + ' ' + CAST(DATEPART(hh,getdate()) AS VARCHAR) +':'+ CAST(DATEPART(mi,getdate()) as VARCHAR) +':'+ CAST(DATEPART(ss,getdate()) AS VARCHAR)+CAST(DATEPART(ss,getdate()) AS VARCHAR)),109 )
print @EndDate


This returns incorrect results, as does the original. Sean has the right idea. Try this:

SELECT
[Getdate] = GETDATE(),

[Old version] = CONVERT(varchar,(convert(varchar(10), getdate(), 101) + ' '
+ CAST(DATEPART(hh,getdate()) AS VARCHAR) + ':'
+ CAST(DATEPART(mi,getdate()) as VARCHAR) + ':'
+ CAST(DATEPART(ss,getdate()) AS VARCHAR)
+ CAST(DATEPART(ss,getdate()) AS VARCHAR)),109 ),

[New version] = CONVERT(CHAR(11),GETDATE(),101)
+ STUFF(CONVERT(VARCHAR(11),GETDATE(),114),9,1,'')



“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
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38634 Visits: 14411
SELECT  FORMAT(GETDATE(), 'MM/dd/yyyy HH:mm:ss') 
AS [try the FORMAT function available in SQL 2012 and above];



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
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: 40606 Visits: 20000
opc.three (9/9/2013)
SELECT  FORMAT(GETDATE(), 'MM/dd/yyyy HH:mm:ss') 
AS [try the FORMAT function available in SQL 2012 and above];



Thanks Orlando, I'll have a play with this at home when time permits.

“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