September 7, 2012 at 6:58 am
Hi ,
getting error while running this statement to store YYYYMM eg.201209 in database int column todatyearmonth.
eg,
convert(varchar(4),year([getdate()])) + convert(varchar(4),month([getdate()]),105) as todayyearmonth
Please help me how to proceed..
error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Regards,
Kumar
September 7, 2012 at 7:25 am
What are you trying to do?
Are you trying to store a date in an int column? Please, don't do it.
If you try to use the query you posted, it won't work unless you delete the [] surrounding the GETDATE().
Anyway, that would give you '20129'
If you're trying to store the beginning of a month from a specific date, then use this:
SELECT DATEADD( mm, DATEDIFF( mm,0,GETDATE()),0)
You can check this article:
http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/
September 7, 2012 at 7:27 am
KumarSQLDBA (9/7/2012)
Hi ,getting error while running this statement to store YYYYMM eg.201209 in database int column todatyearmonth.
eg,
convert(varchar(4),year([getdate()])) + convert(varchar(4),month([getdate()]),105) as todayyearmonth
Please help me how to proceed..
error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
The square brackets indicate to SQL server that whatever is between them is a column, in a simple query like this.
Try this instead:
SELECT CAST(CONVERT(VARCHAR(6),GETDATE(),112) AS INT)
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
September 7, 2012 at 7:29 am
And to add to Luis, there is a reason we have a datetime datatype in sql. Please us it and not some horrible integer/string disaster. It will cause you and the people after you nothing but pain. If you want datetime information use datetime. Would you try to put binary data in a varchar column??? ALWAYS use the proper datatypes.
_______________________________________________________________
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply