Showing posts with label cast. Show all posts
Showing posts with label cast. Show all posts

Friday, February 24, 2012

Converting/Casting strings into Datetime datatype

Hello,

I have a varchar column that inludes dates in the following fomat: 03032007? When I try to cast this to datetime, I keep getting "Arithmetic overflow error converting expression to data type datetime." error. Maybe someone has some ideas how to handle this?

Thanks!

If you had only stored your date values in the ISO format of YYYYMMDD, they would easily cast or convert to datetime. -Or even left in one of the standard date delimiters, such as [ / - ].

However, you (or some unnamed 'other' person) made up a oddball format, and now you will have to 'handle' it to create a 'real' date value everytime you need to use it.

(This assumes your format is MMDDYYYY.)

SELECT cast( stuff( stuff( '03032007', 3, 0, '/' ), 6, 0, '/' ) AS datetime )


-
2007-03-03 00:00:00.000

|||

You know that MS stores sqlagent datetime in to two int columns with the following format, right? (Take a look at the schema for msdb: sysalerts,sysjobhistory, sysjobschedules, sysjobservers, and sysjobsteps.)

date: YYYYMMDD

time: HHMMSS

So, it's not that weird to see the public employs such schema.

|||

But I also notice that MS stores SQL Agent datetime in ISO format (YYYYMMDD).

That little 'standard' makes a lot of difference in cast/convert.

That is behind my even mentioning using a standard ISO format in my response...

Converting varchar to int (or numeric)

In one of my stored procedures, a varchar is input that is assumed to be an int, and I need to validate that it is an int before I CAST it as an int. I'm currently using ISNUMERIC() to eliminate non-numeric values, but how do I recover gracefully if values such as '7.5e3', '$334', or '45.9943' sneak through? They all pass the ISNUMERIC() test, but cause errors when being cast to int. Similar problems if casting to numeric, etc.You can use the case statement - for example:

declare @.test varchar(20)
select @.test='123.3'
select case when charindex('.',@.test) > 0 then cast(@.test as decimal(10,2))
when charindex('e',@.test) > 0 then cast...
else cast(@.test as int) end|||Thanks. That should work. On retrospect, it's obvious enough I should have come up with that myself!

Sunday, February 19, 2012

Converting to number: convert, cast ??

HI,
Converting to number: convert, cast '
what should I use and how'
I have looked at sql server 2000 help and until now I couldn't get sucess1
to string there is the function Str and for numbers'
Isn´t there any function like CInt, CDbl, CLng, Eval, etc..from vb,
vbscript'
see the code below,thanks
vilmar
spInserirImagem 1,'fundo_condominio_amarelo_01.jpg','amarelo',''
Drop Procedure spInserirImagem
Create Procedure spInserirImagem
@.IdImagem int,@.Descricao char(50),@.PadraoCor char(50),@.strSQL char(5000)
As
begin
Set @.IdImagem = Cast(@.IdImagem as int)
Set @.strSQL = 'Insert into ImagemTopo(IdImagem,Descricao,PadraoCor)
values(' + @.IdImagem + ',' + @.Descricao + ',' + @.PadraoCor + ')'
print RTRIM(LTRIM(@.strSQL))
Exec(@.strSQL)
end> Set @.IdImagem = Cast(@.IdImagem as int)
> Set @.strSQL = 'Insert into ImagemTopo(IdImagem,Descricao,PadraoCor)
> values(' + @.IdImagem + ',' + @.Descricao + ',' + @.PadraoCor + ')'
The problem here is that you are injecting an integer into a string. In
order for @.strSQL to work, you need to convert the INT to a string. This
should work fine:
Set @.IdImagem = Cast(@.IdImagem as VARCHAR(12))
Set @.strSQL = 'Insert into ImagemTopo(IdImagem,Descricao,PadraoCor) values('
+ @.IdImagem + ',' + @.Descricao + ',' + @.PadraoCor + ')'
However, I'm not sure why you think you have to make a string out of this.
Can't you just do:
INSERT ImagemTopo(IdImagem,Descricao,PadraoCor)
VALUES(@.IdImagem, @.Descricao, @.PadraoCor)
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/|||Hi
I am not sure why think you need to cast this way, as you appending to a
string. The following should work:
Set @.strSQL = 'Insert into ImagemTopo(IdImagem,Descricao,PadraoCor)
values(' + CONVERT(varchar,@.IdImagem) + ',''' + @.Descricao + ''',''' +
@.PadraoCor + ''')'
John
"news.microsoft.com" <suporte@.hitecnet.com.br> wrote in message
news:uvtN3$zuDHA.2060@.TK2MSFTNGP10.phx.gbl...
> HI,
> Converting to number: convert, cast '
> what should I use and how'
> I have looked at sql server 2000 help and until now I couldn't get sucess1
> to string there is the function Str and for numbers'
> Isn´t there any function like CInt, CDbl, CLng, Eval, etc..from vb,
> vbscript'
> see the code below,thanks
> vilmar
> spInserirImagem 1,'fundo_condominio_amarelo_01.jpg','amarelo',''
> Drop Procedure spInserirImagem
> Create Procedure spInserirImagem
> @.IdImagem int,@.Descricao char(50),@.PadraoCor char(50),@.strSQL char(5000)
> As
> begin
> Set @.IdImagem = Cast(@.IdImagem as int)
> Set @.strSQL = 'Insert into ImagemTopo(IdImagem,Descricao,PadraoCor)
> values(' + @.IdImagem + ',' + @.Descricao + ',' + @.PadraoCor + ')'
> print RTRIM(LTRIM(@.strSQL))
> Exec(@.strSQL)
> end
>|||Thank you John Bell and Aaron Bertrand a lot!!
I was too forgetting about double single quotes in stored procedure besides
putting convert or cast in the way which
you showed me!!
Regards,
Vilmar
From Brazil
"John Bell" <jbellnewsposts@.hotmail.com> escreveu na mensagem
news:AK1Ab.20423$_y4.206566615@.news-text.cableinet.net...
> Hi
> I am not sure why think you need to cast this way, as you appending to a
> string. The following should work:
> Set @.strSQL = 'Insert into ImagemTopo(IdImagem,Descricao,PadraoCor)
> values(' + CONVERT(varchar,@.IdImagem) + ',''' + @.Descricao + ''',''' +
> @.PadraoCor + ''')'
> John
> "news.microsoft.com" <suporte@.hitecnet.com.br> wrote in message
> news:uvtN3$zuDHA.2060@.TK2MSFTNGP10.phx.gbl...
> > HI,
> > Converting to number: convert, cast '
> > what should I use and how'
> > I have looked at sql server 2000 help and until now I couldn't get
sucess1
> > to string there is the function Str and for numbers'
> >
> > Isn´t there any function like CInt, CDbl, CLng, Eval, etc..from vb,
> > vbscript'
> > see the code below,thanks
> > vilmar
> > spInserirImagem 1,'fundo_condominio_amarelo_01.jpg','amarelo',''
> > Drop Procedure spInserirImagem
> > Create Procedure spInserirImagem
> > @.IdImagem int,@.Descricao char(50),@.PadraoCor char(50),@.strSQL char(5000)
> > As
> > begin
> > Set @.IdImagem = Cast(@.IdImagem as int)
> > Set @.strSQL = 'Insert into ImagemTopo(IdImagem,Descricao,PadraoCor)
> > values(' + @.IdImagem + ',' + @.Descricao + ',' + @.PadraoCor + ')'
> > print RTRIM(LTRIM(@.strSQL))
> > Exec(@.strSQL)
> > end
> >
> >
>

Converting to datetime...

Hello..
I am having a difficult time trying to get SQL Server to convert the
following date:
22-08-2004 00:00:00
I have tried to convert and cast and I get the following error message:
Server: Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.
Any help would be most appreciative.
Thank you,
Brett
P.S.
I am using SQL Server 2000How are you doing this convert ? This works for me:
Select convert(varchar(20),'22-08-2004 00:00:00',102)
Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Brett Davis" <bdavis123@.cox.net> schrieb im Newsbeitrag
news:eHEz6r1SFHA.2432@.TK2MSFTNGP12.phx.gbl...
> Hello..
> I am having a difficult time trying to get SQL Server to convert the
> following date:
> 22-08-2004 00:00:00
> I have tried to convert and cast and I get the following error message:
> Server: Msg 242, Level 16, State 3, Line 1
> The conversion of a char data type to a datetime data type resulted in
> an out-of-range datetime value.
> Any help would be most appreciative.
> Thank you,
> Brett
> P.S.
> I am using SQL Server 2000
>|||You need to tell SQL Server more about the pattern used for the datetime. Tr
y
this:
Select Convert(DateTime, '22-08-2004 00:00:00', 105)
Without specifying the date format, I believe that SQL uses the format from
the
database's collation or perhaps the server's regional settings.
HTH
Thomas
"Brett Davis" <bdavis123@.cox.net> wrote in message
news:eHEz6r1SFHA.2432@.TK2MSFTNGP12.phx.gbl...
> Hello..
> I am having a difficult time trying to get SQL Server to convert the follo
wing
> date:
> 22-08-2004 00:00:00
> I have tried to convert and cast and I get the following error message:
> Server: Msg 242, Level 16, State 3, Line 1
> The conversion of a char data type to a datetime data type resulted in
an
> out-of-range datetime value.
> Any help would be most appreciative.
> Thank you,
> Brett
> P.S.
> I am using SQL Server 2000
>|||Use styles 112 or 126. See CONVERT in BOL.
Example:
select cast('20040822' as datetime)
select cast('2004-08-22T00:00:00.000' as datetime)
go
AMB
"Brett Davis" wrote:

> Hello..
> I am having a difficult time trying to get SQL Server to convert the
> following date:
> 22-08-2004 00:00:00
> I have tried to convert and cast and I get the following error message:
> Server: Msg 242, Level 16, State 3, Line 1
> The conversion of a char data type to a datetime data type resulted in
> an out-of-range datetime value.
> Any help would be most appreciative.
> Thank you,
> Brett
> P.S.
> I am using SQL Server 2000
>
>|||Thomas,
You use the style parameter when converting from datetime to varchar / char
and not the opposite. All these statements will give same result.
select convert(datetime, '20050427', 105)
select convert(datetime, '20050427', 112)
select convert(datetime, '20050427', 126)
select convert(datetime, '20050427')
AMB
"Thomas" wrote:

> You need to tell SQL Server more about the pattern used for the datetime.
Try
> this:
> Select Convert(DateTime, '22-08-2004 00:00:00', 105)
> Without specifying the date format, I believe that SQL uses the format fro
m the
> database's collation or perhaps the server's regional settings.
>
> HTH
>
> Thomas
>
> "Brett Davis" <bdavis123@.cox.net> wrote in message
> news:eHEz6r1SFHA.2432@.TK2MSFTNGP12.phx.gbl...
>
>|||I think we might be saying the same thing. By passing the style parameter, y
ou
are giving SQL information about the format of the string.

>All these statements will give same result.
> select convert(datetime, '20050427', 105)
> select convert(datetime, '20050427', 112)
> select convert(datetime, '20050427', 126)
> select convert(datetime, '20050427')
But these do not:
1. select convert(datetime, '22-08-2004 00:00:00', 105)
2. select convert(datetime, '22-08-2004 00:00:00', 112)
3. select convert(datetime, '22-08-2004 00:00:00', 126)
4. select convert(datetime, '22-08-2004 00:00:00',)
Only the first one successfully parses the string into a datetime. The other
s
fail with a conversion error because the system thinks that the first digits
are
the month instead of the day.
I'll grant you that passing the ISO format (yyyymmdd) would be the best way
to
avoid all of these problems.
Thomas|||You are right.
AMB
"Thomas" wrote:

> I think we might be saying the same thing. By passing the style parameter,
you
> are giving SQL information about the format of the string.
>
> But these do not:
> 1. select convert(datetime, '22-08-2004 00:00:00', 105)
> 2. select convert(datetime, '22-08-2004 00:00:00', 112)
> 3. select convert(datetime, '22-08-2004 00:00:00', 126)
> 4. select convert(datetime, '22-08-2004 00:00:00',)
> Only the first one successfully parses the string into a datetime. The oth
ers
> fail with a conversion error because the system thinks that the first digi
ts are
> the month instead of the day.
> I'll grant you that passing the ISO format (yyyymmdd) would be the best wa
y to
> avoid all of these problems.
>
> Thomas
>
>