Solved

Duplicate + trim records SQL

Posted on 2016-09-08
11
50 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 18

Expert Comment

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

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

Open in new window

0
 
LVL 18

Expert Comment

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

Expert Comment

by:Lee Savidge
ID: 41789752
You're welcome :)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 31

Expert Comment

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

Expert Comment

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

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 31

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 31

Expert Comment

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

Expert Comment

by:Pawan Kumar Khowal
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Access Date Query 28 29
Group by and order by clause 28 36
SQL 2016 Setup - Connectivity Issues 4 17
Test a query 23 19
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

746 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