Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 @Declare

Author  Topic 

cjsmile999
Starting Member

5 Posts

Posted - 2011-09-03 : 20:53:52
Hello Everyone,

Yes I'm a complete noob at this, don't laugh :)! Still trying to learn this stuff.

I have two strings in my DB one String Date (yyyy-mm-dd) and one String Time (hh:mm:ss). I want to combine the two into one datetime field, and to be honest I'm not sure where to begin. This is the error I'm receiving:
"Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "trips.cxldate" could not be bound.
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "trips.cxltime" could not be bound."

And this is what I have so far:


declare @sdate as varchar(10)
set @sdate = sl.sldate

declare @stime as varchar(8)
set @stime = sl.sltime

select @sdate + ' ' + @stime from SL

--Convert(dateadd(...


Thank you for you're help!!!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-03 : 22:17:18
You apparently aren't showing us your full code as your errors are referring to things you haven't shown us.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-03 : 22:25:30
Here's a quick example how to combine them:

declare @v1 datetime

select @v1 = c1 + ' ' + c2
from t1
where ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cjsmile999
Starting Member

5 Posts

Posted - 2011-09-03 : 23:15:23
No really that all I have so far...I know it's pathetic! When I check it, it doesn't find any errors but when I execute it that's the error that it shows. I'm not sure if it makes a difference, but I'm using SQL 2005. I'm going to start with the example you provide and let you know how I come out. Thanks for your help.
Go to Top of Page

cjsmile999
Starting Member

5 Posts

Posted - 2011-09-03 : 23:43:52
OK, I took your example, but it doesn't return any results. It says it completed successfully, but it doesn't return anything.

declare @v1 datetime

select @v1 = sldate + ' ' + sltime
from SL
where cost > 0

Also, I noticed you have the @declare set to datetime. Does that mean when the strings are combined it's automatically converted to a datetime or do I still have to convert it?

Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-04 : 00:04:06
is your attempt to store the values inside a variable or to show it? if its former, you might need a table variable if your query returns multiple date values and your variable can hold only one.it should be something like


declare @datetable table
(
sdate datetime
)

insert @datetable
select sldate + ' ' + sltime from SL



if its latter you just need only this


select sldate + ' ' + sltime from SL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-04 : 00:15:22
quote:
Originally posted by cjsmile999

OK, I took your example, but it doesn't return any results. It says it completed successfully, but it doesn't return anything.

declare @v1 datetime

select @v1 = sldate + ' ' + sltime
from SL
where cost > 0

Also, I noticed you have the @declare set to datetime. Does that mean when the strings are combined it's automatically converted to a datetime or do I still have to convert it?

Thank you



I put it into a variable so you won't see a result set returned. I used a variable as that seemed to be what you wanted from your first post. If you don't care about reusing this anywhere, do it like visakh's example.

Regarding the conversion, yes for that concatenation it'll implicitly convert it as that format is supported by datetime. You can check out the CONVERT function in BOL for more details.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -