I have a table where the first column has several numbers in numerous
formats. I would like to sort by using the first two digits. (15)
This is what I have right now.
select *
FROM GL1400
WHERE GL_ACCT '15';
GO
Obviosuly in the 300k rows there are a few '15' that are in the middle of
the numbers. I was able to cut the amount of arounious results by doing this
search as the column format is 15-XXXX-XXXX-XXXX
select *
FROM GL1400
WHERE GL_ACCT '15-%';
GO
I have been searching for the command that will only search the first two
characters with no such luck.
Any help would be appreciated.
Thanks
Beginnerselect *
FROM GL1400
WHERE left(GL_ACCT,2) = '15'
GO
http://sqlservercode.blogspot.com/|||Am I missing something, I do not see a formula that you wrote.
here is an example of the culumn:
14-15-123
15-11-123
expected result = 1 row
15-11-123
Given the above example you can see where my curent search failed.
SELECT *
FROM GL1400
where GL_ACCT LIKE '02-%';
GO
Using the above fomula I am getting both results.
"Absar Ahmad" wrote:
> Seems that followign command will help you. Check BOL for details of this
> command:
> Left
> If this is not what you are looking for, please send DDL of GL1400 table a
nd
> a few rows of sample data along with example of expected Result from the
> query.
> "bluesrock12000" wrote:
>|||I am using the LEFT function
LEFT(FIELD,2) = '15'
WHERE left(GL_ACCT,2) = '15'|||Any of the following query should work:
SELECT *
FROM GL1400
where left(GL_ACCT,2) = '15'
or
SELECT *
FROM GL1400
where GL_ACCT like '15%'
Note: The second query can benefit from index on GL_ACCT.
"bluesrock12000" wrote:
> Am I missing something, I do not see a formula that you wrote.
> here is an example of the culumn:
> 14-15-123
> 15-11-123
> expected result = 1 row
> 15-11-123
>
> Given the above example you can see where my curent search failed.
> SELECT *
> FROM GL1400
> where GL_ACCT LIKE '02-%';
> GO
> Using the above fomula I am getting both results.|||
> select *
> FROM GL1400
> WHERE GL_ACCT '15-%';
> GO
> I have been searching for the command that will only search the first two
> characters with no such luck.
select *
FROM GL1400
WHERE GL_ACCT LIKE '15%'
(You had no operator, so what it was effectively doing was a bit like:
SELECT GL_ACCT '15' -- I.e. select the values from GL_ACCT, but give the
column an alias of "15"|||Seems that followign command will help you. Check BOL for details of this
command:
Left
If this is not what you are looking for, please send DDL of GL1400 table and
a few rows of sample data along with example of expected Result from the
query.
"bluesrock12000" wrote:
> I have a table where the first column has several numbers in numerous
> formats. I would like to sort by using the first two digits. (15)
> This is what I have right now.
> select *
> FROM GL1400
> WHERE GL_ACCT '15';
> GO
> Obviosuly in the 300k rows there are a few '15' that are in the middle of
> the numbers. I was able to cut the amount of arounious results by doing th
is
> search as the column format is 15-XXXX-XXXX-XXXX
> select *
> FROM GL1400
> WHERE GL_ACCT '15-%';
> GO
> I have been searching for the command that will only search the first two
> characters with no such luck.
> Any help would be appreciated.
> Thanks
> Beginner|||Thank you for all your help,
That was exactly what I was looking for.
"Absar Ahmad" wrote:
> Any of the following query should work:
> SELECT *
> FROM GL1400
> where left(GL_ACCT,2) = '15'
> or
> SELECT *
> FROM GL1400
> where GL_ACCT like '15%'
> Note: The second query can benefit from index on GL_ACCT.
> "bluesrock12000" wrote:
>
>
No comments:
Post a Comment