how to convert nvarchar to time in sql server

I have column called Shift_End in my table but the data type is nvarchar.  All the values for this column are military format but nvarchar.  For example like this:
Shift_End
1500
1600
1730
1800
1830
So i want to pull all records where Shift_End > current datetime.  If shift_end is 1600, then i want to see all records after 1600.
moe57Asked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>I have column called Shift_End in my table but the data type is nvarchar
Two design mistakes on somebody's part:  Storing time data as an nvarchar, and storing time data separate from the date.  If this is to keep track of shifts, you'll need the date and time together to track hours where the shift crosses over midnight.

Either way, try this..
CREATE TABLE #time (Shift_End nvarchar(10)) 

INSERT INTO #time (Shift_End)
VALUES ('1500'), ('1600'), ('1730'), ('1800'), ('1830')

SELECT Shift_End
FROM #time
WHERE CAST(SHift_End as int) > 1600 AND TRY_CAST(Shift_End as int) IS NOT NULL

Open in new window

0
UnifiedISCommented:
You could easily convert your nvarchar to int and and compare to the current time by turning that into an int

DECLARE @V nvarchar(50)
SET @V = '1600'
SELECT CAST(@V AS int)
SELECT DATEPART(HOUR,GETDATE()) * 100 + DATEPART(MINUTE, GETDATE())
0
UnifiedISCommented:
I think the shift_end is part of a work schedule, not the time from a punch clock, correct?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

moe57Author Commented:
yes that is part of the work schedule
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>where Shift_End > current datetime.
<correction>  Didn't catch that part, try this...
CREATE TABLE #time (Shift_End nvarchar(10)) 

INSERT INTO #time (Shift_End)
VALUES ('1500'), ('1600'), ('1730'), ('1800'), ('1830')

Declare @current_hhmm int 
SET @current_hhmm = (DATEPART(hh, getdate()) * 100)  + DATEPART(mm, getdate())

SELECT Shift_End
FROM #time
WHERE CAST(SHift_End as int) > @current_hhmm AND TRY_CAST(Shift_End as int) IS NOT NULL

Open in new window

0
moe57Author Commented:
just to clarify, i have a date column in the table and the data type for that column datetime but the time is separete and nvarchar
0
UnifiedISCommented:
This should work:

SELECT *
FROM TABLE
WHERE CAST(Shift_End AS int) >  (SELECT DATEPART(HOUR,GETDATE()) * 100 + DATEPART(MINUTE, GETDATE()) )
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
Scott PletcherSenior DBACommented:
i want to pull all records where Shift_End > current datetime.  If shift_end is 1600, then i want to see all records after 1600.

Those contradict each other.  Do you want shift end > current datetime, or current datetime > shift end?
0
moe57Author Commented:
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
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.