Solved

need sql Query in sqlserver2008 or 2005

Posted on 2010-11-12
8
529 Views
Last Modified: 2012-05-10
hi ,

i have table called EMP.
tHE EMP TABLE CONTAINS  10 EMP NAMES LIKE SHOWN BELOW.
I NEED THE QUERY FOR TO GET
REQUIREMENT1.
--------------------
THE FIRST FIVE EMPNAMES NEED TO COME IN UPPERCASE AND
THE REST FIVE EMPNAMES IN LOWER CASE.
PLS PROVIDE ME THE QUERY FOR THE ABOVE.

REQUIREMENT2
-------------------
tHE EMP TABLE CONTAINS 10 RECORDS.FOR EX
THE EMP NAME CONTAINS RAVIKUMAR AS EMPNAME.
I NEED THE FIRST 4 CHARACTERS IN UPPERCASE AND REST 5 CHARACTERS IN LOWERCASE
WHICH IS SHOWN BELOW.

EMPNAME
-------------
RAVIkumar
ANANDkumar.

pls provide me the seperate quries for the 2 requirements.


Thanks
Ravi


0
Comment
Question by:mu_ravi1
8 Comments
 
LVL 6

Expert Comment

by:DalHorinek
ID: 34126846
Requirement 1:

-- it assumes that it has some primary key called id

(SELECT UPPER(name) FROM emp ORDER BY id LIMIT 5)
   UNION ALL
(SELECT LOWER(name) FROM emp ORDER BY id DESC LIMIT 5);
-- but it changes order, if you're sure there's only 10 records in table, you use:
(SELECT UPPER(name) FROM emp LIMIT 5)
   UNION ALL
(SELECT LOWER(name) FROM emp LIMIT 5,5);

Requirement 2:

SELECT CONCAT(UPPER(SUBSTR(name, 1,4)), LOWER(SUBSTR(name, 5,CHAR_LENGTH(name)-4))) FROM emp
0
 
LVL 6

Expert Comment

by:DalHorinek
ID: 34126861
Actually that 2nd wouldn't work probably on MS, I didn't notice that.

SELECT UPPER(SUBSTRING(name, 1,4)) + " " + LOWER(SUBSTRING(name, 5,LEN(name)-4)) FROM emp

This should be correct.
0
 

Author Comment

by:mu_ravi1
ID: 34126962
hi DalHorinek

Requirement1 query
The table contains more the 10 records

(SELECT UPPER(name) FROM emp ORDER BY id LIMIT 5)
   UNION ALL
(SELECT LOWER(name) FROM emp ORDER BY id DESC LIMIT 5);

This query given not executed.
it is giving the below error

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'ORDER'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'ORDER'.
-----------------------------------------------------------------------------
Requirement 2
SELECT UPPER(SUBSTRING(name, 1,4)) + " " + LOWER(SUBSTRING(name, 5,LEN(name)-4)) FROM emp

This query given not executed.
it is giving the below error

Invalid length parameter passed to the SUBSTRING function.

pls help me
Ravi

0
 

Author Comment

by:mu_ravi1
ID: 34126972
Hi DalHorinek
FYI
iam using sqlserver 2005.
regards
ravi
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 7

Expert Comment

by:rmm2001
ID: 34128716
#1 I'm confused..
do you want it like this?
JAMES
ADAM
SAM
LUKE
JON
jane
laura
joan
emily
sara
TOM
DON
...



#2
SELECT UPPER(SUBSTRING(name, 1,4)) + ' ' + LOWER(RIGHT(LEFT(name,4), LEN(name)-4)) FROM emp
0
 
LVL 13

Accepted Solution

by:
devlab2012 earned 250 total points
ID: 34136629
#1
--first five UPPER, next five lower, other records unaffected
SELECT CASE WHEN RowNum <=5 then Upper(Name) WHEN RowNum <=10 then Lower(Name) else Name end Name
FROM(
      select Name, Row_Number() over(order by Name) RowNum from emp
) t

--returns only first 10 records, first five UPPER, next five lower
SELECT CASE WHEN RowNum <=5 then Upper(Name) WHEN RowNum <=10 then Lower(Name) else Name end Name
FROM(
      select top 10 Name, Row_Number() over(order by Name) RowNum from emp
) t


Requirement 2
I see that RAVI and ANAND are like first name of person. You want to make it upper case. But there is no way that sql can detect by itself that this portion of a string is first name. You can only convert a fixed length to upper case. Following query will do this-
SELECT CASE WHEN LEN(Name) >= 4 THEN UPPER(SUBSTRING(Name, 1,4)) + LOWER(SUBSTRING(Name,5, LEN(Name)-4))
ELSE UPPER(Name) END Name
FROM Employee

0
 

Author Comment

by:mu_ravi1
ID: 34142612
Hi DalHorinek

Thank u very much for ur concern.
now the quries are working fine.

one small modification in the first reqiriemnt.

#1
--first five UPPER, next five lower, other records unaffected
SELECT CASE WHEN RowNum <=5 then Upper(Name) WHEN RowNum <=10 then Lower(Name) else Name end Name
FROM(
      select Name, Row_Number() over(order by Name) RowNum from emp
) t

in the above mentioned

i need the query for n number of records not fixed 10 records.
The table may have n number of records.
could u pls change the query for 'n' number of records.
Sorry for the late reply i am in vacation.
Ravi
0
 

Author Closing Comment

by:mu_ravi1
ID: 34143805
Excellent
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now