Solved

Concat date part to time part

Posted on 2011-02-28
6
1,016 Views
Last Modified: 2012-05-11
This is SQL 2000....

I have a piece of SQL code and I do a comparision. See below.
That works but I created a table to hold "cut off" hours for shipments. Now, I dont know how to do the comparison..i somehow need to concat "date" of "getdate" to "time" from the table...then do the comparison..

see below please
--This is how it currently works...we want to check the *hour*
DECLARE @CUTOFF DATETIME
declare @poDate datetime

SET @CUTOFF = dateadd(hour,16,CONVERT(VARCHAR,GETDATE(),101))
set @poDate ='2011-02-04 10:38:00.000'

-- Then compare
If @poDate <= @cutoff Begin
 .....
End

------ This is what i want to do -----------

-- But for cutoff date, I created a table with values like this, looking at the *hour*
 --1900-01-01 15:00:00.000
 --1900-01-01 19:00:00.000
create table #cutoff
(
   id int identity,
   shipmethod varchar(20),
   cutoffHour datetime

)
insert into #cutoff ('USPS','1900-01-01 16:00:00.000')
insert into #cutoff ('UPSA','1900-01-01 19:00:00.000')
/*
But now, the date is "1900-01-01" and I cant just do a comparision. I think I need to somehow get the "date" part from getdate()
and concat it to Time part and then do the comparision. 
*/
select @cutoffhour = cutoffhour from #cutoff --somehow concat Date of getdate to time from cutoff hour

If @poDate <= @cutoffHour Begin
 .....
End

Open in new window

0
Comment
Question by:Camillia
6 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 125 total points
ID: 35002974
create table #cutoff
(
   id int identity,
   shipmethod varchar(20),
   cutoffHour datetime

)
insert into #cutoff  select 'USPS',CONVERT(varchar(10) , GETDATE(), 120)+' 16:00:00.000'
insert into #cutoff  select 'UPSA',CONVERT(varchar(10) , GETDATE(), 120)+' 19:00:00.000'

select * from #cutoff
0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 35003937
pls check below code. It will  concat "date" of "getdate" to "time" from the table:

select cast(getdate() as int)+ @cutoffhour
0
 
LVL 6

Assisted Solution

by:anushahanna
anushahanna earned 125 total points
ID: 35003945
the below are other date formats you can put to use:
http://www.sql-server-helper.com/tips/date-formats.aspx
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 125 total points
ID: 35004470
IF YOU HAVE A CUT OFF HOUR THEN WHY NOT JUST STORE The hour and compare the hours?


isn't your table too simplistic? won't the cut off time depend on the actual date/day of week etc....

so you should have two tables... 1 for the default  cut off hours per day of the week... by method
and the second as an exception table to cater for national holidays etc....

hour(date)

convert(char(8),getdate(),112)  give the YYYYMMDD date to which you can then attach a character time component

 as HH:MM  (with a space between the date and the time component....
0
 
LVL 9

Assisted Solution

by:rajeevnandanmishra
rajeevnandanmishra earned 125 total points
ID: 35007117
Hi,

In your original query you can use the below line:
 
select @cutoffhour = convert(char(10), @poDate, 111) 
              + cutoffhour from #cutoff --somehow concat Date of getdate to time from cutoff hour

Open in new window


And this will include your cutOffHour with the PoDate.
0
 
LVL 7

Author Comment

by:Camillia
ID: 35007430
going to try now. will post back
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Nested Case statement 4 39
VB.net Duplicating a table - primary key not created 3 32
MS SQL Server select from Sub Table 14 27
SQL Recursion schedule 13 19
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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