Solved

Duplicate + trim records SQL

Posted on 2016-09-08
11
67 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 500 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 29

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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 29

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 29

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 29

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

615 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