SQL - CONVERT or whatever is used to take txt field CUSTOM_FIELD and extract date and then use in WHERE statement

Table : PHM_ENHANCED_CHGS
Field : CUSTOM_FIELD
Data type : text
Example : 201503302304

Field is a date time stamp.

I need to be able to do whatever is needed to the field data and then do a WHERE date between here and here.
ghettocounselorDirector Pharmacy InformaticsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
Without knowing your table name or fields names, this may get you going:

SELECT *
FROM YourTable
WHERE CONVERT(datetime, MyStringField) BETWEEN '2015-01-01' AND '2015-03-31 23:59:59.997'

If your fields do not have the time with the date, you can try this:

SELECT *
FROM YourTable
WHERE CONVERT(datetime, MyStringField) BETWEEN '2015-01-01' AND '2015-03-31'
0
Scott PletcherSenior DBACommented:
Let's take the month of March 2015 as an example:

WHERE CUSTOM_FIELD >= '201503010000' AND CUSTOM_FIELD < '201504010000'

Date/datetime comparisons should use >= and < rather than >= and <=, even when they're stored in text :-).  That insures accuracy and performance even if the data type of the column changes.

Btw, never use functions against a table column unless it's absolutely unavoidable, because then SQL can't fully use indexes in that case.  In technical terms, the function makes it "nonsargable".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ghettocounselorDirector Pharmacy InformaticsAuthor Commented:
Duh! sheesh, funny how sometimes you look at something and don't see the simplicity available. Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.