Solved

Construct Date in SQL

Posted on 2006-11-19
4
1,020 Views
Last Modified: 2010-07-27
Hello all,

I have a Stored Proc that take a date as a parameter.  I would like to create a variable that contains the same date as the passed value but I want to change the time to 11:59pm.

For example if the proc gets the date '10/25/2006 8:00 am', I would like my varaible to say "10/25/2006 11:59 pm'.
If the proc gets a date without time I still need it to work, so '10/26/2006' would be '10/26/2006 11:59 pm'

Thanks
Jym
0
Comment
Question by:jymmealey
[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
4 Comments
 
LVL 14

Expert Comment

by:mherchl
ID: 17977588
declare @datenew datetime
set @datenew = convert(datetime, convert(varchar,@olddate, 112) + ' 23:59')
0
 
LVL 6

Accepted Solution

by:
Gokulm earned 500 total points
ID: 17977603
You can construct the date as follows and return

SELECT  CONVERT(char(10), GETDATE(), 101) + ' 11:59 PM'

This will retun a character

SELECT CAST( CONVERT(char(10), GETDATE(), 101) + ' 11:59 PM' AS datetime)

This will return a datetime
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17977607
>For example if the proc gets the date '10/25/2006 8:00 am', I would like my varaible to say "10/25/2006 11:59 pm'.
I guess because you want to get all before 10/26/2006...

where yourfield < dateadd(day, 1, convert(datetime, convert (varchar(10), @parameter, 101), 101))
0
 
LVL 2

Author Comment

by:jymmealey
ID: 17977630
mherchl,

I ran the following and got an error:

SELECT CONVERT(datetime, CONVERT(varchar,'10/26/2006 8:00 am', 112) + ' 23:59')

Error was
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

I assume this is because of the type 112, it is expecting ISO?  

Ran Gokulm's query and it worked like expected.

Thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

696 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