?
Solved

MS SQL substring a character

Posted on 2008-10-07
3
Medium Priority
?
590 Views
Last Modified: 2010-04-21
I viewed searched and found one of the questions that pertains to my issue "MS SQL substring a character"  The response did not work for me however i was hoping to get more assistance with a similar issue.

I am looking to pull the characters that happen on the left side of the first ':' for instance i could have data that looks like 12:31:01 or 13:20 or 1:00 or 3:00:09:00

In all of these cases i would like to pull out and use the 12,13,1, and 3

The code i attempted to modify was

substring(ltrim(rtrim(field)), 1, Charindex('$', ltrim(rtrim(field)))

I modified it to look like this:

substring(ltrim(rtrim([Time])), 1, Charindex(':', ltrim(rtrim([time])))

in which i received an error invalid or missing Expression.
0
Comment
Question by:JPLLaf
[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
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22664708
Try this:
SELECT LEFT('12:31:01', CHARINDEX(':', '12:31:01')-1)

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22664727
Or simply this if you know there is at least one number up to max of 2 before the first ':'.
SELECT REPLACE(LEFT(LTrim(RTrim([Time])), 2), ':', '')

Open in new window

0
 

Author Closing Comment

by:JPLLaf
ID: 31504031
The second solution works great, for some reason the first one caused errors in previous ddate time formulas in my View.  Awesome work 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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

800 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