Sunday, February 12, 2012

Convert DateTime to NVARCHAR

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=2Eg. 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=E9Hi
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
"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é
>|||Thanks a lot for your help this was exactly what I needed.
Ren=E9

No comments:

Post a Comment