Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Duplicate + trim records SQL

Posted on 2016-09-08
11
Medium Priority
?
73 Views
Last Modified: 2016-09-08
Hi!
I have a sql and want 1 of the columns to return as 2 equal columns but one of them to be trimed. Is it possible?
Lets say you write "select files from images" and want the output to be "image1.jpg" in one of the columns and "image1" in the other.
0
Comment
Question by:Adam_Li
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 41789710
so, this?

select files, replace(files, '.jpg', '') as filesnoext from images

Open in new window

0
 

Author Closing Comment

by:Adam_Li
ID: 41789741
Quick answear! This works very well. Thanks
0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41789749
Are you looking for this

SELECT File, REPLACE(File, '.jpg', '') FileName from images

Open in new window

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41789750
Great !
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41789752
You're welcome :)
0
 
LVL 32

Expert Comment

by:awking00
ID: 41789877
You might have also used -
select files, left(files,len(files) - 4) as name from images
0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41789883
This will not work if your extension length is more than 3. E.g. jpeg
0
 
LVL 32

Expert Comment

by:awking00
ID: 41790105
And the accepted answer wouldn't work if the extension was .jpeg (or any other extension) either :-)
0
 
LVL 32

Expert Comment

by:awking00
ID: 41790111
Probably the safest way is to find the index of the ." and take the substring up until that index.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41790113
... of the "." ...
0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41790591
@awking00 - Yes you are correct. I have a solution for this.

@Author - if your extension can vary then use below rather than the accepted solution. To handle this you are can declare a variable and accept a value in there.

DECLARE @Ext AS VARCHAR(10) = '.jpg'

SELECT File, REPLACE(File, @Ext, '') FileName from images

Open in new window


Also please update the accepted solution.

Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

722 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