Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert int int to date urgent please


convert int int to date urgent please

Author
Message
ashwinboinala
ashwinboinala
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 497
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [ServerName]
,[IsExisting]
,[IsEnabled]
,[IsScheduleEnabled]
,[NAME]
,CONVERT(date, CONVERT(VARchar(18), LASTRUNDATE))
,[Date]
FROM [Monitor].[dbo].[Job_Monitor]

plz need it urgent lastrundate is int (20130620)format need to convert to date 2013/06/20
error :::

Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.
HowardW
HowardW
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1211 Visits: 9892
Nothing wrong with the syntax, so you must have values in the column that aren't a valid date value.

Look at the following examples:

DECLARE @lastrundate INT='20130228'

SELECT CONVERT(date, CONVERT(VARchar(18), @lastrundate))
--success


DECLARE @lastrundate INT='20130229'

SELECT CONVERT(date, CONVERT(VARchar(18), @lastrundate))
--failure



You'll need to find the invalid date values before converting.
ashwinboinala
ashwinboinala
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 497
i have 0 in that so do i need to write case
ashwinboinala
ashwinboinala
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 497
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [ServerName]
,[IsExisting]
,[IsEnabled]
,[IsScheduleEnabled]
,[NAME]

,case when CONVERT(date, CONVERT(VARchar(18), 'LASTRUNDATE'))='0' then NULL else CONVERT(date, CONVERT(VARchar(18), LASTRUNDATE)) end as lastrundate
,[Date]
FROM [Monitor].[dbo].[Job_Monitor]


plz help

error

Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.
HowardW
HowardW
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1211 Visits: 9892
You need to find the rows that don't convert. Try this out to find the offending rows:


CREATE TABLE #validdatesasints ( date INT PRIMARY KEY )


DECLARE @ValidDateStartValue DATE= '19000101' , --technically speaking you can go to the year 0001, but I wouldn't bother and it makes the lookup table v large
@ValidDateEndValue DATE= '21000101'
--same as above, can go to year 9999, but seems silly


DECLARE @Top INT= DATEDIFF(DAY, @ValidDateStartValue, @ValidDateEndValue) + 1

INSERT INTO #validdatesasints
( date
)
SELECT TOP ( @Top )
CONVERT(INT, CONVERT(VARCHAR, DATEADD(day,
ROW_NUMBER() OVER ( ORDER BY ( SELECT
NULL
) ) - 1,
@ValidDateStartValue), 112))
FROM master.sys.columns a ,
master.sys.columns b



--find offending rows
SELECT *
FROM [Monitor].[dbo].[Job_Monitor]
WHERE LASTRUNDATE NOT IN ( SELECT date
FROM #validdatesasints )
AND LASTRUNDATE IS NOT NULL


ashwinboinala
ashwinboinala
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 497
thanks for u r time getting below error
Msg 208, Level 16, State 0, Line 10
Invalid object name '#validdatesasints'.
ashwinboinala
ashwinboinala
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 497
i am getting this value from sysjobhistroy table from msdb there its in int format if u can give me solution where i can filter that table thats fine
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8582 Visits: 18157
Would this give you a clue?
SELECT CONVERT( date, CONVERT( char(8), NULLIF( Someintdate, 0)))
FROM (VALUES(20130601), (0), (20130515))x(Someintdate)




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
ashwinboinala (6/20/2013)
i am getting this value from sysjobhistroy table from msdb there its in int format if u can give me solution where i can filter that table thats fine


Wouldnt the LastRunDate be NULL if the job has never been run? which is where you problem maybe.

As a test wrap an ISNULL around the LASTRUNDATE field and set it to 1 then run the query.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
HowardW
HowardW
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1211 Visits: 9892
Jason-299789 (6/20/2013)
ashwinboinala (6/20/2013)
i am getting this value from sysjobhistroy table from msdb there its in int format if u can give me solution where i can filter that table thats fine


Wouldnt the LastRunDate be NULL if the job has never been run? which is where you problem maybe.

As a test wrap an ISNULL around the LASTRUNDATE field and set it to 1 then run the query.


Converting NULL values won't fail. There must be non-null int values that are not convertible.
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