SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Function to convert date time to string


SQL Function to convert date time to string

Author
Message
kishorefeb28
kishorefeb28
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 96
Hi Experts,

please help me in writing a SQL Function as below

the input parameter for function should be datetime of sql datetimeformat

and out put should be a string = yyyymmdd1 or yyyymmdd2

the last character 1 or 2 based on below condition
if time is between 6AM and 5.59PM then 1
if 6PM to 5.59AM then 2

please help me with such function am running out of time

thank you.

Kishore
Mitesh Oswal
Mitesh Oswal
SSC Eights!
SSC Eights! (910 reputation)SSC Eights! (910 reputation)SSC Eights! (910 reputation)SSC Eights! (910 reputation)SSC Eights! (910 reputation)SSC Eights! (910 reputation)SSC Eights! (910 reputation)SSC Eights! (910 reputation)

Group: General Forum Members
Points: 910 Visits: 653

USE Master
GO

DECLARE @dt DATETIME

SELECT @dt = '2014/01/27 01:00 PM'

select REPLACE(CONVERT(VARCHAR(10),@dt,111),'/','')+CASE WHEN DATEPART(HH,@dt) >= 6 AND DATEPART(HH,@dt) < 18 THEN '1' ELSE '2' END


SELECT @dt = '2014/01/27 01:00 AM'

select REPLACE(CONVERT(VARCHAR(10),@dt,111),'/','')+CASE WHEN DATEPART(HH,@dt) >= 6 AND DATEPART(HH,@dt) < 18 THEN '1' ELSE '2' END



Regards,
Mitesh OSwal
+918698619998
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16222 Visits: 19545
;WITH SampleData AS (
SELECT MyDate = CAST('2014-01-27 05:59:59.997' AS DATETIME) UNION ALL
SELECT '2014-01-27 06:00:00.000' UNION ALL
SELECT '2014-01-27 06:00:00.003' UNION ALL
SELECT '2014-01-27 17:59:59.994' UNION ALL
SELECT '2014-01-27 17:59:59.997' UNION ALL
SELECT '2014-01-27 18:00:00.000')
SELECT
MyDate,
CONVERT(CHAR(8),MyDate, 112) + CASE WHEN DATEPART(HOUR,MyDate) >= 6 AND DATEPART(HOUR,MyDate) < 18 THEN '1' ELSE '2' END
FROM SampleData



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
kishorefeb28
kishorefeb28
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 96
It Worked

Thanks a ton Mitesh
kishorefeb28
kishorefeb28
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 96
Thank you for the reply chris.:-)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26038 Visits: 17528
--EDIT--

It is Monday morning and I misread this post entirely. Need more coffee.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
nelia.dragoo
nelia.dragoo
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 265
Greetings!

Would using date type 112 not remove the need to replace the "/" characters in the date string?

This would reduce the date string portion from

REPLACE(CONVERT(VARCHAR(10),@dt,111),'/','')



to

CONVERT(VARCHAR(8),@dt,112)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search