Showing posts with label modify. Show all posts
Showing posts with label modify. Show all posts

Sunday, March 11, 2012

DateTime format

Hello all,

I'm trying to write a query against an exisiting table that i can't modify and i'm running into a bit of a problem. The table stores timestamps as a char field instead of a datetime.

So, i've had to use the CONVERT function to change it to a datetime during my query. A sample is below:

SELECT convert(datetime, logged, 120) FROM AP200310

This works, except i want to include the option of querying a single day. Since the data that is returned is in this format:

12/12/2006 6:54:15 PM

The following sql statement doesn't work:
SELECT convert(datetime, logged, 120) FROM AP200310 WHERE logged = '12/12/2006'

Thanks in advance for any help.

Have you tried using cast instead of convert?|||

You need to handle the logged field because it is a datetime field.

You can try this to get your query to work:

SELECT

CONVERT(NVARCHAR(10),logged,120)

FROM

AP200310

WHERE(CONVERT(NVARCHAR(10),logged,101)='12/12/2006')

|||

Thanks for the quick response.

I tried your code and it didn't seem to work.

Help!!

|||

Can you post the results of the query limno posted? and tell us why it does not work. It seems to work for me.

declare @.ttable (col1int identity, col2char(22))insert into @.tvalues ('12/12/2006 6:54:15 PM')insert into @.tvalues ('12/12/2006 6:55:15 PM')insert into @.tvalues ('12/20/2006 6:54:15 PM')insert into @.tvalues ('10/12/2006 6:54:15 PM')selectconvert(nvarchar(10),col2,120), *from @.twhere(CONVERT(NVARCHAR(10),col2,120) ='12/12/2006')

|||

Here is my query.

SELECT CONVERT(NVARCHAR(10), LOGGED, 120) AS LOGGED
FROM AP200612
WHERE (CONVERT(NVARCHAR(10), LOGGED, 101) = '12/12/2006')

My results are nothing is returned.

To help matters, i've included a copy of the schema of the table in question:

CREATETABLE [dbo].[AP200612](

[ACCOUNT] [char]

(9)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612ACCOUNT]DEFAULT(''),

[LOGGED] [char]

(19)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612LOGGED]DEFAULT(''),

[ORIGIN] [decimal]

(3, 0)NULLCONSTRAINT [gmc_AP200612ORIGIN]DEFAULT((0)),

[STAT_NUM] [char]

(5)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612STAT_NUM]DEFAULT(''),

[SUBLOC] [char]

(5)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612SUBLOC]DEFAULT(''),

[SUFFIX] [char]

(2)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612SUFFIX]DEFAULT(''),

[TERM] [char]

(5)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612TERM]DEFAULT(''),

[TIME] [char]

(19)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612TIME]DEFAULT(''),

[TYPE] [char]

(3)COLLATE SQL_Latin1_General_CP1_CI_ASNULLCONSTRAINT [gmc_AP200612TYPE]DEFAULT('')

)

ON [PRIMARY]

Thanks for your responses and any future responses.

Richard M.

|||

I just replaced your column name and table name and it works for me:

You have different format numbers - 120 and 101, both are same though. Can you also post some sample rows?

declare @.ttable (col1int identity, col2char(19))insert into @.tvalues ('12/12/2006 6:54:15')insert into @.tvalues ('12/12/2006 6:55:15')insert into @.tvalues ('12/20/2006 6:54:15')insert into @.tvalues ('10/12/2006 6:54:15')SELECTCONVERT(NVARCHAR(10), col2, 120)AS col2FROM @.tWHERE (CONVERT(NVARCHAR(10), col2, 101) ='12/12/2006')

|||Hi rmethod, ndinakar's solution should work, I'd like know how you insert rows into the AP200612 table. BTW, if the LOGGED column is used to store some date, why not use DATETIME/SMALLDATETIME data type? Then you can use rich?T-SQLDate and Time functions to filter rows based on the LOGGED column.

Saturday, February 25, 2012

Dates Error

Hi:

I got the next problem, when I try to modify a record of my SQL Server Database from my Delphi application the next message error appears

"Date is less than 01/12/2003"

The record that I'm trying to modify was inserted from the same applicaition.

I'm not so sure if it's a database problem, but I don't know why it is passing. What can I do?

Thaks for your help!!

Cristopher SerratoNope,

Probably someone wrote a trigger to check to rows modified date. Someone probably updated the row since you got it last..

Your update has to supply a date if I'm not mistaken...

They basically want you to requery the data so you can work with th most current version of data...

Just a guess...

Tuesday, February 14, 2012

Date_Time Convert(24) to DateTime Format

Please help me modify the sql statement so that I average wly values for
FullScan and CPU.
My Date_Time and samples are written to a table with the
char(24) format.
I need to convert the Date_Time field from char(24) to datetime format.
Please help me with this task.
Thanks,
Date_Time char(24)
2005-01-24 16:06:48.966
2005-01-24 16:07:48.966
2005-01-24 16:08:48.966
select
dateadd(hour,datediff(hour,'20040802',[d
ate_Time]),'20050202') as HourStart,
avg([Full_Scan_Sec]) as FullScan,
avg([CPU_Processor_Time]) as CPU
from Server_Data
where date_time BETWEEN '20040802' and '20050202'
group by dateadd(hour,datediff(hour,'20040802',[d
ate_Time]),'20040802')
order by dateadd(hour,datediff(hour,'20040802',[d
ate_Time]),'20050202')Joe
Have you tried CONVERT system function?
"Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
news:42A097A1-85E1-4252-A39E-5A7320E65268@.microsoft.com...
> Please help me modify the sql statement so that I average wly values
for
> FullScan and CPU.
> My Date_Time and samples are written to a table with the
> char(24) format.
> I need to convert the Date_Time field from char(24) to datetime format.
> Please help me with this task.
> Thanks,
>
> Date_Time char(24)
> 2005-01-24 16:06:48.966
> 2005-01-24 16:07:48.966
> 2005-01-24 16:08:48.966
> select
> dateadd(hour,datediff(hour,'20040802',[d
ate_Time]),'20050202') as
HourStart,
> avg([Full_Scan_Sec]) as FullScan,
> avg([CPU_Processor_Time]) as CPU
> from Server_Data
> where date_time BETWEEN '20040802' and '20050202'
> group by dateadd(hour,datediff(hour,'20040802',[d
ate_Time]),'20040802')
> order by dateadd(hour,datediff(hour,'20040802',[d
ate_Time]),'20050202')
>|||I tried to convert the Date_Time from char(24) to smalldatetime received
syntax error.
select counterDateTime from CounterData (NOLOCK)
where convert(smalldatetime,counterdatetime)
Line 2: Incorrect syntax near ')'.
Please help me resolve this problem.
Thank You,
"Uri Dimant" wrote:

> Joe
> Have you tried CONVERT system function?
>
> "Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
> news:42A097A1-85E1-4252-A39E-5A7320E65268@.microsoft.com...
> for
> HourStart,
>
>|||> select counterDateTime from CounterData (NOLOCK)
> where convert(smalldatetime,counterdatetime)
Abive is not a valid WHERE clause. It is similar to saying:
WHERE colname
A WHERE clause need some predicate, like:
WHERE colname = 23
What do you want to achieve? Return only the rows in where you have a string
in the column that can
be converted to datetime? If so, try:
WHERE ISDATE(counterdatetime) = 1
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
news:84F6AF1A-E00B-416D-95CB-F5A3C05054BD@.microsoft.com...
> I tried to convert the Date_Time from char(24) to smalldatetime received
> syntax error.
> select counterDateTime from CounterData (NOLOCK)
> where convert(smalldatetime,counterdatetime)
> Line 2: Incorrect syntax near ')'.
> Please help me resolve this problem.
> Thank You,
>
>
> "Uri Dimant" wrote:
>|||I'm trying to convert the DateTime column format from char(24) to
smalldatetime in a sql query. When it's in smalldatetime format then use the
average function to calculate average full_scan and CPU values.
The sql statements listed below.
Thank You,
"Tibor Karaszi" wrote:

> Abive is not a valid WHERE clause. It is similar to saying:
> WHERE colname
> A WHERE clause need some predicate, like:
> WHERE colname = 23
> What do you want to achieve? Return only the rows in where you have a stri
ng in the column that can
> be converted to datetime? If so, try:
> WHERE ISDATE(counterdatetime) = 1
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
> news:84F6AF1A-E00B-416D-95CB-F5A3C05054BD@.microsoft.com...
>
>|||I'm not sure exactly what your problem is. You posted a query in the origina
l post, but you didn't
say what happens when you run the query. Are you saying that below part fail
s? If so, what error
message do you get? Or incorrect results?
dateadd(hour,datediff(hour,'20040802',[d
ate_Time]),'20050202')
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
news:4D4CBFAA-70AB-498A-BBE4-3F9497793250@.microsoft.com...
> I'm trying to convert the DateTime column format from char(24) to
> smalldatetime in a sql query. When it's in smalldatetime format then use t
he
> average function to calculate average full_scan and CPU values.
> The sql statements listed below.
> Thank You,
>
> "Tibor Karaszi" wrote:
>