Showing posts with label bol. Show all posts
Showing posts with label bol. Show all posts

Thursday, March 22, 2012

Converting @myString to @myInt

Here's a fun one. BOL says that using CONVERT or CAST to change a string into a number won't work.

The following "works" and I'm assuming it is because the compiler can verify that the string value is in fact a number, even with the Unicode N:

declare @.col2 int

select @.col2 = convert (int, N'12345')

======================================

Here's what I need to work, or I need a creative work-around if someone has an interesting idea:

declare @.col2 int,
@.myString nvarchar(100)

select @.col2 = '12345'
select @.myString = convert(int, @.col2)

Msg 245, Level 16, State 1, Line 67
Conversion failed when converting the nvarchar value '12345' to data type int.

I'm assuming that since the compiler cannot pre-determine the value of @.col2 that it won't allow this.

I understand the responsibility to ensure that the converted string is actually a number. I can put something together as a pre-check for this.

Any suggestions or creative work-arounds would be helpful!

Thanks!

Doug B

> declare @.col2 int,

> @.myString nvarchar(100)

>

> select @.col2 = '12345'

> select @.myString = convert(int, @.col2)

>

> Msg 245, Level 16, State 1, Line 67

> Conversion failed when converting the nvarchar value '12345' to data

> type int.

This works fine for me on SQL Server 2000 (8.00.2151) and SQL Server 2005

(9.00.1399).

A

|||Thanks so much...my example was an attempt to simplify the question, so the code wasn't "exactly" what I was running. Once I realized it should have worked, I found a bug in a substring elsewhere.

Looks like you can convert from a string to a number after all, as long as it is a valid number.

Thanks!

DB|||

> Thanks so much...my example was an attempt to simplify the question,

Don't do that. The people here who are trying to help do not need it.

Whether your SQL is 4 lines or 40, we are still going to cut & paste into QA

or SSMS and run it, and then we will find the actual problem instead of

wasting time asking for more information...

Friday, February 10, 2012

Convert Date to String

Hello, I have a date field in a table. I don't need the time part. I want
the result to be in the format YYYYMMDD. Consulted BOL and it gave me:
SELECT CONVERT (char, fieldname, 8). Tried that and I get the time hours.
How can I get it to read from the left and only give me YYYYMMDD? Thanks,
PanchoSELECT CONVERT(CHAR(8), ColumnName, 112)
"Pancho" <Pancho@.discussions.microsoft.com> wrote in message
news:D45536FD-271B-413F-BAFC-5EB418FC7FCE@.microsoft.com...
> Hello, I have a date field in a table. I don't need the time part. I
> want
> the result to be in the format YYYYMMDD. Consulted BOL and it gave me:
> SELECT CONVERT (char, fieldname, 8). Tried that and I get the time hours.
> How can I get it to read from the left and only give me YYYYMMDD? Thanks,
> Pancho|||Aaron,
This is great, worked fine. It required me to add the phrase AS ColumnName
and then it worked. I had tried styles 1 through 16 and stopped. I got a
message, and then assumed that there were no higher style numbers. This
taught me the danger of assuming. Thanks for your quick response. Have a
nice wend, Pancho.
"Aaron Bertrand [SQL Server MVP]" wrote:

> SELECT CONVERT(CHAR(8), ColumnName, 112)
>
> "Pancho" <Pancho@.discussions.microsoft.com> wrote in message
> news:D45536FD-271B-413F-BAFC-5EB418FC7FCE@.microsoft.com...
>
>|||> This is great, worked fine. It required me to add the phrase AS
> ColumnName
> and then it worked. I had tried styles 1 through 16 and stopped. I got a
> message, and then assumed that there were no higher style numbers. This
> taught me the danger of assuming.
Yes, you should learn searching instead. :-)
http://www.aspfaq.com/2464|||Bookmarked aspfaq.com! Thanks again, P.
"Aaron Bertrand [SQL Server MVP]" wrote:

> Yes, you should learn searching instead. :-)
> http://www.aspfaq.com/2464
>
>