Home All Groups Group Topic Archive Search About

Convert DateTime to NVARCHAR

Author
12 Nov 2005 5:03 PM
refdk
Hi,

My test table looks like this:

1    Id                 int    4    0
0    DateTimes       datetime    8    1
0    DatoTid             nvarchar    14    1

With the following content:

1    15-10-2005 10:22:00    15102006102200
2    16-10-2005 11:16:00    17102005111600

I would like to show the records that are different, but I can't see
how to get rid of the <->, <:> and <space> characters.

It would be enough only to get the records that has differents dates
e.g. 16-10-2005<>17102005. I have tried the following statement in
diffrents ways but....


SELECT [Id], CONVERT(NVARCHAR(10), DateTimes, 113) as DatTid, DatoTid
  FROM [SQL_Eksempler].[dbo].[Dato]
    WHERE CONVERT(NVARCHAR(10), DateTimes, 113)<>Datotid

/René

Author
12 Nov 2005 6:30 PM
John Bell
Hi

To do a string comparison try using convert for your datatime and then use
replace to get rid of the unwanted characters:

SELECT id, CAST(REPLACE(CONVERT(CHAR(10), DateTimes, 105),'-',SPACE(0)) +
REPLACE(CONVERT(CHAR(8), DateTimes, 108),':',SPACE(0)) AS NVARCHAR(14)),
DatoTie
FROM #Dates
WHERE CAST(REPLACE(CONVERT(CHAR(10), DateTimes, 105),'-',SPACE(0)) +
REPLACE(CONVERT(CHAR(8), DateTimes, 108),':',SPACE(0)) AS NVARCHAR(14)) <>
DatoTie

John

Show quote
"refdk" wrote:

> Hi,
>
> My test table looks like this:
>
> 1    Id                 int    4    0
> 0    DateTimes       datetime    8    1
> 0    DatoTid             nvarchar    14    1
>
> With the following content:
>
> 1    15-10-2005 10:22:00    15102006102200
> 2    16-10-2005 11:16:00    17102005111600
>
> I would like to show the records that are different, but I can't see
> how to get rid of the <->, <:> and <space> characters.
>
> It would be enough only to get the records that has differents dates
> e.g. 16-10-2005<>17102005. I have tried the following statement in
> diffrents ways but....
>
>
> SELECT [Id], CONVERT(NVARCHAR(10), DateTimes, 113) as DatTid, DatoTid
>   FROM [SQL_Eksempler].[dbo].[Dato]
>     WHERE CONVERT(NVARCHAR(10), DateTimes, 113)<>Datotid
>
> /René
>
>
Author
13 Nov 2005 7:12 AM
refdk
Thanks a lot for your help this was exactly what I needed.

René

AddThis Social Bookmark Button