Solved

Duplicate + trim records SQL

Posted on 2016-09-08
11
59 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
  • 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 28

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 28

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 28

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 28

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell code to list items in dropdown 6 57
Foxpro errors 4 28
Attaching Database Failed ? 3 37
SQL - Update field defined as Text 6 15
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

813 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

11 Experts available now in Live!

Get 1:1 Help Now