Showing posts with label varchar. Show all posts
Showing posts with label varchar. Show all posts

Tuesday, March 27, 2012

Ho to Convert Varchar datatype into datetime


declare @.a varchar(10)
select @.a= shiftstarttime from o_parameter
print @.a

declare @.b varchar(10)
select @.b= shiftendtime from o_parameter
print @.b


declare @.dt varchar(20)
set @.dt=Left(getdate(),12)

print @.dt

declare @.dt1 varchar(20)
set @.dt1=Left(dateadd(dd, 1,getdate()),12)
print @.dt1

declare @.dt3 varchar(20)

set @.dt3= @.dt1 + space(0) + @.a

print @.dt3

declare @.dt4 varchar(20)

set @.dt4= @.dt + space(0) + @.b

print @.dt4

output of above script is as fallow

09.30am
06.30pm
Aug 22 2007
Aug 23 2007
Aug 23 2007 09.30am
Aug 22 2007 06.30pm

now i want to convert @.dt3 and @.dt4 into datetime .

because i wnat to calculate datedifference in hours by using this function

SET @.in_hour=DATEDIFF (HH ,@.D1,@.D2)

where @.D1 and @.D2 are datetime parameters.

but how can i get @.dt3 and @.dt4 into datetime so i can pass it to DATEDIFF() function.

so plz Guide me. or if u know how to write it then plz write here

i already use cast for it but it doesn't work.

so plz reply urgently.

Try this:

Code Snippet

DECLARE @.dt3 varchar(20), @.dt4 varchar(20), @.in_hour int

SET @.dt3 = 'AUG 23 2007 09:30am'

SET @.dt4 = 'AUG 23 2007 06:30am'

SET @.in_hour=DATEDIFF(HH,CAST(@.dt3 as datetime), CAST(@.dt4 as datetime))

SELECT @.in_hour

or

Code Snippet

DECLARE @.dt3 datetime, @.dt4 datetime, @.in_hour int

SET @.dt3 = CAST('AUG 23 2007 09:30am' as datetime)

SET @.dt4 = CAST('AUG 23 2007 06:30am' as datetime)

SET @.in_hour=DATEDIFF(HH,@.dt3,@.dt4)

SELECT @.in_hour

|||

You can use the following query,

Code Snippet

select

Datediff(HH,

cast(Convert(varchar, dateadd(dd, 1,getdate()),101) + ' ' + replace(shiftstarttime,'.',':') as datetime),

cast(Convert(varchar, dateadd(dd, 1,getdate()),101) + ' ' + replace(shiftendtime,'.',':') as datetime))

from

o_parameter

--or

Select datediff(HH,Convert(datetime, replace(shiftstarttime,'.',':')), Convert(datetime, replace(shiftendtime,'.',':')))

from

o_parameter

Wednesday, March 7, 2012

hierarchical selection within a select statment

CREATE TABLE RS_A(ColA char(1), ColB varchar(10))

INSERT INTO RS_A
VALUES ('S', 'shakespeare')
INSERT INTO RS_A
VALUES ('B', 'shakespeare')
INSERT INTO RS_A
VALUES ('P', 'shakespeare')

INSERT INTO RS_A
VALUES ('S', 'milton')
INSERT INTO RS_A
VALUES ('P', 'milton')
INSERT INTO RS_A
VALUES ('B', 'shelley')

INSERT INTO RS_A
VALUES ('B', 'kafka')
INSERT INTO RS_A
VALUES ('S', 'kafka')

INSERT INTO RS_A
VALUES ('P', 'tennyson')

SELECT * FROM RS_A

Now i need a select which selects based on hierarchy

if ColA = 'S', then select only that row
else if ColA = 'B' then select only that row
else if colA = 'P' then select only that row

So my results should look like
S shakespeare
S milton
B shelley
S kafka
P tennyson

Is there a way to do this within a select statement
I tried using a CASE in WHERE CLAUSE but it put out all rows which
existed/

If any of you can help me with this right away, its is greatly
appreciated
Thanks in advance(rshivaraman@.gmail.com) writes:

Quote:

Originally Posted by

SELECT * FROM RS_A
>
Now i need a select which selects based on hierarchy
>
if ColA = 'S', then select only that row
else if ColA = 'B' then select only that row
else if colA = 'P' then select only that row
>
So my results should look like
S shakespeare
S milton
B shelley
S kafka
P tennyson
>
Is there a way to do this within a select statement
I tried using a CASE in WHERE CLAUSE but it put out all rows which
existed/


First translate the codes to numeric values with CASE, you can take
MIN, and then translate back:

SELECT CASE minval WHEN 1 THEN 'S' WHEN 2 THEN 'B' WHEN 3 THEN 'P' END,
ColB
FROM (SELECT ColB, minval = MIN(CASE ColA
WHEN 'S' THEN 1
WHEN 'B' THEN 2
WHEN 'P' THEN 3
END)
FROM RS_A
GROUP BY ColB) AS x

If there are many possible values for ColA, it would be better to
put the mapping in a table and then join with that table.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Ingenius :
Thank you for the above and the RETURN was what was missing after
RAISEERROR

-RS