Showing posts with label holds. Show all posts
Showing posts with label holds. Show all posts

Sunday, March 25, 2012

Converting a varchar to int

I am struggling with converting a certain varchar column into an int.
I have a table that has 2 fields - one field holds the loan number and
the other field holds the codes associated with that loan number.
Here's some example data:

Loan# Codes
11111 24-13-1
22222 1
33333 2-9

I need to check the Codes field for certain code numbers. The Select
statement I'd like to use is:

SELECT Loan#
FROM Table1 WHERE Codes IN (2, 13, 1)
/*My desired results is that all loans from the above example would be
selected because they all have one of these codes*/

Of course I cannot use the above statement because the Codes field is a
varchar. And if I put single quotes around the numbers in my IN
statement I don't get the desired results; the fields with multiple
codes are excluded.

But how do I convert this varchar to an int? A simple convert or cast
statement doesn't work. I've looked all over the web to find how to do
this, but have not been able to figure it out. Any help would be much
appreciated.Patti wrote:

Quote:

Originally Posted by

I am struggling with converting a certain varchar column into an int.
I have a table that has 2 fields - one field holds the loan number and
the other field holds the codes associated with that loan number.
Here's some example data:
>
Loan# Codes
11111 24-13-1
22222 1
33333 2-9


A classic violation of first normal form:

http://en.wikipedia.org/wiki/First_..._ single_field
If at all possible, change your table to look like this:

Loan# Code
11111 24
11111 13
11111 1
22222 1
33333 2
33333 9

Quote:

Originally Posted by

I need to check the Codes field for certain code numbers. The Select
statement I'd like to use is:
>
SELECT Loan#
FROM Table1 WHERE Codes IN (2, 13, 1)
/*My desired results is that all loans from the above example would be
selected because they all have one of these codes*/


and then this simply becomes

SELECT Loan#
FROM Table1
WHERE Code in (2, 13, 1)

That said, if fixing the 1NF violation will take a while, then in the
short term, you can do something like the following. (You can't convert
Codes to int, because e.g. '24-13-1' isn't a number. Instead, you must
convert the search terms from int to varchar.)

SELECT Loan#
FROM Table1
WHERE '-'+Codes+'-' like '-2-'
OR '-'+Codes+'-' like '-13-'
OR '-'+Codes+'-' like '-1-'

Also, you may need SELECT DISTINCT, in case some Loan#s have multiple
matches and you only want to include them once.|||I can't change the actual table, but I can create a stored proc that
inserts it correctly into another table. I didn't even think to do
that (**duh**)! Thank you very much for your assistance!

Ed Murphy wrote:

Quote:

Originally Posted by

Patti wrote:
>

Quote:

Originally Posted by

I am struggling with converting a certain varchar column into an int.
I have a table that has 2 fields - one field holds the loan number and
the other field holds the codes associated with that loan number.
Here's some example data:

Loan# Codes
11111 24-13-1
22222 1
33333 2-9


>
A classic violation of first normal form:
>
http://en.wikipedia.org/wiki/First_..._ single_field
>
If at all possible, change your table to look like this:
>
Loan# Code
11111 24
11111 13
11111 1
22222 1
33333 2
33333 9
>

Quote:

Originally Posted by

I need to check the Codes field for certain code numbers. The Select
statement I'd like to use is:

SELECT Loan#
FROM Table1 WHERE Codes IN (2, 13, 1)
/*My desired results is that all loans from the above example would be
selected because they all have one of these codes*/


>
and then this simply becomes
>
SELECT Loan#
FROM Table1
WHERE Code in (2, 13, 1)
>
That said, if fixing the 1NF violation will take a while, then in the
short term, you can do something like the following. (You can't convert
Codes to int, because e.g. '24-13-1' isn't a number. Instead, you must
convert the search terms from int to varchar.)
>
SELECT Loan#
FROM Table1
WHERE '-'+Codes+'-' like '-2-'
OR '-'+Codes+'-' like '-13-'
OR '-'+Codes+'-' like '-1-'
>
Also, you may need SELECT DISTINCT, in case some Loan#s have multiple
matches and you only want to include them once.

|||Ed Murphy (emurphy42@.socal.rr.com) writes:

Quote:

Originally Posted by

SELECT Loan#
FROM Table1
WHERE '-'+Codes+'-' like '-2-'
OR '-'+Codes+'-' like '-13-'
OR '-'+Codes+'-' like '-1-'


Seems like some % are missing.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||This might work:
SELECT Loan#
FROM Table1
WHERE patindex('%[2,13,1]%',Codes) 0

Patti wrote:

Quote:

Originally Posted by

I am struggling with converting a certain varchar column into an int.
I have a table that has 2 fields - one field holds the loan number and
the other field holds the codes associated with that loan number.
Here's some example data:
>
Loan# Codes
11111 24-13-1
22222 1
33333 2-9
>
I need to check the Codes field for certain code numbers. The Select
statement I'd like to use is:
>
SELECT Loan#
FROM Table1 WHERE Codes IN (2, 13, 1)
/*My desired results is that all loans from the above example would be
selected because they all have one of these codes*/
>
Of course I cannot use the above statement because the Codes field is a
varchar. And if I put single quotes around the numbers in my IN
statement I don't get the desired results; the fields with multiple
codes are excluded.
>
But how do I convert this varchar to an int? A simple convert or cast
statement doesn't work. I've looked all over the web to find how to do
this, but have not been able to figure it out. Any help would be much
appreciated.

|||Erland Sommarskog wrote:

Quote:

Originally Posted by

Ed Murphy (emurphy42@.socal.rr.com) writes:


Quote:

Originally Posted by

Quote:

Originally Posted by

>SELECT Loan#
>FROM Table1
>WHERE '-'+Codes+'-' like '-2-'
> OR '-'+Codes+'-' like '-13-'
> OR '-'+Codes+'-' like '-1-'


>
Seems like some % are missing.


Yes, of course you're right, should be

WHERE '-'+Codes+'-' like '%-2-%'
OR '-'+Codes+'-' like '%-13-%'
OR '-'+Codes+'-' like '%-1-%'

but the approach of "use a stored procedure to copy the data to a
better-normalized table" is probably better. (Oh, and that new
table should probably have an index on the Code column.)

Converting a string of binary numbers to a binary datatype

I have a varchar field which holds numeric (binary) data e.g. '00101111' and I want to convert this to a binary data type with the value e.g. 0x00101111

But when I try the following SQL:

select top 5 flag2,convert(binary,flag2) flag2_as_binary from my_table

I get:

flag2 flag2_as_binary
--- -------------------
00000000 0x303030303030303000000000000000000000000000000000 000000000000
00000000 0x303030303030303000000000000000000000000000000000 000000000000
00000000 0x303030303030303000000000000000000000000000000000 000000000000
00000100 0x303030303031303000000000000000000000000000000000 000000000000
00000100 0x303030303031303000000000000000000000000000000000 000000000000

(5 row(s) affected)

I want some SQL that will return the following (with flag2_as_binary as a real binary datatype):

flag2 flag2_as_binary
--- -------------------
00000000 0x00000000
00000000 0x00000000
00000000 0x00000000
00000100 0x00000100
00000100 0x00000100

(5 row(s) affected)

Thanks.The sql binary datatype is actually displayed hexidecimal base 16 usinge characters 0-9 and A-F, not base 2.

Monday, March 19, 2012

Convert varchar to datetime

I have a column that holds dates but is varchar(50) and contains data in the following format:

01/12/2003

Code:
SET DATEFORMAT dmy
DECLARE @.Data DATETIME
SET @.Data = 01/01/2004
SELECT * FROM Lista WHERE CONVERT(DATETIME, Data) < @.Data

With this code it does not return no result. But if to change the operator returns all the registers. E all the registered in cadastre dates are 1/1/2003

Help me please.Hi there,

instead of
SET @.Data = 01/01/2004
you should try
SET @.Data = '01/01/2004'
and it should work!

Greetings,
Carsten|||Very thanks!
You really it saved my day.
Thanks

Sunday, February 19, 2012

Convert int seconds to legable time

I have a field that holds the number of seconds from midnight. I need to
take this int value and turn it into a legable time. I also need to take
input in 12:00 format and change back to int seconds to store in the db.
Any suggestions?You need to read up on datediff and dateadd.
declare @.secs bigint
set @.secs = 12345
select dateadd(second, @.secs, '2 March 2006')
---
2006-03-02 03:25:45.000
select datediff (second, '2 March 2006', '2 March 2006 16:47:13')
60433
Roy Harvey
Beacon Falls, CT
On Thu, 2 Mar 2006 12:07:27 -0800, Pat Brown
<PatBrown@.discussions.microsoft.com> wrote:

>I have a field that holds the number of seconds from midnight. I need to
>take this int value and turn it into a legable time. I also need to take
>input in 12:00 format and change back to int seconds to store in the db.
>Any suggestions?

Convert HH,MM to decimal ?

I have a table with a specific column that i get from an AS/400
The column holds worktime specified in HH,MM format. How do i convert
that do a decimal number.
ie. 7,45 (7 Hours and 45 minutes) i want it to become 7,75.
Thankskjo007@.hotmail.com wrote:

> ie. 7,45 (7 Hours and 45 minutes) i want it to become 7,75.
select datepart(hour, '2005-01-01 18:45') + cast(datepart(minute,
'2005-01-01 07:45 AM') as decimal) / 60
HTH,
Stijn Verrept.|||>> ie. 7,45 (7 Hours and 45 minutes) i want it to become 7,75.
>
> select datepart(hour, '2005-01-01 18:45') + cast(datepart(minute,
> '2005-01-01 07:45 AM') as decimal) / 60
I don't think that will work as "HH,MM" is not a recognised Sql date format
and therefore the date functions you use will not return the expected data..

> HTH,
> Stijn Verrept.
>
--== Posted via mcse.ms - Unlimited-Unrestricted-Secure Usenet News=
=--
http://www.mcse.ms The #1 Newsgroup Service in the World! 120,000+ New
sgroups
--= East and West-Coast Server Farms - Total Privacy via Encryption =--|||Peter wrote:

> I don't think that will work as "HH,MM" is not a recognised Sql date
> format and therefore the date functions you use will not return the
> expected data..
? What do you mean? I tried it out, it works without problems. He
wants the time converted to decimal and that's what this does:
select datepart(hour, '2005-01-01 07:45 AM') + cast(datepart(minute,
'2005-01-01 07:45 AM') as decimal) / 60
Kind regards,
Stijn Verrept.|||
> ? What do you mean? I tried it out, it works without problems. He
> wants the time converted to decimal and that's what this does:
> select datepart(hour, '2005-01-01 07:45 AM') + cast(datepart(minute,
> '2005-01-01 07:45 AM') as decimal) / 60
> --
> Kind regards,
> Stijn Verrept.
>
He said he is receiving the data from AS/400 in the format "HH,MM" and gave
the example "7,45"
Substituting that into your solution gives...
select datepart(hour, '7,45') + cast(datepart(minute, '7,45') as decimal) /
60
... which doesn't work!
Regards
Peter
--== Posted via mcse.ms - Unlimited-Unrestricted-Secure Usenet News=
=--
http://www.mcse.ms The #1 Newsgroup Service in the World! 120,000+ New
sgroups
--= East and West-Coast Server Farms - Total Privacy via Encryption =--|||<kjo007@.hotmail.com> wrote in message
news:1139314990.653609.216860@.f14g2000cwb.googlegroups.com...
>I have a table with a specific column that i get from an AS/400
> The column holds worktime specified in HH,MM format. How do i convert
> that do a decimal number.
> ie. 7,45 (7 Hours and 45 minutes) i want it to become 7,75.
> Thanks
declare @.time varchar(5)
set @.time = '7,45'
select cast(left(@.time, charindex(',', @.time)-1) + (right(@.time,
len(@.time) - charindex(',', @.time)))/60.0 as decimal(5,2))|||It may be worth creating a function for this logic if it is needed in more
than one place.
usd_ConvertAS400Time(7,45) will be easier to type, read, and maintain.
However, there may be a performance hit, I'm not really sure.
Then again, changing the column and your AS400 import to store the value
differently may remove more headaches, depending on if you ever use the data
in the current format.
"Raymond D'Anjou" <rdanjou@.canatradeNOSPAM.com> wrote in message
news:%23ZoNr3$KGHA.2276@.TK2MSFTNGP15.phx.gbl...
> <kjo007@.hotmail.com> wrote in message
> news:1139314990.653609.216860@.f14g2000cwb.googlegroups.com...
> declare @.time varchar(5)
> set @.time = '7,45'
> select cast(left(@.time, charindex(',', @.time)-1) + (right(@.time,
> len(@.time) - charindex(',', @.time)))/60.0 as decimal(5,2))
>|||Peter wrote:

> He said he is receiving the data from AS/400 in the format "HH,MM"
> and gave the example "7,45"
> Substituting that into your solution gives...
> select datepart(hour, '7,45') + cast(datepart(minute, '7,45') as
> decimal) / 60
> ... which doesn't work!
Very true, my bad!
HTH,
Stijn Verrept.|||Of course, either way there could be a performance hit.
If he cannot change the table structure...
it may be better (easier/faster) for the poster to do the conversion client
side..
"Jim Underwood" <james.underwoodATfallonclinic.com> wrote in message
news:ejySWSALGHA.532@.TK2MSFTNGP15.phx.gbl...
> It may be worth creating a function for this logic if it is needed in more
> than one place.
> usd_ConvertAS400Time(7,45) will be easier to type, read, and maintain.
> However, there may be a performance hit, I'm not really sure.
> Then again, changing the column and your AS400 import to store the value
> differently may remove more headaches, depending on if you ever use the
> data
> in the current format.
> "Raymond D'Anjou" <rdanjou@.canatradeNOSPAM.com> wrote in message
> news:%23ZoNr3$KGHA.2276@.TK2MSFTNGP15.phx.gbl...
>|||maybe this:
select replace('7,45', ',', ':')
7:45

Sunday, February 12, 2012

Convert Error

I am trying to convert a varchar(10) into a smalldatetime field in a view.

The table holds the date information in the format dd/mm/yyyy in a
varchar(10).

I can convert most fields in this format in my view using;

CONVERT (smalldatetime, ActualInDate, 103)

However one field fails, I have some dates as 02/01/0000, this is because it is a blank system date which is output from my mainframe job to extract data from another server.

Can I convert these blank dates to sql blank dates in my convert statement, ie replace these dates from 02/01/0000 to 01/01/1900.

The error I get is the error below;

Database Server: Microsoft SQL Server
Version 08.00.0760
Runtime Error: [Microsoft][ODBC SQL Server Driver][SQL Server]The conversion of char data type to smalldatetime datatype resulted in an out-of-range small datetime value.

Cheers,

JonMy guess would be that you are having problems with different data rows than you think are causing the problems. Use the IsDate() function to show you the "problem children" in your data. I suspect that your server is interpreting the dates as MM/DD/YYYY instead of dd/mm/yyyy as you expect. If that is the case, I'd convert the whole bunch to ISO standard format of YYYY-MM-DD (like 1900-01-01), which is a lot harder to misinterpret!

-PatP|||This appears to work:

CREATE TABLE MyTable
(
ActualInDate varchar(20)
)

INSERT INTO MyTable(ActualInDate)
VALUES ('02/01/0000')
INSERT INTO MyTable(ActualInDate)
VALUES ('13/02/1975')

SELECT CONVERT (smalldatetime, (CASE WHEN ActualInDate = '02/01/0000' THEN '01/01/1900' ELSE ActualInDate END), 103)
FROM MyTable|||Yup, it appears, but appearances are misleading...

SELECT CONVERT (smalldatetime, (
case
when charindex('0000', ActualInDate) > 0 then '01/01/1900'
else case
when isdate(ActualInDate) = 0 then '01/01/1900'
else ActualInDate end
end), 103)
FROM MyTable|||Thanks for the ideas but I just used this to do it and it seems fine:

CONVERT (smalldatetime, REPLACE(ActualIn, '02/01/0000', '01/01/1900'), 103)

Cheers|||rdjabarov,
do you hang out to bust my balls or is it just my imagination. i was solving the stated problem not writing his application for him.

He did'nt ask me to validate his date or I would have suggested isDate as well. He asked to get rid of the null data value coming from his main frame and that's what I did.|||rdjabarov,
do you hang out to bust my balls or is it just my imagination. i was solving the stated problem not writing his application for him.

He did'nt ask me to validate his date or I would have suggested isDate as well. He asked to get rid of the null data value coming from his main frame and that's what I did.I don't think that's his sole purpose in life, but rdjabarov often comes across that way. I've just had to develop thicker skin where his posts are concerned.

He really does know what he's doing (at least as often as most of us do anyway :)), but he comes across pretty abrasively at time.

-PatP|||Well there's the other side of the coin as well...you have to take everything with a grain of salt...because

IT JUST DOESN'T MATTER

Speaking of which...where's Rudy?|||I did not say he did'nt know what he was talking about. I was objecting to the snide remarks

I am sorry.
I am cranky ass.
Quitting smoking.
Unrealistic deadline.
Little sleep.
Stressed from a couple job interviews.
Too easily offended from my hot southern celtic blood.
I apologize.|||Speaking of which...where's Rudy?speaking of abrasive, i was actually enjoying the intellectual dialogue in this thread until your honking great red font spoiled the mood

;)|||i was actually enjoying the intellectual dialogue in this thread

I'm still looking...|||speaking of abrasive, i was actually enjoying the intellectual dialogue in this thread until your honking great red font spoiled the mood

;)Yeah, but it does help to put things into context!

-PatP|||Pat does contexts ;)