Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2004, 06:53 AM
Authorized User
 
Join Date: Sep 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Concatenate numbers and string

Hello, I am new with SQL, and I am quite lost . Please could anyone help me, please?

I have to generate a datetime automatically, to select the start and the end datetime for my query.

I need something like:

@datetime = @day + ' ' + @Month + ' ' + @year + ' 11:00:00'

I have been trying to concatenate them, but it doesn't work.

Regards,
Mushu

 
Old September 10th, 2004, 07:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

What are the data type declarations for your variables?

@day, @Month, etc need to be of type char (or varchar) to concatentate them. The result is of type char. Assuming @datetime is of type datetime, you will need to CAST the resultant character string to a datetime datatype, as:
Code:
@datetime = CAST(@day + '/' + @Month + '/' + @year + ' 11:00:00' as datetime)

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 13th, 2004, 03:14 AM
Authorized User
 
Join Date: Sep 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, thank you for your early answer :D. I have still some problems.

This is my code:

set nocount on
declare @startdate datetime
declare @enddate datetime
declare @day integer
declare @month nvarchar
declare @year integer
declare @daychar nvarchar
declare @yearchar nvarchar
declare @date nvarchar
set @day = day(getdate())
--select day(getdate())
set @month = datename(month, getdate())
--select datename(mm, getdate())
set @year = year(getdate())
--select year(getdate())
set @daychar = cast(@day as nvarchar)
set @yearchar = cast(@year as nvarchar)
set @date = @daychar + ' ' + @month + ' ' + @yearchar
set @startdate = @date + ' 09:00:00'
set @enddate = @date + ' 10:00:00'
set nocount off
-- Result = 1; @month = datename(month, getdate()) = S
-- It looks like I have to define the number of characters, but I don't know,
-- It is a variable
select @date, @daychar + ' ' + @month + ' ' + @yearchar + ' 09:00:00'

What could be te solution?

Thanks, regards,
Mushu

 
Old September 13th, 2004, 05:00 AM
Authorized User
 
Join Date: Sep 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, it's me again, I think I got it :D, this is the code:

set nocount on
--declare @startdate datetime
declare @startdate nvarchar(26)
--declare @enddate datetime
declare @enddate nvarchar(26)
declare @day integer
declare @month nvarchar(9)
declare @year integer
declare @daychar nvarchar(2)
declare @yearchar nvarchar(4)
declare @date nvarchar(17)
set @day = day(getdate())
--select day(getdate())
set @month = datename(month, getdate())
--select datename(mm, getdate())
set @year = year(getdate())
--select year(getdate())
set @daychar = cast(@day as nvarchar)
set @yearchar = cast(@year as nvarchar(4))
set @date = @daychar + ' ' + @month + ' ' + @yearchar
set @startdate = @date + ' 10:00:00'
set @enddate = @date + ' 11:00:00'
set nocount off
-- Result = 1; @month = datename(month, getdate()) = S
-- It looks like I have to define the number of characters, but I don't know it,
-- It is a variable
select top 1 datetime, value, qualitydetail from history
where tagname = 'IS_Feed_Wt_pH'
and datetime >= @startdate
and datetime <= @enddate
order by datetime desc

in this way I get the last value inserted in the interval of time.

Thanks,
Mushu


 
Old September 13th, 2004, 05:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Mushu,

Why do you have to declare so many such variables?

How about this?
Code:
declare @startdate datetime
declare @enddate datetime

set @startdate = convert(varchar(10),getdate(),121) + ' 10:00:00'
set @enddate = convert(varchar(10),getdate(),121) + ' 11:00:00'

select top 1 datetime, value, qualitydetail from history
where tagname = 'IS_Feed_Wt_pH'
and datetime >= @startdate
and datetime <= @enddate 
order by datetime desc
It can be done like this too, but I believe this would be a bit slow than the first one.
Code:
select top 1 datetime, value, qualitydetail from history
where tagname = 'IS_Feed_Wt_pH'
and datetime >= convert(varchar(10),getdate(),121) + ' 10:00:00'
and datetime <= convert(varchar(10),getdate(),121) + ' 11:00:00'
order by datetime desc
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old September 14th, 2004, 09:20 AM
Authorized User
 
Join Date: Sep 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much happygv, it's just what I need.

I know: keep it simple







Similar Threads
Thread Thread Starter Forum Replies Last Post
replacing numbers in a string cole SQL Server 2000 4 April 2nd, 2007 04:24 PM
find and replace numbers in a string -- wildcards cs8271506 Beginning VB 6 3 March 1st, 2006 07:41 AM
string concatenate phelkuizon Classic ASP Databases 1 September 13th, 2004 04:08 AM
Concatenate numbers fs22 Javascript 1 April 25th, 2004 06:51 AM
Parsing a string of numbers tp194 Classic ASP Databases 1 January 5th, 2004 10:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.