Questions tagged [sql-convert]

SQL `CONVERT()` is a general function that converts an expression of one data type to another.

SQL CONVERT() is a general function that converts an expression of one data type to another.

Note that this is a non-standard function. The ANSI/ISO SQL standard has the CAST() function.

Reference

104 questions
2
votes
1 answer

Error in Converting varchar to datetime

I am trying to convert varchar to datetime, everything is ok when I use the convert function like this: SELECT CONVERT(Datetime, '2016-01-01 00:00:00.000', 120) but when I try to declare a varchar parameter and pass it to the function, I encounter…
Amid Gol
  • 305
  • 2
  • 7
2
votes
1 answer

SQL Server 2008 - CONVERT vs DATE & DATETIME Columns

I store an amendment date field in a lot of my tables. Sometimes I need to use the time portion for my logic and sometimes I just need the date portion. I'm wondering which of the following scenarios is more efficient/best practice: Storing two…
AGB
  • 2,378
  • 21
  • 37
1
vote
1 answer

SQL Format and Convert functions in a select statement

I have a select and I need to format a few of it´s dates (in BBDD they are in YYYY-MM-DD and I need them in DD/MM/YYYY). Looking around I found two different methods (both work OK) 1: FORMAT(pb.FINICIO, 'dd/MM/yyyy') as finicio 2:…
Grismak
  • 192
  • 16
1
vote
2 answers

SQL Server update same column using different conversion without overwriting

I have a table DATES that looks like this: Id TimeStamp TimeStamp_UTC 1 2021-08-03 12:10:30 2021-08-03 12:10:30.000 2 2021-09-13 12:21:44 NULL 3 11/23/2021 1:30:56.511 PM NULL 4 11/23/2021 1:37:27.476 PM NULL The column TimeStamp…
1
vote
1 answer

How can I use cast in SQL with multiple column selections?

I tried this to get our revenue: SELECT CAST(stays_in_week_nights AS int) + CAST(stays_in_weekend_nights AS int) * CAST(adr AS int) AS revenue FROM Hotels I expected to get a number as the result of the calculation - but…
lora
  • 25
  • 4
1
vote
2 answers

Converting string to datetime works with select but nor update (T-SQL)

I have a table in my database that has has both the original user input date string, then tries to get the datetime from that by using CONVERT CREATE TABLE #UserInput ( actualDate DATETIME NULL, dateString VARCHAR(50) NULL ) The…
M. McCrary
  • 35
  • 3
1
vote
3 answers

Convert to dd/MM/yyyy from character string

I have the following output that is stored in a string field: Query SELECT StringField5 FROM MyTable WHERE OrderID=1 Output 02/13/2018 I would like to use one of the ways SQL allow to convert/cast to get the following output: 13/02/2018. I have…
1
vote
1 answer

Azure SQL Data Warehouse - Strange DateTime conversion error/ behaviour

I am reading data from data lake (csv) and when running the below query, I am getting a 'Conversion failed when converting date and/or time from character string' error message. select convert(datetime, NullIf(ltrim(rtrim([Date started])), ''),…
1
vote
1 answer

The multi-part identifier could not be found via convert query

select convert(date, Main_Users_ActivityLog.CreateDate); I'm having trouble working on this query. How can I convert all contributions from a column to the type I wish to use, only a line of code?
Yves Ng
  • 69
  • 10
1
vote
2 answers

Syntax error When convert from MySQL to T-SQL

I'm new in SQL . I'm converting mysql to SQL. I've found an error when I use DateAdd function. Here is the query: SELECT [id], [test_code], [test_name], [test_price], [discount], DATEADD(CURRENT_TIMESTAMP,…
user3074921
1
vote
5 answers

Convert time float to format HH:mm sql server

I need to format a float decimal number into a time format hour:minute. I wrote this Scalar-Value Functions with an input float and output varchar(6): CREATE FUNCTIONE formatOre ( @input float ) returns varchar(6) as begin declare @n float; declare…
img.simone
  • 632
  • 6
  • 10
  • 23
1
vote
1 answer

Executing Stored Procedure With Dynamic Date Parameter - Conversion Error

I'm trying to execute the following stored procedure with a dynamic date parameter so that it picks up 6 months ago to yesterday. When I run on a server with US language settings it works however when I run on a server with UK language settings I…
Baseline9
  • 101
  • 1
  • 3
  • 12
1
vote
1 answer

Count result date when they are equals

I only need Date where they are not repeated like: Date Quantity 2017-06-20 9 2017-08-18 88 2017-06-21 30 So I tried: select CONVERT(date, UpdatedDate) as 'Date', COUNT(*) as 'Flights Quanttity' from…
Vainx507
  • 11
  • 2
1
vote
1 answer

Convert datetime to date using a function

I have one column in my database that I need to convert from datetime to the date data type using a SQL Server function. I can't figure out how to get the correct syntax. Could I get some help with the syntax so IntelliSense will stop yelling at me…
ShreddedSaber
  • 125
  • 1
  • 11
1
vote
1 answer

Wrong conversion to datetime in SQL Server

In Excel, when I convert these 'General' numbers to dates, which is what they really are, I get different numbers than when I CONVERT() them in SQL Server. The data is coming from an import. General Excel conversion to date SQL Server…
PRIME
  • 73
  • 1
  • 3
  • 10